[Nagiosplug-devel] asprintf() madness (example patch)

Andreas Ericsson ae at op5.se
Sat Jun 4 00:44:04 CEST 2005


I'm hoping to start a discussion here rather than having this patch 
applied, so I haven't sent it to the tracker (although committing it is 
a good idea anyways).

In some of the plugins, strings are concatenated with asprintf() just to 
be printed by printf() in the next statement. This is made even more 
horrible when considering asprintf() never guarantees it will 
successfully allocate memory.

The perfdata and fperfdata also use sprintf, although can calculate the 
maximum length required to ensure boundary safety, so sprintf() (which 
is ansi C) would do just fine. The possibly wasted memory is a lot less 
than asprintf() uses for temporary storage.

asprintf(foo, "%s%s", foo, bar);
should be replaced by a concat2(str1, str2) function. If needed, 
concat3, concat4 and even concat_unlim could easily be written as well.

What the hell is the meaning behind all the asprintf()'s when a good 250 
or so of them can be replaced by simpler and more portable functions 
that can also exit properly when it runs out of memory?

Validation of memory allocating calls is another matter that could be 
easily and transparently solved (with use of wrapper functions and 
transparency macros).

-- 
Andreas Ericsson                   andreas.ericsson at op5.se
OP5 AB                             www.op5.se
Lead Developer




More information about the Devel mailing list