This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR 18091: Valgrind errors building libjava


Andrew Haley wrote:

A couple of lines seemed to be missing from Per's checkin.

Oops, indeed. I took a look and noticed that I didn't free 'buf'.


I also think there is a fencepost error in your patch: the variable
'i' in your patch is the length of the package prefix *including*
the last '.', while in the original and the attached patch it is
the length of the package prefix *excluding* the last '.'.  But the
loop to replace '.' is executed i+1 times either way.

Finally, a micro-optimization to remove the memcpy.

I haven't tested it, except to see that it compiles.
Could you look it over and try it out?
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/
2004-10-21  Per Bothner  <per@bothner.com>

	* jcf-parse.c (set_source_filename):  Improvement to Andrew's fix:
	Fix fencepost error in 'i', which got executed one too many times.
	Also, fold memcpy into explicit loop, as originally intended.
	Also, free temporary 'buf' which otherwise leaks.

Index: jcf-parse.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-parse.c,v
retrieving revision 1.177
diff -u -p -r1.177 jcf-parse.c
--- jcf-parse.c	21 Oct 2004 14:38:13 -0000	1.177
+++ jcf-parse.c	21 Oct 2004 18:04:01 -0000
@@ -151,18 +151,22 @@ set_source_filename (JCF *jcf, int index
       char *dot = strrchr (class_name, '.');
       if (dot != NULL)
 	{
-	  int i = dot - class_name + 1;
+	  /* Length of prefix, not counting final dot. */
+	  int i = dot - class_name;
 	  /* Concatenate current package prefix with new sfname. */
-	  char *buf = xmalloc (i+new_len+3);
-	  memcpy (buf, class_name, i);
-	  strcpy (buf + i, sfname);
-	  /* Replace '.' by DIR_SEPARATOR. */
+	  char *buf = xmalloc (i + new_len + 2); /* Space for '.' and '\0'. */
+	  strcpy (buf + i + 1, sfname);
+	  /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
+	     Note we start at the end with the final package dot. */
 	  for (; i >= 0;  i--)
 	    {
-	      if (buf[i] == '.')
-		buf[i] = DIR_SEPARATOR;
+	      char c = class_name[i];
+	      if (c == '.')
+		c = DIR_SEPARATOR;
+	      buf[i] = c;
 	    }
 	  sfname_id = get_identifier (buf);
+	  free (buf);
 	  sfname = IDENTIFIER_POINTER (sfname_id);
 	}
     }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]