tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: printf(1) and incomplete escape sequence



    Date:        Mon, 02 Jul 2018 17:17:43 +0200
    From:        Leonardo Taccari <leot%NetBSD.org@localhost>
    Message-ID:  <20180702151748.7D45A84DEA%mail.netbsd.org@localhost>

  | What should we do if we receive just a `printf '\''?

Pretty much whatever we please ...  (including printing the environment, if
that was sane..., even dumping core)

  | IIUC according POSIX this is unspecified behaviour

It is when the stray terminating \ is in the arg to a %b conversion,
when it is in the format it is just "forgotten".    There are some in
posix who believe that being "forgotten" is the same as "unspecified"
(as they haven't specified anything...) - I consider it to simply be
a bug (in posix.)

  | and for consistency I have treated it similarly to the `default' case
  | for "unknown escape sequence" 

I think that's best.

For comparison, bash's builtin printf simply prints the \ and doesn't
treat it as an error - I think that's sub-optimal (not in the spirit of posix)
which is fairly clear that error messages should be printed, and the
exit code not be 0, whenever something incorrect is detected.

I have committed a slightly different version of your patch (just to
make it more consistent with what was there before) ... and then I
also fixed the "print error messages twice when there's a bad \
conversion in a %b string" which I noticed while testing this.

While here, and discussing printf, it is also worth noting that we
treat
	printf %b '\64'

as a valid way to print '4', whereas posix actually requires:

	printf %b '\064'

(which also works, of course, including where there are 3
octal digits after the \0.)   The earlier case is another
one which is unspecified.

Does anyone have any problem with that extension?

(nb: in the format string, the 0 after the \ is not required,
and if given counts as being one of the (max 3) digits
that are converted from octal to a char - that is, \0123
in a format string, and in a %b arg are treated differently.
That's just the way it is....)

kre



Home | Main Index | Thread Index | Old Index