From: Michal Privoznik <[email protected]> The aim of virSkipSpacesBackwards() is find the first space trailing character in given string, For instance, if the input is "Something whitespacey ", then the output should be pointing to the very first space after "y".
Problem here is that the input string is constant, but the returned pointer is non-constant. This is confusing, a caller shouldn't be able to modify the string, since the input was a constant string. Therefore, make the function return a const pointer too. Under the hood the function used virTrimSpaces() which under some circumstances could modify the input string. A trick was used to hide this fact away, but to be double sure rewrite the function's body. Signed-off-by: Michal Privoznik <[email protected]> --- src/util/virstring.c | 20 +++++++++++++------- src/util/virstring.h | 2 +- src/util/virsysinfo.c | 14 +++++++------- tests/virstringtest.c | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/util/virstring.c b/src/util/virstring.c index 7249ab4e7c..e001d76bf1 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -484,17 +484,23 @@ virTrimSpaces(char *str, char **endp) * but spaces. */ void -virSkipSpacesBackwards(const char *str, char **endp) +virSkipSpacesBackwards(const char *str, + const char **endp) { - /* Casting away const is safe, since virTrimSpaces does not - * modify string with this particular usage. */ - char *s = (char*) str; + const char *end; if (!*endp) - *endp = s + strlen(s); - virTrimSpaces(s, endp); - if (s == *endp) + end = str + strlen(str); + else + end = *endp; + + while (end > str && g_ascii_isspace(end[-1])) + end--; + + if (str == end) *endp = NULL; + else + *endp = end; } /** diff --git a/src/util/virstring.h b/src/util/virstring.h index 3d880faf0c..8c2208ece8 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -75,7 +75,7 @@ void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1); void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1); void virSkipToDigit(const char **str) ATTRIBUTE_NONNULL(1); void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1); -void virSkipSpacesBackwards(const char *str, char **endp) +void virSkipSpacesBackwards(const char *str, const char **endp) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); bool virStringIsEmpty(const char *str); diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c index 0f12a8964f..c017ad34b7 100644 --- a/src/util/virsysinfo.c +++ b/src/util/virsysinfo.c @@ -625,7 +625,7 @@ virSysinfoParseBIOS(const char *base, virSysinfoBIOSDef **bios) { int ret = -1; const char *cur; - char *eol = NULL; + const char *eol = NULL; virSysinfoBIOSDef *def; if ((cur = strstr(base, "BIOS Information")) == NULL) @@ -679,7 +679,7 @@ virSysinfoParseX86System(const char *base, virSysinfoSystemDef **sysdef) { int ret = -1; const char *cur; - char *eol = NULL; + const char *eol = NULL; virSysinfoSystemDef *def; if ((cur = strstr(base, "System Information")) == NULL) @@ -755,7 +755,7 @@ virSysinfoParseX86BaseBoard(const char *base, size_t *nbaseBoard) { const char *cur; - char *eol = NULL; + const char *eol = NULL; virSysinfoBaseBoardDef *boards = NULL; size_t nboards = 0; @@ -832,7 +832,7 @@ virSysinfoParseX86Chassis(const char *base, { int ret = -1; const char *cur; - char *eol = NULL; + const char *eol = NULL; virSysinfoChassisDef *def; if ((cur = strstr(base, "Chassis Information")) == NULL) @@ -942,7 +942,7 @@ virSysinfoParseOEMStrings(const char *base, while ((cur = strstr(cur, "String "))) { char *collon = NULL; unsigned int idx = 0; - char *eol; + const char *eol; cur += 7; @@ -1005,7 +1005,7 @@ static void virSysinfoParseX86Processor(const char *base, virSysinfoDef *ret) { const char *cur, *tmp_base; - char *eol; + const char *eol; virSysinfoProcessorDef *processor; while ((tmp_base = strstr(base, "Processor Information")) != NULL) { @@ -1103,7 +1103,7 @@ static void virSysinfoParseX86Memory(const char *base, virSysinfoDef *ret) { const char *cur, *tmp_base; - char *eol; + const char *eol; virSysinfoMemoryDef *memory; while ((tmp_base = strstr(base, "Memory Device")) != NULL) { diff --git a/tests/virstringtest.c b/tests/virstringtest.c index a9c8e621ce..0792155cc3 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -118,7 +118,7 @@ static int testSkipSpacesBackwards(const void *opaque G_GNUC_UNUSED) { const char *str = TEST_STR TEST_SPACES; - char *eol = NULL; + const char *eol = NULL; virSkipSpacesBackwards(str, &eol); -- 2.51.2
