[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: So you think you are (or wanna be) a hacker



John Summerfield wrote:
> So set the ball rolling, here is a snippet from a program I found via 
> freshmeat the other day:
> 	configfile = malloc(strlen(getenv("HOME")) + 20);
> 	sprintf(configfile,"%s/%s",getenv("HOME"), cfgfile);

The malloc() might fail and return NULL.  You need to deal with that.  You
also should use snprintf() or do something else to deal with the fact that
you don't know the provenance of cfgfile.

Also, as Jason Rennie points out, HOME or cfgfile might be null.
configfile with a leading or trailing / probably won't work too well.

The duplicate calls to getenv() are ugly.
-- 
John Hasler
john@dhh.gt.org (John Hasler)
Dancing Horse Hill
Elmwood, WI



Reply to: