[Nagiosplug-devel] asprintf() madness (example patch)
Andreas Ericsson
ae at op5.se
Sat Jun 4 01:08:01 CEST 2005
And here be the patch. ;)
Andreas Ericsson wrote:
> 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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nagiosplug-check_swap-asprintf-madness.diff
URL: <https://www.monitoring-plugins.org/archive/devel/attachments/20050604/c68c2391/attachment.ksh>
More information about the Devel
mailing list