getpwent_r(3) needs gr->gr_mem to be freed, but that's not documented
Alejandro Colomar
alx@kernel.org
Sun Sep 28 22:25:08 GMT 2025
Hi Mark,
On Sun, Sep 28, 2025 at 02:54:59PM -0700, Mark Harris wrote:
> Alejandro Colomar 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?
>
> All memory referenced by the structure, including gr_mem, comes out of
> the buffer supplied to the function. POSIX doesn't have getgrent_r,
> but it has getgrgid_r, and says:
Hmmm, that's good. Thanks! At least, the user doesn't need to free(3)
anything weird.
So, a good estimate of the size to be allocated prior to the
sgetgrent_r() call should be:
size = strlen(s) + 1 + strchrcnt(s, ',') + 2;
That would be wasting a little bit if there are any commas outside of
the fourth ':'-delimited field, but it should work.
BTW, where's this exactly in the glibc source code? It's a bit hard to
follow.
Have a lovely day!
Alex
> Storage referenced by the group structure is allocated from the
> memory provided with the buffer parameter, which is bufsize bytes in
> size.
--
<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/20250929/643a390c/attachment-0001.sig>
More information about the Libc-alpha
mailing list