From: Paolo Bonzini <pbonzini@redhat.com>
To: Richard Henderson <richard.henderson@linaro.org>, qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org,
"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: Re: [PATCH] migration: fix stringop-truncation warning
Date: Thu, 12 Dec 2019 02:06:53 +0100 [thread overview]
Message-ID: <d3de9def-b922-7469-8534-a448bf3384cc@redhat.com> (raw)
In-Reply-To: <ce08ed58-78f0-83c8-404c-4caa76d38569@linaro.org>
On 12/12/19 01:55, Richard Henderson wrote:
> On 12/11/19 6:23 AM, Paolo Bonzini wrote:
>> @@ -44,8 +44,7 @@ void global_state_store_running(void)
>> {
>> const char *state = RunState_str(RUN_STATE_RUNNING);
>> assert(strlen(state) < sizeof(global_state.runstate));
>> - strncpy((char *)global_state.runstate,
>> - state, sizeof(global_state.runstate));
>> + memcpy(global_state.runstate, state, strlen(state) + 1);
>
> We should assign the strlen result to a local variable rather than compute it
> twice.
We could even strcpy since the assertion ensures it's valid, but perhaps
it's better to do nothing, since the best alternative would be
memset+strcpy, i.e. back to strncpy.
We generally are quite mindful about our uses of strncpy, but maybe we
could fold the assertion and strncpy into a qemu_strncpy function,
and/or qemu_strncpy_nonul for when you're copying into an array that
does _not_ need to be nul-terminated (so the assertion can become <=
rather than <). I'll add it to BiteSizedTasks.
Paolo
prev parent reply other threads:[~2019-12-12 1:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-11 14:23 [PATCH] migration: fix stringop-truncation warning Paolo Bonzini
2019-12-12 0:55 ` Richard Henderson
2019-12-12 1:06 ` Paolo Bonzini [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d3de9def-b922-7469-8534-a448bf3384cc@redhat.com \
--to=pbonzini@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 NNTP newsgroup(s).