[PATCHv4] gdb: Replace memcpy with std::copy to avoid some g++ warnings on sparc

Message ID 20230124223130.982276-1-mark@klomp.org
State Superseded
Headers
Series [PATCHv4] gdb: Replace memcpy with std::copy to avoid some g++ warnings on sparc |

Commit Message

Mark Wielaard Jan. 24, 2023, 10:31 p.m. UTC
  For some reason g++ 12.2.1 on sparc produces a spurious warnings for
stringop-overread and restrict in fbsd-tdep.c for some memcpy calls.
Use std::copy to avoid those.

In function ‘void* memcpy(void*, const void*, size_t)’,
    inlined from ‘gdb::optional<std::vector<unsigned char, gdb::default_init_allocator<unsigned char, std::allocator<unsigned char> > > > fbsd_make_note_desc(target_object, uint32_t)’ at ../../binutils-gdb/gdb/fbsd-tdep.c:666:10:
/usr/include/bits/string_fortified.h:29:33: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ specified bound 18446744073709551612 exceeds maximum object size 9223372036854775807 [-Werror=stringop-overflow=]

In function ‘void* memcpy(void*, const void*, size_t)’,
    inlined from ‘gdb::optional<std::vector<unsigned char, gdb::default_init_allocator<unsigned char, std::allocator<unsigned char> > > > fbsd_make_note_desc(target_object, uint32_t)’ at ../../binutils-gdb/gdb/fbsd-tdep.c:673:10:
/usr/include/bits/string_fortified.h:29:33: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ accessing 18446744073709551612 bytes at offsets 0 and 0 overlaps 9223372036854775801 bytes at offset -9223372036854775805 [-Werror=restrict]

gdb/ChangeLog:

	* fbsd-tdep.c (fbsd_make_note_desc): Use std::copy instead
	of memcpy.
---

 V4: Fix std::copy argument typo
 V3: Drop diagnostic suppressions just use std::copy
 V2: Fix typos and add example errors to commit messages

 gdb/fbsd-tdep.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Tom Tromey Jan. 24, 2023, 11:15 p.m. UTC | #1
>>>>> "Mark" == Mark Wielaard <mark@klomp.org> writes:

Mark> -  memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
...
Mark> +  std::copy (buf->data (), buf->data () + sizeof (structsize),
Mark> +	     desc.data () + sizeof (structsize));

I think this should probably copy buf->size() bytes.
So maybe buf->begin() and buf->end() ?

Tom
  
Mark Wielaard Jan. 25, 2023, 8:49 a.m. UTC | #2
Hi Tom,

On Tue, Jan 24, 2023 at 04:15:32PM -0700, Tom Tromey wrote:
> >>>>> "Mark" == Mark Wielaard <mark@klomp.org> writes:
> 
> Mark> -  memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
> ...
> Mark> +  std::copy (buf->data (), buf->data () + sizeof (structsize),
> Mark> +	     desc.data () + sizeof (structsize));
> 
> I think this should probably copy buf->size() bytes.
> So maybe buf->begin() and buf->end() ?

OK, sounds good. Will post a v5.

Cheers,

Mark
  

Patch

diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index 203390d9880..4e4e837cf7c 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -662,8 +662,9 @@  fbsd_make_note_desc (enum target_object object, uint32_t structsize)
     return buf;
 
   gdb::byte_vector desc (sizeof (structsize) + buf->size ());
-  memcpy (desc.data (), &structsize, sizeof (structsize));
-  memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
+  std::copy (&structsize, &structsize + sizeof (structsize), desc.data ());
+  std::copy (buf->data (), buf->data () + sizeof (structsize),
+	     desc.data () + sizeof (structsize));
   return desc;
 }