mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Peter Wang <novalazy@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: alternative form flag with zero octal value
Date: Wed, 10 Jan 2018 21:17:50 -0500	[thread overview]
Message-ID: <20180111021750.GH1627@brightrain.aerifal.cx> (raw)
In-Reply-To: <20180111020246.GG1627@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 970 bytes --]

On Wed, Jan 10, 2018 at 09:02:46PM -0500, Rich Felker wrote:
> On Thu, Jan 11, 2018 at 11:43:05AM +1100, Peter Wang wrote:
> > Hi,
> > 
> > I'm not certain it is a bug, but this program produces "00" instead of "0":
> > 
> >     #include <stdio.h>
> >     int main(void)
> >     {
> >         printf("%#o\n", 0);
> >         return 0;
> >     }
> > 
> > Please Cc: any replies as I am not subscribed to this list.
> 
> Indeed, this is a bug. I'm not sure whether it was introduced in
> commit 78897b0dc00b7cd5c29af5e0b7eebf2396d8dce0 or already present,
> but it was not present in Dmitry Levin's original more complex version
> of the patch. I'm going to essentially revert this commit and replace
> it with an alternate one-line fix using infrastructure that wasn't
> available at the time but added later in commit
> 167dfe9672c116b315e72e57a55c7769f180dffa.
> 
> Thanks for the report.

Here's the patch I'm going to push if I don't find any problems with
it.

Rich

[-- Attachment #2: 0001-fix-printf-alt-form-octal-with-value-0-and-no-explic.patch --]
[-- Type: text/plain, Size: 1830 bytes --]

From b64539ae06aa91a407359238f4e909adb9bfab3d Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Wed, 10 Jan 2018 20:45:02 -0500
Subject: [PATCH] fix printf alt-form octal with value 0 and no explicit
 precision

commit 78897b0dc00b7cd5c29af5e0b7eebf2396d8dce0 wrongly simplified
Dmitry Levin's original submitted patch fixing alt-form octal with the
zero flag and field width present, omitting the special case where the
value is zero. as a result, printf("%#o",0) wrongly prints "00" rather
than "0".

the logic prior to this commit was actually better, in that it was
aligned with how the alt-form flag (#) for printf is specified ("it
shall increase the precision"). at the time there was no good way to
avoid the zero flag issue with the old logic, but commit
167dfe9672c116b315e72e57a55c7769f180dffa added tracking of whether an
explicit precision was provided.

revert commit 78897b0dc00b7cd5c29af5e0b7eebf2396d8dce0 and switch to
using the explicit precision indicator for suppressing the zero flag.
---
 src/stdio/vfprintf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 15356f5..50fb55c 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -559,7 +559,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 			if (0) {
 		case 'o':
 			a = fmt_o(arg.i, z);
-			if ((fl&ALT_FORM) && p<z-a+1) prefix+=5, pl=1;
+			if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
 			} if (0) {
 		case 'd': case 'i':
 			pl=1;
@@ -574,7 +574,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 			a = fmt_u(arg.i, z);
 			}
 			if (xp && p<0) goto overflow;
-			if (p>=0) fl &= ~ZERO_PAD;
+			if (xp) fl &= ~ZERO_PAD;
 			if (!arg.i && !p) {
 				a=z;
 				break;
-- 
2.10.0


  reply	other threads:[~2018-01-11  2:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-11  0:43 Peter Wang
2018-01-11  2:02 ` Rich Felker
2018-01-11  2:17   ` Rich Felker [this message]
2018-01-11  8:12     ` Jens Gustedt
2018-01-11 18:55       ` Rich Felker
2018-01-11 19:36         ` Jens Gustedt
2018-01-12  0:38           ` Rich Felker
2018-01-12  8:54             ` Jens Gustedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180111021750.GH1627@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=novalazy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).