* [PATCH] migration: fix stringop-truncation warning
@ 2019-12-11 14:23 Paolo Bonzini
2019-12-12 0:55 ` Richard Henderson
0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2019-12-11 14:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-trivial, Marc-André Lureau
From: Marc-André Lureau <marcandre.lureau@redhat.com>
../migration/global_state.c: In function ‘global_state_store_running’:
../migration/global_state.c:47:5: error: ‘strncpy’ specified bound 100 equals destination size [-Werror=stringop-truncation]
47 | strncpy((char *)global_state.runstate,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 | state, sizeof(global_state.runstate));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assert() above allows to call with strlen(src)+1.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
migration/global_state.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/migration/global_state.c b/migration/global_state.c
index 2531147..a39a876 100644
--- a/migration/global_state.c
+++ b/migration/global_state.c
@@ -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);
}
bool global_state_received(void)
--
1.8.3.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] migration: fix stringop-truncation warning
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
0 siblings, 1 reply; 3+ messages in thread
From: Richard Henderson @ 2019-12-12 0:55 UTC (permalink / raw)
To: Paolo Bonzini, qemu-devel; +Cc: qemu-trivial, Marc-André Lureau
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.
r~
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] migration: fix stringop-truncation warning
2019-12-12 0:55 ` Richard Henderson
@ 2019-12-12 1:06 ` Paolo Bonzini
0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2019-12-12 1:06 UTC (permalink / raw)
To: Richard Henderson, qemu-devel; +Cc: qemu-trivial, Marc-André Lureau
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-12-12 1:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).