]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
libc/locale: Fix races between localeconv(3) and setlocale(3)
authorMark Johnston <markj@FreeBSD.org>
Fri, 17 Sep 2021 14:44:23 +0000 (10:44 -0400)
committerMark Johnston <markj@FreeBSD.org>
Wed, 20 Oct 2021 00:53:33 +0000 (20:53 -0400)
commitf89204d6b99d11aa1f67722e8c1d33b0fc4d61d7
tree772e3cb2522003b3bb80eb2d850b8c557c075ba5
parent5aebce3790cc29a02d3041b5412dc9c83905959a
libc/locale: Fix races between localeconv(3) and setlocale(3)

Each locale embeds a lazily initialized lconv which is populated by
localeconv(3) and localeconv_l(3).  When setlocale(3) updates the global
locale, the lconv needs to be (lazily) reinitialized.  To signal this,
we set flag variables in the locale structure.  There are two problems:

- The flags are set before the locale is fully updated, so a concurrent
  localeconv() call can observe partially initialized locale data.
- No barriers ensure that localeconv() observes a fully initialized
  locale if a flag is set.

So, move the flag update appropriately, and use acq/rel barriers to
provide some synchronization.  Note that this is inadequate in the face
of multiple concurrent calls to setlocale(3), but this is not expected
to work regardless.

Thanks to Henry Hu <henry.hu.sh@gmail.com> for providing a test case
demonstrating the race.

PR: 258360
Sponsored by: The FreeBSD Foundation

(cherry picked from commit 7eb138a9e53636366e615bdf04062fedc044bcea)
lib/libc/locale/lmonetary.c
lib/libc/locale/lnumeric.c
lib/libc/locale/localeconv.c