[Libreoffice] [PUSHED, partially] Re: [PATCH] cppcheck "Common realloc mistake"
Gert Faller
gertfaller at aliceadsl.fr
Mon Nov 1 01:02:47 PDT 2010
Selon Caolán McNamara <caolanm at redhat.com>:
> 2. The other possible solution is to take advantage that in this case I
> can see that the string is bring shrunk, so (double-checking man
> realloc) on failure "the original block is left untouched". Right before
> the realloc a NULL char is entered at the point where the programmer
> wants to shrink the string to. So the string is already truncated, the
> programmer just want to try and free the now unused space. On fail of
> realloc pFullQualified is left alone and that truncated original string
> will already give the right results, albeit with a tiny extra memory
> overhead, so...
>
> char *pTmp;
>
> pFullQualifiedName[nLengthOfHostName] = '\0';
> pTmp = realloc(pFullQualifiedName, nLengthOfHostName + 1);
> if (pTmp)
> pFullQualifiedName = pTmp;
>
> Would be good too, seeing as we know we're attempting to free the unused
> part of the string that lies after the inserted NULL terminator.
>
> > And in "ure/sal/osl/w32/security.c"
> In this case I guess the only thing that can be done on realloc failure
> is to return from this with sal_False to indicate failure (after doing
> the same free of the original pre-realloc buffer. That'll cascade into a
> bit of a ugly and tiring free everything else that's malloced and
> un-freed just before that return. Its painful to handle these things
> completely in C, C++ is easier to manage them with std::vector or scoped
> pointers.
>
> C.
>
Hi,
I guessed that too.
I was just wondering if freeing some bytes was worth while.
A patch for this...
Regards.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: realloc_patch_3.patch
Type: application/octet-stream
Size: 1435 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20101101/b1cf3c9c/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: realloc_patch_4.patch
Type: application/octet-stream
Size: 1739 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20101101/b1cf3c9c/attachment-0001.obj>
More information about the LibreOffice
mailing list