aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSami Kerola <kerolasa@iki.fi>2016-09-04 11:15:34 +0100
committerKarel Zak <kzak@redhat.com>2016-09-29 11:49:08 +0200
commit0b404f0845fe97f84e45f9b7b23e9e1814d3ef4e (patch)
tree7ccc0020ba42be41aab6663aa1487f5af972772f
parent68a7f92b94431b28aa345ab30214c3490dcc10bd (diff)
downloadutil-linux-0b404f0845fe97f84e45f9b7b23e9e1814d3ef4e.tar.gz
lib/strutils: make left and right trims more robust
Do not follow null pointer, and stop going any further when ltrim_whitespace() is at the end of a string. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
-rw-r--r--include/strutils.h9
1 files changed, 7 insertions, 2 deletions
diff --git a/include/strutils.h b/include/strutils.h
index 52c28b77bf..51d9c9ff35 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -178,8 +178,11 @@ static inline const char *skip_blank(const char *p)
*/
static inline size_t rtrim_whitespace(unsigned char *str)
{
- size_t i = strlen((char *) str);
+ size_t i;
+ if (!str)
+ return 0;
+ i = strlen((char *) str);
while (i) {
i--;
if (!isspace(str[i])) {
@@ -200,7 +203,9 @@ static inline size_t ltrim_whitespace(unsigned char *str)
size_t len;
unsigned char *p;
- for (p = str; p && isspace(*p); p++);
+ if (!str)
+ return 0;
+ for (p = str; *p && isspace(*p); p++);
len = strlen((char *) p);