Re: Patch for cppcheck error "Common realloc mistake"



Hi Julien

On Thu, Oct 27, 2011 at 05:48:23PM +0200, Julien wrote:
> Hello,
> 
> I had sent a simple proposed patch 21/08 (http://old.nabble.com/Patch-for-cppcheck-error-%22Common-realloc-mistake%22-to32305733.html)

[snip]
>                  if (address_string_len < len + 3) {
>                          address_string_len = len + 3;
> -                        address_string = (char *) g_try_realloc
> (address_string, address_string_len);
> +                        address_string = (char *) g_realloc
> (address_string, address_string_len);
>                  }
> 
>                  memcpy (address_string, "0x", 2);

If address_string is NULL as returned by g_try_realloc(), the following
memcpy() would cause the process to segfault, perhaps creating a core.

With your patch, it abort()s _if_ the underlying realloc() returns
NULL.  But you better read Documentation/vm/overcommit-accounting in a
Linux kernel tree.  Similar implementations exist in other demand
paging kernels too.

There have also been many discussions on whether to check the return
value of a NULL-returning malloc() at all.  See this as an example:
http://news.ycombinator.com/item?id=3112309

		Mukund


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]