Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Dec 2018 23:46:37 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 234430] bc(1) writes error messages to standard output instead of standard error
Message-ID:  <bug-234430-227@https.bugs.freebsd.org/bugzilla/>

next in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D234430

            Bug ID: 234430
           Summary: bc(1) writes error messages to standard output instead
                    of standard error
           Product: Base System
           Version: 12.0-STABLE
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: mcdutchie@hotmail.com

Upon encountering a parsing error, bc(1) passes an error message on to
dc(1), which writes the error message to standard output along with the
normal output.

That is a bug. Error messages should go to standard error instead, as
POSIX specifies:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html#tag_20_09=
_10

GNU 'bc', Solaris 'bc', and (as of 2017) OpenBSD 'bc' act like POSIX says a=
nd
write error messages to standard error.

Bizarrely, the exit status of bc(1) is left unspecified:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html#tag_20_09=
_18
And indeed, all versions of 'bc' exit with status 0 if there is an input
error such as a parsing error, so the exit status cannot be used to
catch it. That leaves examining standard error as the only method for a
program calling bc(1), such as a shell script, to distinguish between an
error state and normal operation. That is, with this bug, there is no
way at all.

The following example shell function transparently hardens bc(1) by
intercepting standard error and exiting the program or subshell if an
error was produced.

bc() {
        _bc_err=3D$(command -p bc "$@" 2>&1 1>&3)
        [ -z "${_bc_err}" ] && return
        printf '%s\n' "$0: bc(1) caught errors:" "${_bc_err}" 1>&2
        exit 127
} 3>&1

This bug was fixed on OpenBSD 'bc' back in Feburary 2017. I figured FreeBSD
would eventually pull in that fix since FreeBSD 'bc' is based on OpenBSD 'b=
c',
but as of FreeBSD 12.0 that hasn't happened.

The OpenBSD patch that fixes the bug is here:
https://marc.info/?l=3Dopenbsd-tech&m=3D148767278323276&w=3D2

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-234430-227>