make obstack object allocators more type-safe

Message ID 1402686633-7872-1-git-send-email-tromey@redhat.com
State Committed
Headers

Commit Message

Tom Tromey June 13, 2014, 7:10 p.m. UTC
  This changes OBSTACK_ZALLOC and OBSTACK_CALLOC to cast their value to
the correct type.  This is more type-safe and also is more in line
with the other object-allocation macros in libiberty.h.

Making this change revealed one trivial error in dwarf2read.c.
On the whole that seems pretty good to me.

Tested by rebuilding.

2014-06-13  Tom Tromey  <tromey@redhat.com>

	* dwarf2read.c (dw2_get_real_path): Use correct type in
	OBSTACK_CALLOC.
	* gdb_obstack.h (OBSTACK_ZALLOC, OBSTACK_CALLOC): Cast result.
---
 gdb/ChangeLog     | 6 ++++++
 gdb/dwarf2read.c  | 2 +-
 gdb/gdb_obstack.h | 6 +++---
 3 files changed, 10 insertions(+), 4 deletions(-)
  

Comments

Pedro Alves June 16, 2014, 2:15 p.m. UTC | #1
On 06/13/2014 08:10 PM, Tom Tromey wrote:
> This changes OBSTACK_ZALLOC and OBSTACK_CALLOC to cast their value to
> the correct type.  This is more type-safe and also is more in line
> with the other object-allocation macros in libiberty.h.
> 
> Making this change revealed one trivial error in dwarf2read.c.
> On the whole that seems pretty good to me.

Agreed.

> 
> Tested by rebuilding.
> 
> 2014-06-13  Tom Tromey  <tromey@redhat.com>
> 
> 	* dwarf2read.c (dw2_get_real_path): Use correct type in
> 	OBSTACK_CALLOC.
> 	* gdb_obstack.h (OBSTACK_ZALLOC, OBSTACK_CALLOC): Cast result.

Looks fine to me ...
  
Tom Tromey June 20, 2014, 2:43 p.m. UTC | #2
>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

Tom> 2014-06-13  Tom Tromey  <tromey@redhat.com>

Tom> 	* dwarf2read.c (dw2_get_real_path): Use correct type in
Tom> 	OBSTACK_CALLOC.
Tom> 	* gdb_obstack.h (OBSTACK_ZALLOC, OBSTACK_CALLOC): Cast result.

I'm checking this in now.

Tom
  

Patch

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 3e441dc..591eed7 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -3297,7 +3297,7 @@  dw2_get_real_path (struct objfile *objfile,
 {
   if (qfn->real_names == NULL)
     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
-				      qfn->num_file_names, char *);
+				      qfn->num_file_names, const char *);
 
   if (qfn->real_names[index] == NULL)
     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
diff --git a/gdb/gdb_obstack.h b/gdb/gdb_obstack.h
index 0b39400..ec2dd97 100644
--- a/gdb/gdb_obstack.h
+++ b/gdb/gdb_obstack.h
@@ -25,11 +25,11 @@ 
 /* Utility macros - wrap obstack alloc into something more robust.  */
 
 #define OBSTACK_ZALLOC(OBSTACK,TYPE) \
-  (memset (obstack_alloc ((OBSTACK), sizeof (TYPE)), 0, sizeof (TYPE)))
+  ((TYPE *) memset (obstack_alloc ((OBSTACK), sizeof (TYPE)), 0, sizeof (TYPE)))
 
 #define OBSTACK_CALLOC(OBSTACK,NUMBER,TYPE) \
-  (memset (obstack_alloc ((OBSTACK), (NUMBER) * sizeof (TYPE)), \
-	   0, (NUMBER) * sizeof (TYPE)))
+  ((TYPE *) memset (obstack_alloc ((OBSTACK), (NUMBER) * sizeof (TYPE)), \
+		    0, (NUMBER) * sizeof (TYPE)))
 
 /* Unless explicitly specified, GDB obstacks always use xmalloc() and
    xfree().  */