[Libreoffice] [PATCH] cppcheck "Common realloc mistake"

Caolán McNamara caolanm at redhat.com
Sat Oct 30 13:25:13 PDT 2010


On Sat, 2010-10-30 at 17:26 +0200, Gert Faller wrote:
> Hi,
> 
> The two cppcheck warnings in 'libs-core' about 'realloc'.
> Not sure about that : what to do on NULL ?

> There are others, for example in :
> 'sdk/odk/source/unoapploader/unx/unoapploader.c'
> but with 'exit(EXIT_FAILURE)' on NULL.

Hmm, well in C land this kind of thing would fit the bill and silence
cppcheck....

newthing = realloc(oldthing....)
if (newthing == NULL)
{
    free(oldthing)
    exit(EXIT_FAILURE);
}
oldthing = newthing;

So for the unoapploader.c if you cook up something like the above and
send it in I'll apply that given that its already exiting on failure and
its good to clear the cppcheck warnings in order to see when new ones
appear.

For the C++ code however, we don't have to grovel around with realloc,
like you say, what to do on NULL, and its non-trivial to deal with it.
So I suggest replacing them with std::vectors, e.g. in the xfld_attr.cxx
example you could try and change the "Attribute *mpList" to a
std::vector<Attribute> maList; and make the corresponding changes, e.g.
maList.resize(mnSize) instead of the mpList = realloc(mnSize * ...);
That would be the ideal solution in my mind.

C.



More information about the LibreOffice mailing list