Bug 267282 - [PATCH] strfmon: Attempt to fix some strfmon(3) bugs
Summary: [PATCH] strfmon: Attempt to fix some strfmon(3) bugs
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-standards (Nobody)
URL: https://github.com/freebsd/freebsd-sr...
Keywords: patch
Depends on:
Blocks:
 
Reported: 2022-10-23 03:49 UTC by Jose Luis Duran
Modified: 2023-01-26 18:52 UTC (History)
1 user (show)

See Also:


Attachments
MWE (1.73 KB, text/plain)
2022-10-23 03:49 UTC, Jose Luis Duran
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jose Luis Duran 2022-10-23 03:49:26 UTC
Created attachment 237546 [details]
MWE

Attached is a simple MWE that reproduces a few bugs in the current strfmon(3) code.

I have created a GitHub pull request (#619), which attempts to fix them:
https://github.com/freebsd/freebsd-src/pull/619

$ ./strfmon_bugs
  Format: [%8n] [%8n]
Expected: [ $123.45] [ $123.45]
  Actual: [ $123.45] [       $123.45]

  Format: [%(#5n]
Expected: [ $   123.45 ] [($   123.45)] [ $ 3,456.78 ]
  Actual: [$   123.45] [($   123.45)] [$ 3,456.78]

  Format: [%!(#5n]
Expected: [    123.45 ] [(   123.45)] [  3,456.78 ]
  Actual: [   123.45] [(   123.45)] [ 3,456.78]

  Format: [%n] (n_cs_precedes = 0, n_sep_by_space = 2, n_sign_posn = 1)
Expected: [- 123.45$]
  Actual: [-123.45$]

  Format: [%i]
Expected: [USD123.45]
  Actual: [USD 123.45]

  Format: [%i]
Expected: [123,45 EUR]
  Actual: [123,45 EUR ]

Posting it here fore visibility and tracking.
Comment 1 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:10 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0efec50e9e08f76e9e9d6a024763ca0fe83ae1f2

commit 0efec50e9e08f76e9e9d6a024763ca0fe83ae1f2
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-13 16:22:54 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon(3): Remove repeated words

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:11 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=947efadc3d6e778a824618d82f53f061bec69b77

commit 947efadc3d6e778a824618d82f53f061bec69b77
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-14 23:26:32 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon: Fix alignment when enclosed by parentheses

    Take into consideration the possibility of quantities enclosed by
    parentheses when aligning.

    Matches the examples from The Open Group's:

    Format  Before          After
    %(#5n   [$   123.45]    [ $   123.45 ]  Use an alternative pos/neg style
            [($   123.45)]  [($   123.45)]
            [$ 3,456.78]    [ $ 3,456.78 ]

    %!(#5n  [   123.45]     [    123.45 ]   Disable the currency symbol
            [(   123.45)]   [(   123.45)]
            [ 3,456.78]     [  3,456.78 ]

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/strfmon.html

    SD5-XSH-ERN-29 is applied, updating the examples for %(#5n and %!(#5n.

    Obtained from:  Darwin
    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c            | 12 ++++++++++--
 lib/libc/tests/stdlib/strfmon_test.c |  4 ++--
 2 files changed, 12 insertions(+), 4 deletions(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:12 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=750fe3e6a4619e040c7b0951775698b61290102e

commit 750fe3e6a4619e040c7b0951775698b61290102e
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-18 02:24:03 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon: Fix an edge case when sep_by_space is 2

    Fix an edge case by printing the required space when, the currency
    symbol succeeds the value, a space separates the sign from the value and
    the sign position precedes the quantity and the currency symbol.

    In other words:

        n_cs_precedes = 0
        n_sep_by_space = 2
        n_sign_posn = 1

    From The Open Group's localeconv[1]:

    > When {p,n,int_p,int_n}_sep_by_space is 2:
    > If the currency symbol and sign string are adjacent, a space separates
    > them; otherwise, a space separates the sign string from the value.

        Format    Before        After
        [%n]      [-123.45¤]    [- 123.45¤]

    [1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/localeconv.html

    Obtained from:  Darwin
    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c            | 8 ++++++--
 lib/libc/tests/stdlib/strfmon_test.c | 2 +-
 2 files changed, 7 insertions(+), 3 deletions(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:13 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f81dfea2912dbc0560587ab534a3d8549dbbb95b

commit f81dfea2912dbc0560587ab534a3d8549dbbb95b
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-14 17:05:22 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:16 +0000

    strfmon: Code cleanup

    No functional change intended.
    Not claiming full style(9) compliance.

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c | 132 ++++++++++++++++++++++++----------------------
 1 file changed, 68 insertions(+), 64 deletions(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:13 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=34f88528edba44b2703ba8c772bef077eca33dab

commit 34f88528edba44b2703ba8c772bef077eca33dab
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-21 19:34:09 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon: Fix formatting of a second fixed-width value

    There is a bug when formatting two consecutive values using fixed-widths
    and the values need padding.  This was because the value of pad_size
    was zeroed only every other time.

    Format           Before                         After
    [%8n] [%8n]      [ $123.45] [       $123.45]    [ $123.45] [ $123.45]

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c            | 2 +-
 lib/libc/tests/stdlib/strfmon_test.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:14 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0afd11d50f277c24e80dd0228b122bcc53d559c0

commit 0afd11d50f277c24e80dd0228b122bcc53d559c0
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-13 14:49:21 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:16 +0000

    strfmon: Fix typos in source code comments

    s/defult/default
    s/internaltion/international

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:15 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=3f97d37ac586f43022fa54fdde881bf30550e67d

commit 3f97d37ac586f43022fa54fdde881bf30550e67d
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-16 04:01:17 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:29:26 +0000

    strfmon_test: Add some tests

    Attempt to test the correctness of strfmon(3).

    Some of them were inspired from the examples section at:
    https://pubs.opengroup.org/onlinepubs/9699919799/functions/strfmon.html

    Items marked with XXX represent an invalid output.

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/tests/stdlib/strfmon_test.c | 151 +++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
Comment 8 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:16 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9e03b903e377c75a60cbbb89ed78955769a1c804

commit 9e03b903e377c75a60cbbb89ed78955769a1c804
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-13 15:51:27 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon: Avoid an out-of-bounds access

    Avoid an out-of-bounds access when trying to set the space_char using an
    international currency format (%i) and the C/POSIX locale.

    The current code tries to read the SPACE from int_curr_symbol[3]:

        currency_symbol = strdup(lc->int_curr_symbol);
        space_char = *(currency_symbol+3);

    But on C/POSIX locales, int_curr_symbol is empty.

    Three implementations have been examined: NetBSD[1], Darwin[2], and
    Illumos[3].  Only NetBSD has fixed it[4].

    Darwin and NetBSD also trim the mandatory final SPACE character after
    reading it.

        Locale         Format    Darwin/NetBSD    FreeBSD/Illumos
        en_US.UTF-8    [%i]      [USD123.45]      [USD 123.45]
        fr_FR.UTF-8    [%i]      [123,45 EUR]     [123,45 EUR ]

    This commit only fixes the out-of-bounds access.

    [1]: https://github.com/NetBSD/src/blob/trunk/lib/libc/stdlib/strfmon.c
    [2]: https://opensource.apple.com/source/Libc/Libc-1439.141.1/stdlib/NetBSD/strfmon.c.auto.html
    [3]: https://github.com/illumos/illumos-gate/blob/master/usr/src/lib/libc/port/locale/strfmon.c
    [4]: https://github.com/NetBSD/src/commit/3d7b5d498aa9609f2bc9ece9c734c5f493a8e239

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c            | 5 +++--
 lib/libc/tests/stdlib/strfmon_test.c | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:16 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=d5980dff6b512fa9ba08d6af3ff365805fed44fd

commit d5980dff6b512fa9ba08d6af3ff365805fed44fd
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-13 14:36:46 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:16 +0000

    strfmon: Fix typo in constant

    s/SUPRESS_CURR_SYMBOL/SUPPRESS_CURR_SYMBOL

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:17 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6da51e19e347c13e133bcba68cc6100c16320a01

commit 6da51e19e347c13e133bcba68cc6100c16320a01
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-21 16:13:27 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:40:17 +0000

    strfmon: Trim the SPACE from international currency symbol

    The international currency symbol (int_curr_symbol) has a mandatory
    SPACE character as the last character.

    Trim this space after reading it, otherwise this extra space will always
    be printed when displaying the int_curr_symbol.

    Fixes the output when the international currency format is selected
    (%i).

        Locale         Format    Before           After
        en_US.UTF-8    [%i]      [USD 123.45]     [USD123.45]
        fr_FR.UTF-8    [%i]      [123,45 EUR ]    [123,45 EUR]

    Note that the en_US.UTF-8 locale states that no space should be printed
    between the currency symbol and the value (sep_by_space = 0).

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.c            | 4 +++-
 lib/libc/tests/stdlib/strfmon_test.c | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:18 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f91301cc792c27c5e3e179b606dd8271a624744b

commit f91301cc792c27c5e3e179b606dd8271a624744b
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-16 03:04:44 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:28:33 +0000

    strfmon_test: Fix typo and remove extra space

    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/tests/stdlib/strfmon_test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Comment 12 commit-hook freebsd_committer freebsd_triage 2022-10-25 21:52:19 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7cfd67ce96d97c263c2c56c5c437426463467689

commit 7cfd67ce96d97c263c2c56c5c437426463467689
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-13 16:11:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-10-25 21:48:20 +0000

    strfmon(3): Fix # explanation

    There's only one value that specifies the number of digits after the
    decimal point (oh, sorry, the "radix character") the other specifies the
    number before...

    While here, add a little more info on the effects of using the #n value.

    Obtained from:  https://github.com/NetBSD/src/commit/d1dd1a086400ae719bde1f2c45938d9bc1d29e8b
    Reviewed by:    kib
    PR:     267282
    Github PR:      #619
    MFC after:      1 week

 lib/libc/stdlib/strfmon.3 | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)