Skip to contentSkip to conversation

migration: fix stringop-truncation warning

PATCH In review Paolo Bonzini 2 participants 3 messages Dec 11, 2019 – Dec 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: fix stringop-truncation warning

From: Marc-André Lureau <[email protected]>

../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.

---
 migration/global_state.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/global_state.c +1 -2
index 2531147..a39a876 100644
@@ -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)
Signature · 1 line
1.8.3.1
#1 lore raw

migration/global_state.c void global_state_store_running(void) · line 47

Quoted 7 lines · Paolo Bonzini
migration/global_state.c +1 -2
@@ -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~

#2 lore raw
Quoted 11 lines · Richard Henderson

migration/global_state.c void global_state_store_running(void) · line 47

Quoted 7 lines · Paolo Bonzini
migration/global_state.c +1 -2
@@ -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