jcf-parse.c (set_source_filename): Improvement to Andrew's fix...
authorPer Bothner <per@bothner.com>
Sun, 28 Nov 2004 06:49:48 +0000 (22:49 -0800)
committerPer Bothner <bothner@gcc.gnu.org>
Sun, 28 Nov 2004 06:49:48 +0000 (22:49 -0800)
* 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.

From-SVN: r91411

gcc/java/ChangeLog
gcc/java/jcf-parse.c

index 0d65ea1814d1cd245e5a5153e0447ec2e722886c..c79a7584f62fc9533eaeadebeca0c23ff62ac838 100644 (file)
@@ -1,3 +1,10 @@
+2004-11-27  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.
+
 2004-11-27  Per Bothner  <per@bothner.com>
 
        * expr.c (build_expr_wfl): Only declare last_file and last_filenode
index 429e3db3a22c1abb426b832ee3dc671c7a9cfc4a..8171522fa79a4c06f2b237f6daaf3f8628a56acb 100644 (file)
@@ -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);
        }
     }