diff mbox

[libiberty] : Add a couple of missing casts

Message ID CAFULd4a7ktYNf5D8qEAoScrFDidV520piF_YWd0Czf=YXUZrfg@mail.gmail.com
State New
Headers show

Commit Message

Uros Bizjak Nov. 12, 2013, 7:24 p.m. UTC
Hello!

This was uncovered by x86 lto-profiledbootstrap. The patch allows
lto-profiledbootstrap to proceed further.

2013-11-12  Uros Bizjak  <ubizjak@gmail.com>

    * cp-demangle.c (d_copy_templates): Cast result of malloc
    to (struct d_print_template *).
    (d_print_comp): Cast result of realloc to (struct d_saved scope *).

Tested on x86_64-pc-linux-gnu.

OK for mainline?

Uros.

Index: cp-demangle.c
===================================================================
--- cp-demangle.c	(revision 204705)
+++ cp-demangle.c	(working copy)
@@ -3968,7 +3968,7 @@ d_copy_templates (struct d_print_info *dpi)
   for (src = dpi->templates; src != NULL; src = src->next)
     {
       struct d_print_template *dst =
-	malloc (sizeof (struct d_print_template));
+	(struct d_print_template *) malloc (sizeof (struct d_print_template));
 
       if (dst == NULL)
 	{
@@ -4381,14 +4381,16 @@ d_print_comp (struct d_print_info *dpi, int option
 
 	    if (scope == NULL)
 	      {
+		size_t size;
+
 		/* This is the first time SUB has been traversed.
 		   We need to capture the current templates so
 		   they can be restored if SUB is reentered as a
 		   substitution.  */
 		++dpi->num_saved_scopes;
-		scopes = realloc (dpi->saved_scopes,
-				  sizeof (struct d_saved_scope)
-				  * dpi->num_saved_scopes);
+		size = sizeof (struct d_saved_scope) * dpi->num_saved_scopes;
+		scopes = (struct d_saved_scope *) realloc (dpi->saved_scopes,
+							   size);
 		if (scopes == NULL)
 		  {
 		    d_print_error (dpi);

Comments

Ian Lance Taylor Nov. 12, 2013, 7:55 p.m. UTC | #1
On Tue, Nov 12, 2013 at 11:24 AM, Uros Bizjak <ubizjak@gmail.com> wrote:
>
> This was uncovered by x86 lto-profiledbootstrap. The patch allows
> lto-profiledbootstrap to proceed further.
>
> 2013-11-12  Uros Bizjak  <ubizjak@gmail.com>
>
>     * cp-demangle.c (d_copy_templates): Cast result of malloc
>     to (struct d_print_template *).
>     (d_print_comp): Cast result of realloc to (struct d_saved scope *).
>
> Tested on x86_64-pc-linux-gnu.
>
> OK for mainline?

The patch is OK, but this code is troubling.  I obviously should have
looked at it earlier.  The C++ demangler is sometimes used in panic
situations, when malloc is not available.  The interface was designed
to be usable without requiring malloc, by passing in a sufficiently
large buffer.  I'm concerned that we apparently now require malloc to
work.

Ian
Richard Biener Nov. 13, 2013, 10:07 a.m. UTC | #2
On Tue, Nov 12, 2013 at 8:55 PM, Ian Lance Taylor <iant@google.com> wrote:
> On Tue, Nov 12, 2013 at 11:24 AM, Uros Bizjak <ubizjak@gmail.com> wrote:
>>
>> This was uncovered by x86 lto-profiledbootstrap. The patch allows
>> lto-profiledbootstrap to proceed further.
>>
>> 2013-11-12  Uros Bizjak  <ubizjak@gmail.com>
>>
>>     * cp-demangle.c (d_copy_templates): Cast result of malloc
>>     to (struct d_print_template *).
>>     (d_print_comp): Cast result of realloc to (struct d_saved scope *).
>>
>> Tested on x86_64-pc-linux-gnu.
>>
>> OK for mainline?
>
> The patch is OK, but this code is troubling.  I obviously should have
> looked at it earlier.  The C++ demangler is sometimes used in panic
> situations, when malloc is not available.  The interface was designed
> to be usable without requiring malloc, by passing in a sufficiently
> large buffer.  I'm concerned that we apparently now require malloc to
> work.

That indeed looks like an important regression - Gary, can you please
work to fix this?

Thanks,
Richard.

> Ian
Gary Benson Nov. 13, 2013, 3:30 p.m. UTC | #3
Richard Biener wrote:
> On Tue, Nov 12, 2013 at 8:55 PM, Ian Lance Taylor <iant@google.com> wrote:
> > On Tue, Nov 12, 2013 at 11:24 AM, Uros Bizjak <ubizjak@gmail.com> wrote:
> > >
> > > This was uncovered by x86 lto-profiledbootstrap. The patch allows
> > > lto-profiledbootstrap to proceed further.
> > >
> > > 2013-11-12  Uros Bizjak  <ubizjak@gmail.com>
> > >
> > >     * cp-demangle.c (d_copy_templates): Cast result of malloc
> > >     to (struct d_print_template *).
> > >     (d_print_comp): Cast result of realloc to (struct d_saved scope *).
> > >
> > > Tested on x86_64-pc-linux-gnu.
> > >
> > > OK for mainline?
> >
> > The patch is OK, but this code is troubling.  I obviously should
> > have looked at it earlier.  The C++ demangler is sometimes used in
> > panic situations, when malloc is not available.  The interface was
> > designed to be usable without requiring malloc, by passing in a
> > sufficiently large buffer.  I'm concerned that we apparently now
> > require malloc to work.
> 
> That indeed looks like an important regression - Gary, can you
> please work to fix this?

I'm on it.

Thanks,
Gary
Ian Lance Taylor Nov. 13, 2013, 3:50 p.m. UTC | #4
On Wed, Nov 13, 2013 at 7:30 AM, Gary Benson <gbenson@redhat.com> wrote:
> Richard Biener wrote:
>> On Tue, Nov 12, 2013 at 8:55 PM, Ian Lance Taylor <iant@google.com> wrote:
>> > On Tue, Nov 12, 2013 at 11:24 AM, Uros Bizjak <ubizjak@gmail.com> wrote:
>> > >
>> > > This was uncovered by x86 lto-profiledbootstrap. The patch allows
>> > > lto-profiledbootstrap to proceed further.
>> > >
>> > > 2013-11-12  Uros Bizjak  <ubizjak@gmail.com>
>> > >
>> > >     * cp-demangle.c (d_copy_templates): Cast result of malloc
>> > >     to (struct d_print_template *).
>> > >     (d_print_comp): Cast result of realloc to (struct d_saved scope *).
>> > >
>> > > Tested on x86_64-pc-linux-gnu.
>> > >
>> > > OK for mainline?
>> >
>> > The patch is OK, but this code is troubling.  I obviously should
>> > have looked at it earlier.  The C++ demangler is sometimes used in
>> > panic situations, when malloc is not available.  The interface was
>> > designed to be usable without requiring malloc, by passing in a
>> > sufficiently large buffer.  I'm concerned that we apparently now
>> > require malloc to work.
>>
>> That indeed looks like an important regression - Gary, can you
>> please work to fix this?
>
> I'm on it.

Thanks.  See also the cplus_demangle_print_callback function.

Ian
diff mbox

Patch

Index: cp-demangle.c
===================================================================
--- cp-demangle.c	(revision 204705)
+++ cp-demangle.c	(working copy)
@@ -3968,7 +3968,7 @@  d_copy_templates (struct d_print_info *dpi)
   for (src = dpi->templates; src != NULL; src = src->next)
     {
       struct d_print_template *dst =
-	malloc (sizeof (struct d_print_template));
+	(struct d_print_template *) malloc (sizeof (struct d_print_template));
 
       if (dst == NULL)
 	{
@@ -4381,14 +4381,16 @@  d_print_comp (struct d_print_info *dpi, int option
 
 	    if (scope == NULL)
 	      {
+		size_t size;
+
 		/* This is the first time SUB has been traversed.
 		   We need to capture the current templates so
 		   they can be restored if SUB is reentered as a
 		   substitution.  */
 		++dpi->num_saved_scopes;
-		scopes = realloc (dpi->saved_scopes,
-				  sizeof (struct d_saved_scope)
-				  * dpi->num_saved_scopes);
+		size = sizeof (struct d_saved_scope) * dpi->num_saved_scopes;
+		scopes = (struct d_saved_scope *) realloc (dpi->saved_scopes,
+							   size);
 		if (scopes == NULL)
 		  {
 		    d_print_error (dpi);