Bug 255646 - newlocale(LC_ALL_MASK, (locale_t) 0) wrongly destroys LC_COLLATE in an existing object
Summary: newlocale(LC_ALL_MASK, (locale_t) 0) wrongly destroys LC_COLLATE in an existi...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Many People
Assignee: Yuri Pankov
URL: https://reviews.freebsd.org/D30146
Keywords:
Depends on:
Blocks:
 
Reported: 2021-05-06 01:13 UTC by Karl Williamson
Modified: 2023-09-20 13:28 UTC (History)
1 user (show)

See Also:


Attachments
.c that illustrates the bug (969 bytes, text/plain)
2021-05-06 01:13 UTC, Karl Williamson
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Karl Williamson 2021-05-06 01:13:04 UTC
Created attachment 224718 [details]
.c that illustrates the bug

Compile and run the attached program.

The second newlocale() overwrites LC_COLLATE of the object created by the first newlocale(); the objects should be completely independent
Comment 1 Yuri Pankov freebsd_committer freebsd_triage 2021-05-06 09:00:12 UTC
It's not really destroyed, rather this is a corner case for 3 locales - C, POSIX, C.UTF-8 - using the same static (as in "not loaded from file") collate object, and using newlocale() simply overwrites the name.
Comment 2 Karl Williamson 2021-05-06 20:37:49 UTC
I had considered the possibility that POSIX and C would have the same collating sequence.  But before I filed the report, I tried a third locale, which happened to be C.UTF-8.  I couldn't imagine that it would have the same collating sequence as the other two, and still don't.

\xC0\x80 is a legal sequence in the first two locales, but not in the third.  Are you ignoring input validity in collation?
Comment 3 commit-hook freebsd_committer freebsd_triage 2023-03-28 15:18:37 UTC
A commit in branch main references this bug:

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

commit 269dea90d6eb32690730b0f6a17fb41170483104
Author:     Yuri Pankov <yuripv@FreeBSD.org>
AuthorDate: 2023-03-28 15:10:47 +0000
Commit:     Yuri Pankov <yuripv@FreeBSD.org>
CommitDate: 2023-03-28 15:16:30 +0000

    libc: use separate collate objects for C, POSIX, and C.UTF-8

    Fix newlocale() overwriting the locale name in collate object
    when same instance was used for those locales, and querylocale()
    reporting unexpected value for LC_COLLATE_MASK.

    PR:             255646, 269375
    Reviewed by:    markj, bapt (previous version)
    Differential Revision:  https://reviews.freebsd.org/D30146

 lib/libc/locale/collate.c                    |  17 +++-
 lib/libc/tests/locale/Makefile               |   1 +
 lib/libc/tests/locale/newlocale_test.c (new) | 111 +++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 3 deletions(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2023-09-20 13:27:08 UTC
A commit in branch stable/13 references this bug:

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

commit a4916232acd614d4d7d7226d678b427aacb9730e
Author:     Yuri Pankov <yuripv@FreeBSD.org>
AuthorDate: 2023-03-28 15:10:47 +0000
Commit:     Yuri Pankov <yuripv@FreeBSD.org>
CommitDate: 2023-09-20 13:10:52 +0000

    libc: use separate collate objects for C, POSIX, and C.UTF-8

    Fix newlocale() overwriting the locale name in collate object
    when same instance was used for those locales, and querylocale()
    reporting unexpected value for LC_COLLATE_MASK.

    PR:             255646, 269375
    Reviewed by:    markj, bapt (previous version)
    Differential Revision:  https://reviews.freebsd.org/D30146

    (cherry picked from commit 269dea90d6eb32690730b0f6a17fb41170483104)

 lib/libc/locale/collate.c                    |  17 +++-
 lib/libc/tests/locale/Makefile               |   1 +
 lib/libc/tests/locale/newlocale_test.c (new) | 111 +++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 3 deletions(-)
Comment 5 Yuri Pankov freebsd_committer freebsd_triage 2023-09-20 13:28:04 UTC
Closing as fixed, please reopen if there are still issues here.