cpupower: remove stringop-truncation waring
PATCH 1/1 cpupower: remove stringop-truncation waring
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().
--- tools/power/cpupower/bench/parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/cpupower/bench/parse.c +1 -1
@@ -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;
Signature · 1 line
2.18.0
Shuah Khan
maintainerQuoted 26 lines · 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 overstrcpy(), and since 'ondemand' fits within 15 bytes" and it null terminates the string its safe to usestrcpy().Fixes 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")Signed-off-by Anders Roxell <[email protected]>--- tools/power/cpupower/bench/parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/power/cpupower/bench/parse.c +0
-0
tools/power/cpupower/bench/parse.c struct config *prepare_default_config() · line 148
Quoted 5 lines
tools/power/cpupower/bench/parse.c +1
-1@@ -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
PATCH v2 1/1 cpupower: remove stringop-truncation waring
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.
--- tools/power/cpupower/bench/parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/cpupower/bench/parse.c +1 -1
@@ -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;
Signature · 1 line
2.18.0
Shuah Khan
maintainerQuoted 25 lines · 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 <[email protected]>--- tools/power/cpupower/bench/parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/power/cpupower/bench/parse.c +0
-0
tools/power/cpupower/bench/parse.c struct config *prepare_default_config() · line 151
Quoted 8 lines
tools/power/cpupower/bench/parse.c +1
-1@@ -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