public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] aout relocs
@ 2007-07-25 20:10 msnyder
  2007-07-25 20:37 ` Ian Lance Taylor
  0 siblings, 1 reply; 7+ messages in thread
From: msnyder @ 2007-07-25 20:10 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 400 bytes --]

Check me on this one, I'm a little uncertain.

As near as I can tell, if reloc_size is zero, the routine does
nothing useful.  Maybe it will never be zero, but if it is, a few
iffy things will happen:

 * we'll call malloc with a size of zero, which is ill defined,
and later free the result

 * we'll call bfd_bread with a size of zero, and

 * a potentially null pointer may slip thru the cracks.


[-- Attachment #2: reloc.txt --]
[-- Type: text/plain, Size: 1272 bytes --]

2007-07-25  Michael Snyder  <msnyder@access-company.com>

	* aoutx.h (NAME): Return TRUE if reloc_size is zero.

Index: aoutx.h
===================================================================
RCS file: /cvs/src/src/bfd/aoutx.h,v
retrieving revision 1.66
diff -p -r1.66 aoutx.h
*** aoutx.h	3 Jul 2007 14:26:39 -0000	1.66
--- aoutx.h	25 Jul 2007 18:59:09 -0000
*************** NAME (aout, slurp_reloc_table) (bfd *abf
*** 2280,2285 ****
--- 2280,2288 ----
        return FALSE;
      }
  
+   if (reloc_size == 0)
+     return TRUE;		/* Nothing to be done.  */
+ 
    if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
      return FALSE;
  
*************** NAME (aout, slurp_reloc_table) (bfd *abf
*** 2289,2299 ****
  
    amt = count * sizeof (arelent);
    reloc_cache = bfd_zmalloc (amt);
!   if (reloc_cache == NULL && count != 0)
      return FALSE;
  
    relocs = bfd_malloc (reloc_size);
!   if (relocs == NULL && reloc_size != 0)
      {
        free (reloc_cache);
        return FALSE;
--- 2292,2302 ----
  
    amt = count * sizeof (arelent);
    reloc_cache = bfd_zmalloc (amt);
!   if (reloc_cache == NULL)
      return FALSE;
  
    relocs = bfd_malloc (reloc_size);
!   if (relocs == NULL)
      {
        free (reloc_cache);
        return FALSE;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-25 20:10 [PATCH] aout relocs msnyder
@ 2007-07-25 20:37 ` Ian Lance Taylor
  2007-07-25 21:23   ` msnyder
  0 siblings, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2007-07-25 20:37 UTC (permalink / raw)
  To: msnyder; +Cc: binutils

msnyder@sonic.net writes:

> As near as I can tell, if reloc_size is zero, the routine does
> nothing useful.  Maybe it will never be zero, but if it is, a few
> iffy things will happen:
> 
>  * we'll call malloc with a size of zero, which is ill defined,
> and later free the result

No, we'll call bfd_malloc with a size of zero.  That is not
ill-defined.  It will either return NULL, or not, as (confusingly)
specified in the C standard.  Passing a NULL pointer to free will
always work.

>  * we'll call bfd_bread with a size of zero, and

Which is fine.

>  * a potentially null pointer may slip thru the cracks.

I'm not sure which pointer you mean here.

> 2007-07-25  Michael Snyder  <msnyder@access-company.com>
> 
> 	* aoutx.h (NAME): Return TRUE if reloc_size is zero.

I have no particular objection to this patch, but the ChangeLog entry
is wrong.  Emacs ^x 4 a will misfire in these functions; you have to
fill in the name manually.

I have to ask, though: why a.out?

Ian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-25 20:37 ` Ian Lance Taylor
@ 2007-07-25 21:23   ` msnyder
  2007-07-25 21:31     ` Andreas Schwab
  0 siblings, 1 reply; 7+ messages in thread
From: msnyder @ 2007-07-25 21:23 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: msnyder, binutils

> msnyder@sonic.net writes:
>
>> As near as I can tell, if reloc_size is zero, the routine does
>> nothing useful.  Maybe it will never be zero, but if it is, a few
>> iffy things will happen:
>>
>>  * we'll call malloc with a size of zero, which is ill defined,
>> and later free the result
>
> No, we'll call bfd_malloc with a size of zero.  That is not
> ill-defined.

With hat in hand, are you sure?  bfd_malloc does not check for
size == 0 before it calls malloc, and malloc(0) is "implementation
defined" (whatever that may mean).

> It will either return NULL, or not, as (confusingly)
> specified in the C standard.  Passing a NULL pointer to free will
> always work.
>
>>  * we'll call bfd_bread with a size of zero, and
>
> Which is fine.

Again, are you sure?  bfd_bread doesn't seem to check size either,
and it passes it to memcpy.  Is memcpy(x,y,0) well defined?

>>  * a potentially null pointer may slip thru the cracks.
>
> I'm not sure which pointer you mean here.

OK: we have
  relocs = bfd_malloc (reloc_size);  // which might be zero
  if (relocs == NULL && reloc_size != 0)
    bail;

So now it is possible that relocs == NULL and reloc_size == 0.
And then,

  if (bfd_bread (relocs, reloc_size, abfd)...

And bfd_bread does this:

  memcpy (ptr, bim->buffer + abfd->where, size);

where both ptr and size might be zero.

Note, sorry about the changelog, I'll take care of that.

> Why aout?

I know, I know...   I'm looking at a Coverity scan.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-25 21:23   ` msnyder
@ 2007-07-25 21:31     ` Andreas Schwab
  2007-07-25 21:51       ` msnyder
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2007-07-25 21:31 UTC (permalink / raw)
  To: msnyder; +Cc: Ian Lance Taylor, binutils

msnyder@sonic.net writes:

> With hat in hand, are you sure?  bfd_malloc does not check for
> size == 0 before it calls malloc, and malloc(0) is "implementation
> defined" (whatever that may mean).

There are only two ways malloc(0) may behave: either return NULL or a
unique pointer.

> Is memcpy(x,y,0) well defined?

Sure.

> And bfd_bread does this:
>
>   memcpy (ptr, bim->buffer + abfd->where, size);
>
> where both ptr and size might be zero.

That's the only place where something needs to be done (the pointer must
still be valid even if the size is zero).

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-25 21:31     ` Andreas Schwab
@ 2007-07-25 21:51       ` msnyder
  2007-07-26  9:37         ` Nick Clifton
  0 siblings, 1 reply; 7+ messages in thread
From: msnyder @ 2007-07-25 21:51 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: msnyder, Ian Lance Taylor, binutils


>> And bfd_bread does this:
>>
>>   memcpy (ptr, bim->buffer + abfd->where, size);
>>
>> where both ptr and size might be zero.
>
> That's the only place where something needs to be done (the pointer must
> still be valid even if the size is zero).

Alright -- that's enough to justify the patch, isn't it?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-25 21:51       ` msnyder
@ 2007-07-26  9:37         ` Nick Clifton
  2007-07-26 18:43           ` msnyder
  0 siblings, 1 reply; 7+ messages in thread
From: Nick Clifton @ 2007-07-26  9:37 UTC (permalink / raw)
  To: msnyder; +Cc: Andreas Schwab, Ian Lance Taylor, binutils

Hi Michael,

>> That's the only place where something needs to be done (the pointer must
>> still be valid even if the size is zero).
> 
> Alright -- that's enough to justify the patch, isn't it?

Certainly - please apply it.  Although you could simplify the second part of 
the patch by just checking to see if count is zero and returning, in the same 
way as you check reloc_size.  That way you do not need to check the return 
values of both allocs.

Cheers
   Nick


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] aout relocs
  2007-07-26  9:37         ` Nick Clifton
@ 2007-07-26 18:43           ` msnyder
  0 siblings, 0 replies; 7+ messages in thread
From: msnyder @ 2007-07-26 18:43 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 551 bytes --]

> Hi Michael,
>
>>> That's the only place where something needs to be done (the pointer
>>> must
>>> still be valid even if the size is zero).
>>
>> Alright -- that's enough to justify the patch, isn't it?
>
> Certainly - please apply it.  Although you could simplify the second part
> of
> the patch by just checking to see if count is zero and returning, in the
> same
> way as you check reloc_size.

Done.

> That way you do not need to check the return values of both allocs.

Well, yes I do -- bfd_malloc can still fail.
Checked in as attached.


[-- Attachment #2: slurp_reloc.txt --]
[-- Type: text/plain, Size: 1559 bytes --]

2007-07-25  Michael Snyder  <msnyder@access-company.com>

	* aoutx.h (slurp_reloc_table): Return TRUE if reloc_size == zero
	or count == zero.

Index: aoutx.h
===================================================================
RCS file: /cvs/src/src/bfd/aoutx.h,v
retrieving revision 1.66
diff -p -r1.66 aoutx.h
*** aoutx.h	3 Jul 2007 14:26:39 -0000	1.66
--- aoutx.h	26 Jul 2007 18:27:21 -0000
*************** NAME (aout, slurp_reloc_table) (bfd *abf
*** 2280,2299 ****
        return FALSE;
      }
  
    if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
      return FALSE;
  
    each_size = obj_reloc_entry_size (abfd);
  
    count = reloc_size / each_size;
  
    amt = count * sizeof (arelent);
    reloc_cache = bfd_zmalloc (amt);
!   if (reloc_cache == NULL && count != 0)
      return FALSE;
  
    relocs = bfd_malloc (reloc_size);
!   if (relocs == NULL && reloc_size != 0)
      {
        free (reloc_cache);
        return FALSE;
--- 2280,2304 ----
        return FALSE;
      }
  
+   if (reloc_size == 0)
+     return TRUE;		/* Nothing to be done.  */
+ 
    if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
      return FALSE;
  
    each_size = obj_reloc_entry_size (abfd);
  
    count = reloc_size / each_size;
+   if (count == 0)
+     return TRUE;		/* Nothing to be done.  */
  
    amt = count * sizeof (arelent);
    reloc_cache = bfd_zmalloc (amt);
!   if (reloc_cache == NULL)
      return FALSE;
  
    relocs = bfd_malloc (reloc_size);
!   if (relocs == NULL)
      {
        free (reloc_cache);
        return FALSE;

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-07-26 18:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-25 20:10 [PATCH] aout relocs msnyder
2007-07-25 20:37 ` Ian Lance Taylor
2007-07-25 21:23   ` msnyder
2007-07-25 21:31     ` Andreas Schwab
2007-07-25 21:51       ` msnyder
2007-07-26  9:37         ` Nick Clifton
2007-07-26 18:43           ` msnyder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).