[RFA,7/9] Use std::vector in find_source_lines

Message ID 20180207220434.6045-8-tom@tromey.com
State New
Headers show
Series
  • more cleanup removal
Related show

Commit Message

Tom Tromey Feb. 7, 2018, 10:04 p.m.
This replaces an explicit malloc and a cleanup with a std::vector.

2018-02-07  Tom Tromey  <tom@tromey.com>

	* source.c (find_source_lines): Use std::vector.
---
 gdb/ChangeLog |  4 ++++
 gdb/source.c  | 20 ++++++++------------
 2 files changed, 12 insertions(+), 12 deletions(-)

-- 
2.13.6

Comments

Tom Tromey Feb. 9, 2018, 1 p.m. | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:


Tom> +    end = &data[size];

Andre pointed out that this is possibly fishy, so I think perhaps the
appended should go in.  Ok?

Tom

commit 4e40d3c76f0e14aea171c9ef7447c78defee8a74
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Feb 9 05:58:46 2018 -0700

    Don't reference past the end of the vector
    
    An earlier change made find_source_lines read:
    
        end = &data[size];
    
    However, since 'size' is the size of the vector, this seems fishy.
    More obviously ok is to compute the end of the data directly:
    
        end = data.data () + size;
    
    2018-02-09  Tom Tromey  <tom@tromey.com>
    
            * source.c (find_source_lines): Don't reference past the end of
            the vector.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3e5b324472..d07da95027 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2018-02-09  Tom Tromey  <tom@tromey.com>
+
+	* source.c (find_source_lines): Don't reference past the end of
+	the vector.
+
 2018-02-09  Joel Brobecker  <brobecker@adacore.com>
 
 	* NEWS <Changes in GDB 8.1>: Clarify that "rbreak" is a new
diff --git a/gdb/source.c b/gdb/source.c
index 9eec58febd..009bec5285 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1219,7 +1219,7 @@ find_source_lines (struct symtab *s, int desc)
     size = myread (desc, data.data (), size);
     if (size < 0)
       perror_with_name (symtab_to_filename_for_display (s));
-    end = &data[size];
+    end = data.data () + size;
     p = &data[0];
     line_charpos[0] = 0;
     nlines = 1;
Pedro Alves Feb. 9, 2018, 1:10 p.m. | #2
On 02/09/2018 01:00 PM, Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

> 

> Tom> +    end = &data[size];

> 

> Andre pointed out that this is possibly fishy, so I think perhaps the

> appended should go in.  Ok?


OK.

Thanks,
Pedro Alves

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f0ec664d23..8bd04e47c7 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@ 
 2018-02-07  Tom Tromey  <tom@tromey.com>
 
+	* source.c (find_source_lines): Use std::vector.
+
+2018-02-07  Tom Tromey  <tom@tromey.com>
+
 	* macrocmd.c (struct temporary_macro_definition): New.
 	(macro_define_command): Use temporary_macro_definition.  Remove
 	cleanups.
diff --git a/gdb/source.c b/gdb/source.c
index 1f136f4cfb..0e871be8ca 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1186,7 +1186,7 @@  void
 find_source_lines (struct symtab *s, int desc)
 {
   struct stat st;
-  char *data, *p, *end;
+  char *p, *end;
   int nlines = 0;
   int lines_allocated = 1000;
   int *line_charpos;
@@ -1207,23 +1207,20 @@  find_source_lines (struct symtab *s, int desc)
     warning (_("Source file is more recent than executable."));
 
   {
-    struct cleanup *old_cleanups;
-
     /* st_size might be a large type, but we only support source files whose 
        size fits in an int.  */
     size = (int) st.st_size;
 
-    /* Use malloc, not alloca, because this may be pretty large, and we may
-       run into various kinds of limits on stack size.  */
-    data = (char *) xmalloc (size);
-    old_cleanups = make_cleanup (xfree, data);
+    /* Use the heap, not the stack, because this may be pretty large,
+       and we may run into various kinds of limits on stack size.  */
+    std::vector<char> data (size);
 
     /* Reassign `size' to result of read for systems where \r\n -> \n.  */
-    size = myread (desc, data, size);
+    size = myread (desc, data.data (), size);
     if (size < 0)
       perror_with_name (symtab_to_filename_for_display (s));
-    end = data + size;
-    p = data;
+    end = &data[size];
+    p = &data[0];
     line_charpos[0] = 0;
     nlines = 1;
     while (p != end)
@@ -1239,10 +1236,9 @@  find_source_lines (struct symtab *s, int desc)
 		  (int *) xrealloc ((char *) line_charpos,
 				    sizeof (int) * lines_allocated);
 	      }
-	    line_charpos[nlines++] = p - data;
+	    line_charpos[nlines++] = p - data.data ();
 	  }
       }
-    do_cleanups (old_cleanups);
   }
 
   s->nlines = nlines;