[PATCH] locale: fix memory leaks in write_locales and write_charmaps

Ruslan Valiyev linuxoid@gmail.com
Sun Aug 23 10:54:50 GMT 2026


Hi Arjun,

Thanks for the review.

> Since every invocation of this macro uses the result of an xstrdup, I
> think it would be cleaner to pass in the un-duplicated string, perform
> a GET using it, and then xstrdup only when necessary, right before
> PUT.

Done -- PUT_UNIQUE now takes the plain string and only allocates on the
insert path:

  #define PUT_UNIQUE(name) \
    do \
      { \
        const char *put_name_ = (name); \
        if (GET (put_name_) == NULL) \
          PUT (xstrdup (put_name_)); \
      } \
    while (0)

The call sites drop their xstrdup accordingly.

> While at it, there's also a whitespace issue: a missing space before
> the "\" at the end of each line.

Fixed.

> I see that it's correct to use PUT here since we are just starting,
> but it might be worth using PUT_UNIQUE everywhere just for
> consistency.

Done -- "POSIX" and "C" go through PUT_UNIQUE as well.

I also put the measured figures in the commit message: before the change
"locale -a" leaks 74 bytes in 3 blocks directly and 835 bytes in 49
blocks indirectly, and "locale -m" leaks 2190 bytes in 227 blocks.  Both
are valgrind-clean afterwards.

v2 follows, rebased onto current master.

Thanks,
Ruslan


More information about the Libc-alpha mailing list