[PATCH] TraceWriterQEMU.cc: Change strncpy to memcpy

Ryan Long ryan.long at oarcorp.com
Wed Sep 1 20:26:19 UTC 2021



-----Original Message-----
From: Gedare Bloom <gedare at rtems.org> 
Sent: Wednesday, September 1, 2021 3:21 PM
To: Ryan Long <ryan.long at oarcorp.com>
Cc: devel at rtems.org
Subject: Re: [PATCH] TraceWriterQEMU.cc: Change strncpy to memcpy

On Thu, Aug 19, 2021 at 7:42 AM Ryan Long <ryan.long at oarcorp.com> wrote:
>
> CID 1506207: Buffer not null terminated
>
> Closes #4491
> ---
>  tester/covoar/TraceWriterQEMU.cc | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tester/covoar/TraceWriterQEMU.cc 
> b/tester/covoar/TraceWriterQEMU.cc
> index c417745..be9b6e1 100644
> --- a/tester/covoar/TraceWriterQEMU.cc
> +++ b/tester/covoar/TraceWriterQEMU.cc
> @@ -106,7 +106,10 @@ namespace Trace {
>      //
>      //  Write the Header to the file
>      //
> -    strncpy( header.magic, QEMU_TRACE_MAGIC, sizeof(header.magic) );
> +    // The header.magic field is actually 12 bytes, but QEMU_TRACE_MAGIC is
> +    // 13 bytes including the NULL.
> +    const char qemu_trace_magic[13] = QEMU_TRACE_MAGIC;
> +    memcpy( header.magic, qemu_trace_magic, sizeof(header.magic) );

Just to clarify, the header.magic should not be NULL terminated?
[Ryan Long] Correct. When I tried to increase the size of header.magic, it wouldn't run correctly.

If so, then this is fine, but I don't think the temporary local variable is needed though.
memcpy( header.magic, QEMU_TRACE_MAGIC, sizeof(header.magic) ); should work fine.

>      header.version = QEMU_TRACE_VERSION;
>      header.kind    = QEMU_TRACE_KIND_RAW;  // XXX ??
>      header.sizeof_target_pc = 32;
> --
> 1.8.3.1
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list