fix GCC7 build by refactoring trimming routines
PATCH 1/1 fix GCC7 build by refactoring trimming routines
I got a compile error with GCC7. When trimming white spaces from strings lsmmc uses strncpy with overlapping memory areas. This is not allowed. In addition, the implementation was not efficient with calling strlen and strncpy once per iteration. Refactor the code to be valid and more effective.
--- lsmmc.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
lsmmc.c +11 -5
@@ -316,8 +316,9 @@ int parse_ids(struct config *config)/* MMC/SD file parsing functions */ char *read_file (char *name) { -char *preparsed;char line[4096 ]; +char *preparsed, *start = line; +int len;FILE *f; f =fopen (name,"r" );
@@ -348,12 +349,17 @@ char *read_file(char *name) } line[sizeof (line) -1 ] ='\0' ; + len =strlen (line); -while (isspace (line[strlen (line) -1 ])) - line[strlen (line) -1 ] ='\0' ; +while (len >0 &&isspace (line[len -1 ])) + len--; -while (isspace (line[0 ])) -strncpy (&line[0 ], &line[1 ],sizeof (line)); +while (len >0 &&isspace (*start)) { + start++; + len--; + } +memmove (line, start, len); + line[len] ='\0' ;return strdup (line); }
Signature · 1 line
2.11.0
Hi,
Quoted 17 lines
I got a compile error with GCC7. When trimming white spaces from strings lsmmc uses strncpy with overlapping memory areas. This is not allowed. In addition, the implementation was not efficient with calling strlen and strncpy once per iteration. Refactor the code to be valid and more effective.
Signed-off-by Wolfram Sang <[email protected]>--- lsmmc.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)lsmmc.c +0
-0
lsmmc.c int parse_ids(struct config *config) · line 318
Quoted 3 lines
lsmmc.c +0
-0@@ -316,8 +316,9 @@ int parse_ids(struct config *config)/* MMC/SD file parsing functions */ char *read_file (char *name) {
See Uwe's note - https://www.spinics.net/lists/linux-mmc/msg51882.html
Cheers,
Avri
Hi Avri, hi Chris,
Quoted 11 lines
Quoted 11 lines
I got a compile error with GCC7. When trimming white spaces from strings lsmmc uses strncpy with overlapping memory areas. This is not allowed. In addition, the implementation was not efficient with calling strlen and strncpy once per iteration. Refactor the code to be valid and more effective.
Signed-off-by Wolfram Sang <[email protected]>--- lsmmc.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
Quoted 10 lines
lsmmc.c int parse_ids(struct config *config) · line 318
Quoted 8 lines
lsmmc.c +0
-0@@ -316,8 +316,9 @@ int parse_ids(struct config *config)/* MMC/SD file parsing functions */ char *read_file (char *name) {See Uwe's note - https://www.spinics.net/lists/linux-mmc/msg51882.html
Thanks for the pointer. Chris, how do you feel with maintaining mmc-utils? Would you maybe be willing to share maintenance with someone?
Happy hacking,
Wolfram
Hi everyone, I'm very sorry for the delay in responding to these patches and the duplicated effort it caused.
Quoted 9 lines · Wolfram Sang
Hi Avri, hi Chris,
Quoted 7 lines
Quoted 7 lines
I got a compile error with GCC7. When trimming white spaces from strings lsmmc uses strncpy with overlapping memory areas. This is not allowed. In addition, the implementation was not efficient with calling strlen and strncpy once per iteration. Refactor the code to be valid and more effective.
Signed-off-by Wolfram Sang <[email protected]>
Thanks, I've applied this fix of Wolfram's to mmc-utils master.
Quoted 2 lines
Thanks for the pointer. Chris, how do you feel with maintaining mmc-utils? Would you maybe be willing to share maintenance with someone?
Yep, that'd make sense.
Signature · 1 line
Chris Ball <http://printf.net/>