[PATCH v1 8/8] migration: use pstrcpy to copy run state

Alex Bennée posted 8 patches 5 years, 2 months ago
Maintainers: Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>, Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aurelien Jarno <aurelien@aurel32.net>
[PATCH v1 8/8] migration: use pstrcpy to copy run state
Posted by Alex Bennée 5 years, 2 months ago
The gcov build triggered:

  ../../migration/global_state.c:47:5: error: ‘strncpy’ specified
      bound 100 equals destination size [-Werror=stringop-truncation]
      strncpy((char *)global_state.runstate

As we shouldn't be using strncpy anyway lets use the suggested
pstrcpy.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 migration/global_state.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/migration/global_state.c b/migration/global_state.c
index 25311479a4b..5fbe6d1ff07 100644
--- a/migration/global_state.c
+++ b/migration/global_state.c
@@ -44,8 +44,8 @@ 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));
+    pstrcpy((char *)global_state.runstate, sizeof(global_state.runstate),
+            state);
 }
 
 bool global_state_received(void)
-- 
2.20.1


Re: [PATCH v1 8/8] migration: use pstrcpy to copy run state
Posted by Paolo Bonzini 5 years, 2 months ago
Il gio 3 set 2020, 13:21 Alex Bennée <alex.bennee@linaro.org> ha scritto:

> The gcov build triggered:
>
>   ../../migration/global_state.c:47:5: error: ‘strncpy’ specified
>       bound 100 equals destination size [-Werror=stringop-truncation]
>       strncpy((char *)global_state.runstate
>
> As we shouldn't be using strncpy anyway lets use the suggested
> pstrcpy.
>

This is wrong, we want the all-zeros behavior of strncpy that pstrcpy lacks.

Paolo


>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  migration/global_state.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/migration/global_state.c b/migration/global_state.c
> index 25311479a4b..5fbe6d1ff07 100644
> --- a/migration/global_state.c
> +++ b/migration/global_state.c
> @@ -44,8 +44,8 @@ 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));
> +    pstrcpy((char *)global_state.runstate, sizeof(global_state.runstate),
> +            state);
>  }
>
>  bool global_state_received(void)
> --
> 2.20.1
>
>
Re: [PATCH v1 8/8] migration: use pstrcpy to copy run state
Posted by Philippe Mathieu-Daudé 5 years, 2 months ago
On 9/3/20 2:13 PM, Paolo Bonzini wrote:
> Il gio 3 set 2020, 13:21 Alex Bennée <alex.bennee@linaro.org
> <mailto:alex.bennee@linaro.org>> ha scritto:
> 
>     The gcov build triggered:
> 
>       ../../migration/global_state.c:47:5: error: ‘strncpy’ specified
>           bound 100 equals destination size [-Werror=stringop-truncation]
>           strncpy((char *)global_state.runstate
> 
>     As we shouldn't be using strncpy anyway lets use the suggested
>     pstrcpy.
> 
> 
> This is wrong, we want the all-zeros behavior of strncpy that pstrcpy lacks.

FWIW links to previous discussions:
https://www.mail-archive.com/qemu-devel@nongnu.org/msg584216.html

> 
> Paolo


Re: [PATCH v1 8/8] migration: use pstrcpy to copy run state
Posted by Alex Bennée 5 years, 2 months ago
Philippe Mathieu-Daudé <philmd@redhat.com> writes:

> On 9/3/20 2:13 PM, Paolo Bonzini wrote:
>> Il gio 3 set 2020, 13:21 Alex Bennée <alex.bennee@linaro.org
>> <mailto:alex.bennee@linaro.org>> ha scritto:
>> 
>>     The gcov build triggered:
>> 
>>       ../../migration/global_state.c:47:5: error: ‘strncpy’ specified
>>           bound 100 equals destination size [-Werror=stringop-truncation]
>>           strncpy((char *)global_state.runstate
>> 
>>     As we shouldn't be using strncpy anyway lets use the suggested
>>     pstrcpy.
>> 
>> 
>> This is wrong, we want the all-zeros behavior of strncpy that pstrcpy lacks.
>
> FWIW links to previous discussions:
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg584216.html

Hmm I wonder why gprof interfered with the assert. Either way I'll drop
the patch.

>
>> 
>> Paolo


-- 
Alex Bennée