public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-srcware at vinc17 dot net" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug stdio/23432] incorrect printf output for integers with thousands separator and precision field larger than the number of digits (needing leading zeros)
Date: Tue, 10 Jan 2023 11:47:09 +0000	[thread overview]
Message-ID: <bug-23432-131-owsnEqLIqc@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-23432-131@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=23432

--- Comment #1 from Vincent Lefèvre <vincent-srcware at vinc17 dot net> ---
This bug is still there in glibc 2.36, but this is even worse than what I've
said, as some leading zeros are also missing, probably because glibc considers
the number of bytes of the string with the thousands separator instead of just
the number of digits. Consider the following new testcase:

#include <stdio.h>
#include <locale.h>

int main (void)
{
  volatile int m = 1234567;
  volatile long n = 1234567890;
  if (setlocale (LC_ALL, ""))
    for (int i = 0; i < 2; i++)
      {
        printf ("%.17d\n", m);
        printf ("%'.17d\n", m);
        printf ("%.17ld\n", n);
        printf ("%'.17ld\n", n);
        m = -m;
        n = -n;
      }
  return 0;
}

zira% LC_ALL=en_US.utf8 ./tst
00000000001234567
000000001,234,567
00000001234567890
00001,234,567,890
-00000000001234567
-000000001,234,567
-00000001234567890
-00001,234,567,890

zira% LC_ALL=fr_FR.utf8 ./tst
00000000001234567
00001 234 567
00000001234567890
1 234 567 890
-00000000001234567
-00001 234 567
-00000001234567890
-1 234 567 890

In the fr_FR.utf8 locale, the space is U+202F NARROW NO-BREAK SPACE, which
takes 3 bytes in UTF-8: e2 80 af. This is probably why there are even fewer
leading zeros in the output.

As this can be seen with this testcase, only the thousands separator is handled
incorrectly, not the minus sign for the negative numbers (which doesn't affect
the number of leading zeros).

Note: this might have been partially fixed recently in master (though I
couldn't see any mention related to the thousands' grouping character or the
width field in the Git log), since a change of behavior triggered a failure in
the GNU MPFR testsuite:

https://sympa.inria.fr/sympa/arc/mpfr/2023-01/msg00001.html
https://sympa.inria.fr/sympa/arc/mpfr/2023-01/msg00002.html

(the "expected" value is actually incorrect as it was based on the incorrect
behavior of glibc; so this is currently a bug in the MPFR testsuite). But note
that there is still a missing thousands' grouping character.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2023-01-10 11:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-23432-131@http.sourceware.org/bugzilla/>
2023-01-10 11:29 ` vincent-srcware at vinc17 dot net
2023-01-10 11:47 ` vincent-srcware at vinc17 dot net [this message]
2023-01-10 11:49 ` vincent-srcware at vinc17 dot net
2023-01-10 12:07 ` [Bug stdio/23432] incorrect printf output " vincent-srcware at vinc17 dot net
2023-01-10 12:23 ` [Bug stdio/23432] incorrect printf output for integers " vincent-srcware at vinc17 dot net
2023-01-10 12:32 ` vincent-srcware at vinc17 dot net
2023-01-17 12:15 ` schwab@linux-m68k.org
2023-01-17 12:51 ` vincent-srcware at vinc17 dot net
2023-01-17 13:32 ` schwab@linux-m68k.org
2023-02-02  1:22 ` vincent-srcware at vinc17 dot net
2023-02-06 13:58 ` vincent-srcware at vinc17 dot net
2023-02-06 15:21 ` cvs-commit at gcc dot gnu.org
2023-02-08  1:12 ` cvs-commit at gcc dot gnu.org

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=bug-23432-131-owsnEqLIqc@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).