public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* found redundant code in file natSystemProperties.cc
@ 2009-07-28  4:21 Martin Ettl
  2009-07-29 18:25 ` Andrew Haley
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Ettl @ 2009-07-28  4:21 UTC (permalink / raw)
  To: java

Hello friends,

i have checked the sources of gcc-4.4.1 with the static code analysis tool cppcheck. It brought up an issue in file gcc-4.4.1/libjava/gnu/classpath/natSystemProperties.cc at line 278. Lets take a look at the sourcecode:


// ...

  while (buffer != NULL)
    {
      if (getcwd (buffer, buflen) != NULL)
	{
	  SET ("user.dir", buffer);
	  break;
	}
      if (errno != ERANGE)
	break;
      buflen = 2 * buflen;
      char *orig_buf = buffer;
      buffer = (char *) realloc (buffer, buflen);
      if (buffer == NULL)
	free (orig_buf);
    }
278 if (buffer != NULL)
    free (buffer);
....

Here the line 278 is redundant, because it is safe to deallocate a NULL-Pointer! In other words, the if-statement can be removed.

Best regards

Ettl Martin


-- 
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: found redundant code in file natSystemProperties.cc
  2009-07-28  4:21 found redundant code in file natSystemProperties.cc Martin Ettl
@ 2009-07-29 18:25 ` Andrew Haley
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Haley @ 2009-07-29 18:25 UTC (permalink / raw)
  To: Martin Ettl; +Cc: java

Martin Ettl wrote:
> Hello friends,
> 
> i have checked the sources of gcc-4.4.1 with the static code analysis tool cppcheck. It brought up an issue in file gcc-4.4.1/libjava/gnu/classpath/natSystemProperties.cc at line 278. Lets take a look at the sourcecode:

> 278 if (buffer != NULL)
>     free (buffer);
> ....
> 
> Here the line 278 is redundant, because it is safe to deallocate a NULL-Pointer! In other words, the if-statement can be removed.

Well yes, but it saves a library call.

Harbison and Steele note "If the argument to free is a null pointer
then no action should occur, but this has been known to cause trouble
in some C implementations."  This is probably just ancient history,
though.

Andrew.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-07-29 18:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-28  4:21 found redundant code in file natSystemProperties.cc Martin Ettl
2009-07-29 18:25 ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).