lib/power: fix governor storage to trim newlines
PATCH 1/1 lib/power: fix governor storage to trim newlines
Currently the Power Libray stores the governor name with an embedded newline read from the scaling_governor sysfs file. This patch strips it out.
--- lib/librte_power/power_acpi_cpufreq.c | 4 ++++ lib/librte_power/power_pstate_cpufreq.c | 4 ++++ 2 files changed, 8 insertions(+)
lib/librte_power/power_acpi_cpufreq.c +4 -0
@@ -147,6 +147,10 @@ power_set_governor_userspace(struct rte_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); + buf[BUFSIZ -1 ] ='\0' ; +if (strlen (buf)) +/* Strip off terminating '\n' */ +strtok (buf,"\n" );/* Check if current governor is userspace */ if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
lib/librte_power/power_pstate_cpufreq.c +4 -0
@@ -292,6 +292,10 @@ power_set_governor_performance(struct pstate_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); + buf[BUFSIZ -1 ] ='\0' ; +if (strlen (buf)) +/* Strip off terminating '\n' */ +strtok (buf,"\n" );/* Check if current governor is performance */ if (strncmp(buf, POWER_GOVERNOR_PERF,
Signature · 1 line
2.17.1
Quoted 18 lines · David Hunt
Currently the Power Libray stores the governor name with an embedded newline read from the
scaling_governorsysfs file. This patch strips it out.Fixes 445c6528b55f ("power: common interface for guest and host")Signed-off-by David Hunt <[email protected]>--- lib/librte_power/power_acpi_cpufreq.c | 4 ++++ lib/librte_power/power_pstate_cpufreq.c | 4 ++++ 2 files changed, 8 insertions(+)lib/librte_power/power_acpi_cpufreq.c +0
-0
lib/librte_power/power_acpi_cpufreq.c power_set_governor_userspace(struct rte_power_info *pi) · line 150
Quoted 4 lines
lib/librte_power/power_acpi_cpufreq.c +1
-0@@ -147,6 +147,10 @@ power_set_governor_userspace(struct rte_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); + buf[BUFSIZ -1 ] ='\0' ;
Should not be needed as fgets null-terminates.
" fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer."
/Bruce
Quoted 18 lines · David Hunt
Currently the Power Libray stores the governor name with an embedded newline read from the
scaling_governorsysfs file. This patch strips it out.Fixes 445c6528b55f ("power: common interface for guest and host")Signed-off-by David Hunt <[email protected]>--- lib/librte_power/power_acpi_cpufreq.c | 4 ++++ lib/librte_power/power_pstate_cpufreq.c | 4 ++++ 2 files changed, 8 insertions(+)lib/librte_power/power_acpi_cpufreq.c +0
-0
lib/librte_power/power_acpi_cpufreq.c power_set_governor_userspace(struct rte_power_info *pi) · line 153
Quoted 7 lines
lib/librte_power/power_acpi_cpufreq.c +4
-0@@ -147,6 +147,10 @@ power_set_governor_userspace(struct rte_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); + buf[BUFSIZ -1 ] ='\0' ; +if (strlen (buf)) +/* Strip off terminating '\n' */ +strtok (buf,"\n" );
I have a feeling that either strlen or strtok here is unnecessary.
If it's always terminating - you can just use strlen return value and overwrite the '\n' without going over the string the second time - you know where the string ends!
You have already written null-terminator to the end of the buffer, so it can't overflow on strtok, so you don't really need strlen either, because the string will either:
1) be empty (in which case strtok does nothing)
2) contain text + newline (in which case you cut off the newline and leave the text - no need for strlen), or
3) contain just a newline (which would make it empty after strtok)
Did you mean to only cut off the newline off the strings that have stuff other than newline? That would be the only case where using strlen would make sense - in which case, not only the check is wrong, but you could also replace it with a simple 'if (buf[0] != '\n')' check instead of strlen.
Signature · 2 lines
Thanks, Anatoly
Hi Anatoly,
Quoted 31 lines · Anatoly
Quoted 18 lines · David Hunt
Currently the Power Libray stores the governor name with an embedded newline read from the
scaling_governorsysfs file. This patch strips it out.Fixes 445c6528b55f ("power: common interface for guest and host")Signed-off-by David Hunt <[email protected]>---
lib/librte_power/power_acpi_cpufreq.c | 4 ++++lib/librte_power/power_pstate_cpufreq.c| 4 ++++2 files changed, 8 insertions(+)lib/librte_power/power_acpi_cpufreq.c +0
-0lib/librte_power/power_acpi_cpufreq.c power_set_governor_userspace(struct · line 153
Quoted 7 lines
lib/librte_power/power_acpi_cpufreq.c +4
-0@@ -147,6 +147,10 @@ power_set_governor_userspace(struct rte_power_info *pi) s = fgets(buf, sizeof(buf), f); FOPS_OR_NULL_GOTO(s, out); + buf[BUFSIZ-1] = '\0'; + if (strlen(buf)) + /* Strip off terminating '\n' */ + strtok(buf, "\n");I have a feeling that either strlen or strtok here is unnecessary.
If it's always terminating - you can just use strlen return value and overwrite the '\n' without going over the string the second time - you know where the string ends!
Quoted 8 lines
You have already written null-terminator to the end of the buffer, so it can't overflow on strtok, so you don't really need strlen either, because the string will either:
1) be empty (in which case strtok does nothing)
2) contain text + newline (in which case you cut off the newline and leave the text - no need for strlen), or
3) contain just a newline (which would make it empty after strtok)Did you mean to only cut off the newline off the strings that have stuff other than newline? That would be the only case where using strlen would make sense - in which case, not only the check is wrong, but you could also replace it with a simple 'if (buf[0] != '\n')' check instead of strlen.
So just the strtok() then, without anything else (apart from the comment) . Sure! :)
Thanks,
Dave.
PATCH v2 1/1 lib/power: fix governor storage to trim newlines
In reply to David HuntCurrently the Power Libray stores the governor name with an embedded newline read from the scaling_governor sysfs file. This patch strips it out.
--- lib/librte_power/power_acpi_cpufreq.c | 2 ++ lib/librte_power/power_pstate_cpufreq.c | 2 ++ 2 files changed, 4 insertions(+)
lib/librte_power/power_acpi_cpufreq.c +2 -0
@@ -147,6 +147,8 @@ power_set_governor_userspace(struct rte_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); +/* Strip off terminating '\n' */ +strtok (buf,"\n" );/* Check if current governor is userspace */ if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
lib/librte_power/power_pstate_cpufreq.c +2 -0
@@ -292,6 +292,8 @@ power_set_governor_performance(struct pstate_power_info *pi) s =fgets (buf,sizeof (buf), f);FOPS_OR_NULL_GOTO (s, out); +/* Strip off terminating '\n' */ +strtok (buf,"\n" );/* Check if current governor is performance */ if (strncmp(buf, POWER_GOVERNOR_PERF,
Signature · 1 line
2.17.1
Quoted 9 lines · David Hunt
Currently the Power Libray stores the governor name with an embedded newline read from the
scaling_governorsysfs file. This patch strips it out.Fixes 445c6528b55f ("power: common interface for guest and host")Signed-off-by David Hunt <[email protected]>---
Signature · 2 lines
Thanks, Anatoly
Thomas Monjalon
Applied29/03/2019 18:56, Burakov, Anatoly:
Quoted 11 lines · David Hunt
Quoted 8 lines
Currently the Power Libray stores the governor name with an embedded newline read from the
scaling_governorsysfs file. This patch strips it out.Fixes 445c6528b55f ("power: common interface for guest and host")Signed-off-by David Hunt <[email protected]>Acked-by Anatoly Burakov <[email protected]>
Applied, thanks