All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpupower: remove stringop-truncation waring
@ 2018-08-21 12:02 Anders Roxell
  2018-08-27 17:46 ` Shuah Khan
  0 siblings, 1 reply; 4+ messages in thread
From: Anders Roxell @ 2018-08-21 12:02 UTC (permalink / raw)
  To: trenn, shuah; +Cc: linux-pm, linux-kernel, Anders Roxell

The strncpy doesn't null terminate the string because the size is too
short by one byte.

parse.c: In function ‘prepare_default_config’:
parse.c:148:2: warning: ‘strncpy’ output truncated before terminating
    nul copying 8 bytes from a string of the same length
    [-Wstringop-truncation]
  strncpy(config->governor, "ondemand", 8);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Passing the length of the input argument to strncpy() is almost always
wrong and provides no extra benefit over strcpy(), and since 'ondemand'
fits within 15 bytes" and it null terminates the string its safe to use
strcpy().

Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 tools/power/cpupower/bench/parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
index 9ba8a44ad2a7..91b5b768ffd2 100644
--- a/tools/power/cpupower/bench/parse.c
+++ b/tools/power/cpupower/bench/parse.c
@@ -145,7 +145,7 @@ struct config *prepare_default_config()
 	config->cpu = 0;
 	config->prio = SCHED_HIGH;
 	config->verbose = 0;
-	strncpy(config->governor, "ondemand", 8);
+	strcpy(config->governor, "ondemand");
 
 	config->output = stdout;
 
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpupower: remove stringop-truncation waring
  2018-08-21 12:02 [PATCH] cpupower: remove stringop-truncation waring Anders Roxell
@ 2018-08-27 17:46 ` Shuah Khan
  2018-08-28  9:31   ` [PATCH v2] " Anders Roxell
  0 siblings, 1 reply; 4+ messages in thread
From: Shuah Khan @ 2018-08-27 17:46 UTC (permalink / raw)
  To: Anders Roxell, trenn; +Cc: linux-pm, linux-kernel, Shuah Khan

On 08/21/2018 06:02 AM, Anders Roxell wrote:
> The strncpy doesn't null terminate the string because the size is too
> short by one byte.
> 
> parse.c: In function ‘prepare_default_config’:
> parse.c:148:2: warning: ‘strncpy’ output truncated before terminating
>     nul copying 8 bytes from a string of the same length
>     [-Wstringop-truncation]
>   strncpy(config->governor, "ondemand", 8);
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Passing the length of the input argument to strncpy() is almost always
> wrong and provides no extra benefit over strcpy(), and since 'ondemand'
> fits within 15 bytes" and it null terminates the string its safe to use
> strcpy().
> 
> Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
> ---
>  tools/power/cpupower/bench/parse.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
> index 9ba8a44ad2a7..91b5b768ffd2 100644
> --- a/tools/power/cpupower/bench/parse.c
> +++ b/tools/power/cpupower/bench/parse.c
> @@ -145,7 +145,7 @@ struct config *prepare_default_config()
>  	config->cpu = 0;
>  	config->prio = SCHED_HIGH;
>  	config->verbose = 0;
> -	strncpy(config->governor, "ondemand", 8);
> +	strcpy(config->governor, "ondemand");
This change is fine, however continuing to use strncpy with sizeof(config->governor).

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] cpupower: remove stringop-truncation waring
  2018-08-27 17:46 ` Shuah Khan
@ 2018-08-28  9:31   ` Anders Roxell
  2018-08-28 21:48     ` Shuah Khan
  0 siblings, 1 reply; 4+ messages in thread
From: Anders Roxell @ 2018-08-28  9:31 UTC (permalink / raw)
  To: trenn, shuah; +Cc: linux-pm, linux-kernel, Anders Roxell

The strncpy doesn't null terminate the string because the size is too
short by one byte.

parse.c: In function ‘prepare_default_config’:
parse.c:148:2: warning: ‘strncpy’ output truncated before terminating
    nul copying 8 bytes from a string of the same length
    [-Wstringop-truncation]
  strncpy(config->governor, "ondemand", 8);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The normal method of passing the length of the destination buffer works
correctly here.

Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 tools/power/cpupower/bench/parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
index 9ba8a44ad2a7..84caee38418f 100644
--- a/tools/power/cpupower/bench/parse.c
+++ b/tools/power/cpupower/bench/parse.c
@@ -145,7 +145,7 @@ struct config *prepare_default_config()
 	config->cpu = 0;
 	config->prio = SCHED_HIGH;
 	config->verbose = 0;
-	strncpy(config->governor, "ondemand", 8);
+	strncpy(config->governor, "ondemand", sizeof(config->governor));
 
 	config->output = stdout;
 
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] cpupower: remove stringop-truncation waring
  2018-08-28  9:31   ` [PATCH v2] " Anders Roxell
@ 2018-08-28 21:48     ` Shuah Khan
  0 siblings, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2018-08-28 21:48 UTC (permalink / raw)
  To: Anders Roxell, trenn; +Cc: linux-pm, linux-kernel, Shuah Khan

On 08/28/2018 03:31 AM, Anders Roxell wrote:
> The strncpy doesn't null terminate the string because the size is too
> short by one byte.
> 
> parse.c: In function ‘prepare_default_config’:
> parse.c:148:2: warning: ‘strncpy’ output truncated before terminating
>     nul copying 8 bytes from a string of the same length
>     [-Wstringop-truncation]
>   strncpy(config->governor, "ondemand", 8);
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> The normal method of passing the length of the destination buffer works
> correctly here.
> 
> Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
> ---
>  tools/power/cpupower/bench/parse.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
> index 9ba8a44ad2a7..84caee38418f 100644
> --- a/tools/power/cpupower/bench/parse.c
> +++ b/tools/power/cpupower/bench/parse.c
> @@ -145,7 +145,7 @@ struct config *prepare_default_config()
>  	config->cpu = 0;
>  	config->prio = SCHED_HIGH;
>  	config->verbose = 0;
> -	strncpy(config->governor, "ondemand", 8);
> +	strncpy(config->governor, "ondemand", sizeof(config->governor));
>  
>  	config->output = stdout;
>  
> 

Thanks. Applied to cpupower git for 4.19-rc3

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-08-28 21:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-21 12:02 [PATCH] cpupower: remove stringop-truncation waring Anders Roxell
2018-08-27 17:46 ` Shuah Khan
2018-08-28  9:31   ` [PATCH v2] " Anders Roxell
2018-08-28 21:48     ` Shuah Khan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.