aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWolfram Sang <wsa+renesas@sang-engineering.com>2018-12-03 14:19:23 +0100
committerChris Ball <chris@printf.net>2018-12-08 00:03:54 -0500
commitbd23a79e34ec7f058426027fbcc1b493f960da90 (patch)
tree74b815a56eca61d37a089570c987eb86b1a66fb8
parentb4fe0c8c0e57a74c01755fa9362703b60d7ee49d (diff)
downloadmmc-utils-bd23a79e34ec7f058426027fbcc1b493f960da90.tar.gz
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. Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Signed-off-by: Chris Ball <chris@printf.net>
-rw-r--r--lsmmc.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/lsmmc.c b/lsmmc.c
index c4faa00..9737b37 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -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);
}