Jon Steinhart <[EMAIL PROTECTED]> wrote:

>> Jon Steinhart <[EMAIL PROTECTED]> writes:

>> > 3. If the $HOME environment variable is set, mypath is copied from the
>> >    getenv return.  Why?  It's never changed.

>> > 4. If the $HOME environment variable is not set, mypath is copied from the
>> >    pw_dir member of the returned passwd structure.  Now, I understand that
>> >    this is a static structure, but getpwuid is never called again so I
>> >    don't see why the copy is needed.

>> > 4. If the $HOME environment variable is not set, the pw_dir member of the
>> >    passwd structure returned by getpwuid() is checked for a NULL pointer.
>> >    This can never happen in a non-error return, which is already checked.
>> >    So why the superfluous check?

>> These checks are not superflous, they are for maintainability.
>> Two years from now someone will add a second getenv(3) call and
>> waste their (probably volunteer) time trying to figure out how
>> they busted the home dir variable.  That is why, unless you are
>> writing super-tight-must-be-the-best-performing-code-ever
>> applications (which mh is not), it is necessary always to make a
>> copy of the static buffer pointed to by the return values of such
>> functions.

>> --  
>> Eric Gillespie <*> [EMAIL PROTECTED]

>This is one of those places where we'll have to respectfully disagree.  I'm
>obviously in the minority given the quality of software that I see these days,
>but I think that programming is still something that should be done by
>professionals.  I don't want someone hacking on code that doesn't take the time
>to figure out what's going on first.  Protecting against the really silly
>mistakes allows such people to make really complex ones.  As I said in my
>earlier email, I'm not going to write slow and sloppy  code just because
>computers are fast.  Matter of fact, I keep on trying to get up the courage
>to tackle m_getfld().

>Oh, some details.

> 1.  A second getenv() call would not break the code.  The copy was really
>     unnecessary.

> 2.  It's hard for me to imagine a situation where getpwuid() would #1 get
>     called a second time and #2 for a different uid, which is the only that
>     a problem would occur.

The assumption that there will not be another getpwuid(), nor a
getpwnam(), is a horrible assumption, particularly from somebody who
has just commented about sloppy code.  Your comment about "a different uid"
is also confused -- there is no certainty that a second call for the
same uid will return the same result, and it is sloppy programming
to assume that it will.

Presumably the copy following getenv() is for consistency between
the two cases (getenv() or getpwuid()).

 -NWR

Reply via email to