From ed5a1921810d3ce32cc66fa08f40f6a0f9d7a8fd Mon Sep 17 00:00:00 2001 From: andersen Date: Tue, 3 Apr 2001 22:50:52 +0000 Subject: if strlen(s) was 0, it would then end up using s[-1] as an array index. Bad, bad, bad. This was crashing the shell on powerpc boxes, though all other archs seem to have a much more forgiving malloc implementations. I finally found this bug using electric-fence on a powerpc box. -Erik git-svn-id: svn://busybox.net/trunk/busybox@2233 69ca8d6d-28ef-0310-b511-8ec308f3f277 --- libbb/trim.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libbb/trim.c b/libbb/trim.c index 8a9b07a14..be97d4924 100644 --- a/libbb/trim.c +++ b/libbb/trim.c @@ -33,13 +33,14 @@ void trim(char *s) { + int len; + /* trim trailing whitespace */ - while (isspace(s[strlen(s)-1])) - s[strlen(s)-1]='\0'; + while ( (len=strlen(s)) >= 1 && isspace(s[len-1])) + s[len-1]='\0'; /* trim leading whitespace */ - memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s)); - + memmove(s, &s[strspn(s, " \n\r\t\v")], len); } /* END CODE */ -- cgit v1.2.3-55-g6feb