[cs631apue] segfail when using getpwuid
jschauma at stevens.edu
Sun Oct 5 21:02:11 EDT 2014
Jin Sun <jsun6 at stevens.edu> wrote:
> anybody know why this line get a segfail?
I'd guess that getpwuid(3) returned NULL, but you're trying to
dereference the result of that function call without checking it first.
Remember what I keep saying every single week about function calls? If
they _can_ fail under some circumstances, they _will_ fail.
In particular, you appear to be calling getpwuid(3) with gid. There is
no guarantee that for every gid one can think of there must necessarily
be a password file entry (ie a user account).
You want to:
- add some whitespace to make the whole thing not quite so dense
- call getgrgid(3) instead of getpwuid(3) if you're passing a gid
- check the return result of getgrgid(3) before using it as a valid
More information about the cs631apue