getpwent_r(3) needs gr->gr_mem to be freed, but that's not documented

Alejandro Colomar alx@kernel.org
Sun Sep 28 21:34:06 GMT 2025


Hi Collin,

On Sun, Sep 28, 2025 at 02:27:52PM -0700, Collin Funk wrote:
> Cristian Rodríguez <cristian@rodriguez.im> writes:
> 
> > On Sun, Sep 28, 2025 at 5:40 PM Alejandro Colomar <alx@kernel.org> wrote:
> >>
> >> On Sun, Sep 28, 2025 at 10:26:33PM +0200, Alejandro Colomar wrote:
> >> > Hi!
> >> >
> >> > Another thing I realized about getpwent_r(3) et al. is that gr->gr_mem
> >>
> >> Actually, getgrent_r(3).
> >>
> >> > is allocated by the function, but is expected to be freed by the caller.
> >> >
> >> > I didn't find this documented, neither in the manual page nor in the
> >> > glibc manual.  Is it documented but I missed it?  Or did I misunderstand
> >> > something?  Or is it just that nobody documented it?
> >
> > It is just not documented it seems, that's great..another footgun..
> 
> POSIX also doesn't require the gr_mem member be allocated using malloc.

Unless I'm missing something, POSIX only defines getgrent(3), but not
getgrent_r(3).  The former keeps track of the malloc(3)d pointer in the
static structure, and thus can free internally; the user need not call
free(3).  The latter can't keep track of that, and thus it's the user's
problem.


Have a lovely night!
Alex

> 
> Collin
> 

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250928/126416e8/attachment.sig>


More information about the Libc-alpha mailing list