proper semantics for returning a character string?

Natanael Copa natanael.copa at gmail.com
Thu Apr 20 21:32:56 UTC 2006


On Thu, 20 Apr 2006 12:17:18 -0400 (EDT)
"Robert P. J. Day" <rpjday at mindspring.com> wrote:

> On Thu, 20 Apr 2006, Jason Schoon wrote:
> 
> > You have a nice potential for memory leakage using this approach.
> > If this is a routine called numerous times without exiting the
> > application, you will slowly be losing all that dynamically
> > allocated space.
> 
> it occurs to me to ask what happens if you try mapping a UID to a
> username numerous times using the standard library routines.
> 
> i'm assuming you would use:
> 
>   struct passwd *getpwuid(uid_t uid);
> 
> right?  this would give you a dynamically-allocated "struct passwd",
> out of which you would extract the username.  so far, so good.
> 
> but what happens to that "struct passwd" space?  i'm assuming it stays
> around as well so that, if you call that routine a thousand times with
> the same UID, you'll get a thousand "passwd struct"s lying around,
> even though it's the same UID each time, is that correct?

no. the space get overwritten second time. The code is not re-entrant.

       The return value may point to static area, and may  be  overwritten  by
       subsequent calls to getpwent(), getpwnam(), or getpwuid().

Please, please, I don't want to be the only one reading the manuals.

> 
> if that's true, then even the *standard* semantics for this operation
> have the potential for memory leakage, no?

Please, read the manuals before you make us read them for you.

-- 
Natanael Copa



More information about the busybox mailing list