Skip to contentSkip to conversation

lib/strutils: make left and right trims more robust

PATCH Applied Sami Kerola 2 participants 2 messages Sep 4, 2016 – Sep 29, 2016 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 include/strutils.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
include/strutils.h +7 -2
index 52c28b7..51d9c9f 100644
@@ -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);
 
Signature · 1 line
2.9.3
maintainer Applied
#1 lore raw
Quoted 2 lines · Sami Kerola
 include/strutils.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com