[PATCH] Fix GCC 8's -Wstringop-overflow on bfd/coff-rs6000.c

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Fix GCC 8's -Wstringop-overflow on bfd/coff-rs6000.c

Sergio Durigan Junior-2
GCC 8 will bring a new warning option which will detect possible
overflow and truncation on string manipulation functions.  For more
details, see:

  https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00471.html

While compiling BFD with it, I can see one place on bfd/coff-rs6000.c
where the warning is triggered.  This:

  (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);

will not include the trailing NUL on fhdr.magic, but that's fine
because it's a magic number.  The fix is trivial: just use memcpy
instead.

OK to push?

2018-02-05  Sergio Durigan Junior  <[hidden email]>

        * coff-rs6000.c (xcoff_write_archive_contents_old): Use
        'memcpy' instead of 'strncpy' when writing the magic number.
---
 bfd/coff-rs6000.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bfd/coff-rs6000.c b/bfd/coff-rs6000.c
index 2fc1feb1c5..d02835ede6 100644
--- a/bfd/coff-rs6000.c
+++ b/bfd/coff-rs6000.c
@@ -2090,7 +2090,7 @@ xcoff_write_archive_contents_old (bfd *abfd)
   char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
 
   memset (&fhdr, 0, sizeof fhdr);
-  (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
+  (void) memcpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
   sprintf (fhdr.freeoff, "%d", 0);
 
--
2.14.3

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix GCC 8's -Wstringop-overflow on bfd/coff-rs6000.c

Nick Clifton
Hi Sergio,

> 2018-02-05  Sergio Durigan Junior  <[hidden email]>
>
> * coff-rs6000.c (xcoff_write_archive_contents_old): Use
> 'memcpy' instead of 'strncpy' when writing the magic number.

Approved - please apply.

Cheers
  Nick


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix GCC 8's -Wstringop-overflow on bfd/coff-rs6000.c

Sergio Durigan Junior-2
On Tuesday, February 06 2018, Nick Clifton wrote:

> Hi Sergio,
>
>> 2018-02-05  Sergio Durigan Junior  <[hidden email]>
>>
>> * coff-rs6000.c (xcoff_write_archive_contents_old): Use
>> 'memcpy' instead of 'strncpy' when writing the magic number.
>
> Approved - please apply.

Thanks, pushed.

--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/