diff --git a/lib/libutil/passwd.c b/lib/libutil/passwd.c index 9cc1d481a349..d6c0d9ea4e8f 100644 --- a/lib/libutil/passwd.c +++ b/lib/libutil/passwd.c @@ -503,13 +503,21 @@ trim_whitespace(char *line) _DIAGASSERT(line != NULL); + /* Handle empty string */ + if (strlen(line) == 0) + return; + /* Remove leading spaces */ p = line; while (isspace((unsigned char) *p)) p++; memmove(line, p, strlen(p) + 1); - /* Remove trailing spaces */ + /* Handle empty string after removal of whitespace characters */ + if (strlen(line) == 0) + return; + + /* Remove trailing spaces, line must not be empty string here */ p = line + strlen(line) - 1; while (isspace((unsigned char) *p)) p--;