[PATCH 1/6] libbb: Fix trim

Samuel Mendoza-Jonas sam at mendozajonas.com
Tue May 8 06:30:20 UTC 2018


Commit 20077c14, "make trim() return pointer to terminating NULL",
avoids a superfluous store if the string doesn't need to be trimmed, but
accidentally returns a pointer to the end of the string.

Move the 's += len' statement to inside the if statement to make sure we
return a pointer to the start of the string.

Signed-off-by: Samuel Mendoza-Jonas <sam at mendozajonas.com>
---
 libbb/trim.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libbb/trim.c b/libbb/trim.c
index e360ba138..1bfae0dc0 100644
--- a/libbb/trim.c
+++ b/libbb/trim.c
@@ -27,11 +27,10 @@ char* FAST_FUNC trim(char *s)
 		}
 	}
 
-	s += len;
 	/* If it was a "const char*" which does not need trimming,
 	 * avoid superfluous store */
 	if (old != len)
-		*s = '\0';
+		s[len] = '\0';
 
 	return s;
 }
-- 
2.17.0



More information about the busybox mailing list