John Hasler wrote:
The size of cfgfile is what bothered me as soon as I saw it.Presumably properly calculating the amount of storage to request takes care of that.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.
Leading / is fine, you haven't seen the code to deal with (or not) trailing slash or other stupid things clever users do.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.
but harmless.I agree that python and C++ are better languages for the task, but the program I found is written in C, not python or C++. However, the book does deal with C++ too. If you can write broken code in C you can do it in C++.
The main reason though for me commending now is to mention David A Wheeler's excellent site. Here is a document relevant to the topic at hand:
Secure Programming for Linux and Unix HOWTO http://www.dwheeler.com/secure-programs/ -- Cheers John -- spambait 1aaaaaaa@computerdatasafe.com.au Z1aaaaaaa@computerdatasafe.com.au Tourist pics http://portgeographe.environmentaldisasters.cds.merseine.nu/