[Bug 234430] bc(1) writes error messages to standard output instead of standard error

bugzilla-noreply at freebsd.org bugzilla-noreply at freebsd.org
Wed Dec 26 23:46:40 UTC 2018


https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234430

            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 at FreeBSD.org
          Reporter: mcdutchie at 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 and
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=$(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 'bc',
but as of FreeBSD 12.0 that hasn't happened.

The OpenBSD patch that fixes the bug is here:
https://marc.info/?l=openbsd-tech&m=148767278323276&w=2

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


More information about the freebsd-bugs mailing list