Stefan Hagen <sh+openbsd-ports@codevoid.de> wrote:
> Omar Polo wrote (2022-06-30 10:26 CEST):
> > Stefan Hagen <sh+openbsd-ports@codevoid.de> wrote:
> > > I think we could:
> > >
> > > locale_t oldloc = duplocale(LC_GLOBAL_LOCALE);
> > > locale_t modloc = newlocale(LC_NUMERIC_MASK, "C", oldloc);
> > > freelocale(oldloc);
> > > locale_t newloc = uselocale(modloc);
> > > freelocale(modloc);
> > ^^^^^^
> > you mean newloc here
>
> No, from my understanding newloc is the resource that's being used.
> So oldloc and modloc could be freed.
well, I already said my knowledge of the locale api is limited, but
setlocale(3) says quite clearly that:
RETURN VALUES
The function uselocale() returns the thread-specific locale which is in
use right before the call, or the special return value LC_GLOBAL_LOCALE
if the thread used the global locale before the call.
so after the uselocale call in your example, it's `modloc' to be in use
and `newloc' to be the previous value. At that point if I'm reading the
manpage correctly you should compare it against LC_GLOBAL_LOCALE and
otherwise freelocale(3) it.
freeing "modloc" (which is now in use) will cause UB since you're
possibly freeing memory used by the libc. (not on OpenBSD since
freelocale(3) here it's a no-op, but on other systems...)
> > > From our uselocale(3) manpage:
> > >
> > > The current thread uses newloc until uselocale() is called again
> > > successfully with a non-null argument in the same thread, and
> > > passing newloc to freelocale(3) or newlocale(3) before that results
> > > in undefined behaviour.
> > >
> > > I think calling freelocale(newloc) at this point would result in
> > ^^^^^^
> > and modloc here, right?
>
> No, newloc. modloc is only read and based on the information in modloc
> the thread locale is set. modloc is not copied into the thread locale.
>
> > modloc is in use after uselocale and freelocale(3) it would cause UB,
> > while "newloc" is the one used up to that point and _i think_ can be
> > free'd. No idea what' the default locale for a thread and if that can
> > be passed to uselocale tho.
>
> Read /usr/src/lib/libc/locale/uselocale.c
>
> It uses the passed locale for informational purpose only. It is not
> used afterwards. It's really the returned locale that's tricky.
In this case, I think it's more interesting to look at another
implementation does since the locale support in our libc is (for a good
reason) limited.
See for e.g. the musl one:
http://git.musl-libc.org/cgit/musl/tree/src/locale/uselocale.c
No comments:
Post a Comment