]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Allocate sufficient space for string buffer
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 9 Jun 2012 13:22:00 +0000 (13:22 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Sat, 9 Jun 2012 13:22:00 +0000 (13:22 +0000)
* input-scrub.c (input_scrub_include_sb): Use sb_build to
allocate sufficient space for from_sb.  Use sb_terminate to
terminate string.
* read.c (read_a_source_file): Use sb_build to allocate
sufficient space and replace sb_add_string with sb_add_buffer.
(s_macro): Likewise.
(input_scrub_insert_line): Likewise.
(s_irp): Use sb_build to allocate sufficient space.
(do_repeat): Use sb_build to allocate sufficient space
for many.
* sb.c (sb_build): Remove static.
* sb.h (sb_build): New prototype.

gas/ChangeLog
gas/input-scrub.c
gas/read.c
gas/sb.c
gas/sb.h

index 00281a84596c0d06a503bc27d57796c7eea8cd17..62705c2630612bb5abe878df7fe99101c7c6fb90 100644 (file)
@@ -1,3 +1,18 @@
+2012-06-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * input-scrub.c (input_scrub_include_sb): Use sb_build to
+       allocate sufficient space for from_sb.  Use sb_terminate to
+       terminate string.
+       * read.c (read_a_source_file): Use sb_build to allocate
+       sufficient space and replace sb_add_string with sb_add_buffer.
+       (s_macro): Likewise.
+       (input_scrub_insert_line): Likewise.
+       (s_irp): Use sb_build to allocate sufficient space.
+       (do_repeat): Use sb_build to allocate sufficient space
+       for many.
+       * sb.c (sb_build): Remove static.
+       * sb.h (sb_build): New prototype.
+
 2012-06-09  Alan Modra  <amodra@gmail.com>
 
        * sb.c: Include limits.h.
index 46dd244a9814ed53de4ee7b34c4726f6f7791930..adae4d4dcd004b3ff0abaca61f80dcf247989547 100644 (file)
@@ -264,6 +264,8 @@ input_scrub_include_file (char *filename, char *position)
 void
 input_scrub_include_sb (sb *from, char *position, int is_expansion)
 {
+  int newline;
+
   if (macro_nest > max_macro_nest)
     as_fatal (_("macros nested too deeply"));
   ++macro_nest;
@@ -277,9 +279,11 @@ input_scrub_include_sb (sb *from, char *position, int is_expansion)
 
   next_saved_file = input_scrub_push (position);
 
-  sb_new (&from_sb);
+  /* Allocate sufficient space: from->len + optional newline.  */
+  newline = from->len >= 1 && from->ptr[0] != '\n';
+  sb_build (&from_sb, from->len + newline);
   from_sb_is_expansion = is_expansion;
-  if (from->len >= 1 && from->ptr[0] != '\n')
+  if (newline)
     {
       /* Add the sentinel required by read.c.  */
       sb_add_char (&from_sb, '\n');
@@ -288,8 +292,7 @@ input_scrub_include_sb (sb *from, char *position, int is_expansion)
 
   /* Make sure the parser looks at defined contents when it scans for
      e.g. end-of-line at the end of a macro.  */
-  sb_add_char (&from_sb, 0);
-  from_sb.len--;
+  sb_terminate (&from_sb);
 
   sb_index = 1;
 
index 2b37173c3e45a165373581c6fdeee8681ee7043b..21c42b27342fb8e2c687417bcdacc4c16e3905b7 100644 (file)
@@ -1181,7 +1181,6 @@ read_a_source_file (char *name)
              bump_line_counters ();
              s += 4;
 
-             sb_new (&sbuf);
              ends = strstr (s, "#NO_APP\n");
 
              if (!ends)
@@ -1262,7 +1261,9 @@ read_a_source_file (char *name)
                 actual macro expansion (possibly nested) and other
                 input expansion work.  Beware that in messages, line
                 numbers and possibly file names will be incorrect.  */
-             sb_add_string (&sbuf, new_buf);
+             new_length = strlen (new_buf);
+             sb_build (&sbuf, new_length);
+             sb_add_buffer (&sbuf, new_buf, new_length);
              input_scrub_include_sb (&sbuf, input_line_pointer, 0);
              sb_kill (&sbuf);
              buffer_limit = input_scrub_next_buffer (&input_line_pointer);
@@ -2437,8 +2438,8 @@ s_irp (int irpc)
 
   as_where (&file, &line);
 
-  sb_new (&s);
   eol = find_end_of_line (input_line_pointer, 0);
+  sb_build (&s, eol - input_line_pointer);
   sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
   input_line_pointer = eol;
 
@@ -2773,17 +2774,20 @@ s_macro (int ignore ATTRIBUTE_UNUSED)
 
   as_where (&file, &line);
 
-  sb_new (&s);
   eol = find_end_of_line (input_line_pointer, 0);
+  sb_build (&s, eol - input_line_pointer);
   sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
   input_line_pointer = eol;
 
   if (line_label != NULL)
     {
       sb label;
+      size_t len;
 
-      sb_new (&label);
-      sb_add_string (&label, S_GET_NAME (line_label));
+      name = S_GET_NAME (line_label);
+      len = strlen (name);
+      sb_build (&label, len);
+      sb_add_buffer (&label, name, len);
       err = define_macro (0, &s, &label, get_macro_line_sb, file, line, &name);
       sb_kill (&label);
     }
@@ -3207,7 +3211,7 @@ do_repeat (int count, const char *start, const char *end)
       return;
     }
 
-  sb_new (&many);
+  sb_build (&many, count * one.len);
   while (count-- > 0)
     sb_add_sb (&many, &one);
 
@@ -3247,7 +3251,7 @@ do_repeat_with_expander (int count,
          char * sub;
          sb processed;
 
-         sb_new (& processed);
+         sb_build (& processed, one.len);
          sb_add_sb (& processed, & one);
          sub = strstr (processed.ptr, expander);
          len = sprintf (sub, "%d", count);
@@ -6167,8 +6171,9 @@ void
 input_scrub_insert_line (const char *line)
 {
   sb newline;
-  sb_new (&newline);
-  sb_add_string (&newline, line);
+  size_t len = strlen (line);
+  sb_build (&newline, len);
+  sb_add_buffer (&newline, line, len);
   input_scrub_include_sb (&newline, input_line_pointer, 0);
   sb_kill (&newline);
   buffer_limit = input_scrub_next_buffer (&input_line_pointer);
index 73fd10c6c50b50c1434afd0edd47c866b2a90eab..f68402fc470f6af031241d32f62d7388e1af6246 100644 (file)
--- a/gas/sb.c
+++ b/gas/sb.c
@@ -57,7 +57,7 @@ static void sb_check (sb *, size_t);
 
 /* Initializes an sb.  */
 
-static void
+void
 sb_build (sb *ptr, size_t size)
 {
   ptr->ptr = xmalloc (size + 1);
index cc11ef658bbdc30bdc59af8786d5089f7a4c0eea..ea010ee34eb02980c4bfd239322319994c0a540a 100644 (file)
--- a/gas/sb.h
+++ b/gas/sb.h
@@ -53,6 +53,7 @@ typedef struct sb
 sb;
 
 extern void sb_new (sb *);
+extern void sb_build (sb *, size_t);
 extern void sb_kill (sb *);
 extern void sb_add_sb (sb *, sb *);
 extern void sb_scrub_and_add_sb (sb *, sb *);