bug in putenv()!

Chris Calabrese[mav] cjc at ulysses.homer.nj.att.com
Fri Feb 24 01:05:15 AEST 1989


In article <18436 at adm.BRL.MIL>, Leisner.Henr at xerox.com (marty) writes:
> According to Christopher J. Calabrese
> >> If putenv always malloc'd space, it would start eating up
> some serious memory in something like the shell, where you might
> change the value of an environment variable several hundred
> times in a particularly large script (please don't ask for examples,
> you get the general idea :-)
> 
> I don't agree -- putenv could detect whether an equivalent environment
> string already exists.  If putenv always does a malloc/strcpy, whenever a
> string is overwritten it could free  or realloc the storage.

Yes, this detection would be quite simple, but the real trick
would be to detect if the old value had it's space malloc'd for it.
I don't think most implementations malloc the initial space,
or at least not for each individual value in the array, so you
can't use free() on these.  In order to do this, there would
have to be some kind of guarentee that the space could be free'd
first, and that's highly implementation dependant.

> I had a version which works sorta this way on Ms/Dos (actually the whole
> environment array was one malloced block ).
> A version of putenv on a masscomp I'm looking at right now appears to work
> the same way (it uses realloc on successive calls to putenv after the first
> malloc [hmmm -- maybe someone else had the same idea ;-) ]).

These are reasonable ways of doing it, but it has to be done from
the ground up - you can't just tack on a new version of putenv.

BTW, what do you mean by the `first malloc'?  Do the elements of the
array get created with malloc, or is there some list of spaces
which are allowed to be free'd because they were created with putenv
(one way of avoiding the problems of
non-portability/implementation-dependence)?
-- 
Name:			Christopher J. Calabrese
Brain loaned to:	AT&T Bell Laboratories, Murray Hill, NJ
att!ulysses!cjc		cjc at ulysses.att.com
Obligatory Quote:	``Now, where DID I put that bagel?''



More information about the Comp.unix.wizards mailing list