* first Trim the right end, so we don't need to move stuff around we would later cut
authorWilfried Göesgens <willi@citadel.org>
Mon, 22 Feb 2010 21:34:43 +0000 (21:34 +0000)
committerWilfried Göesgens <willi@citadel.org>
Mon, 22 Feb 2010 21:34:43 +0000 (21:34 +0000)
* protect us for a bufferunderrun here

libcitadel/lib/stringbuf.c

index fe4913fa4487a48fb166485ed04bfe2d5b396eae..8048aeb663dff97665a414b7ec0b26b819ffadd9 100644 (file)
@@ -931,16 +931,20 @@ void StrBufTrim(StrBuf *Buf)
        int delta = 0;
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
 
+       while ((Buf->BufUsed > 0) &&
+              isspace(Buf->buf[Buf->BufUsed - 1]))
+       {
+               Buf->BufUsed --;
+       }
+       Buf->buf[Buf->BufUsed] = '\0';
+
+       if (Buf->BufUsed == 0) return;
+
        while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
                delta ++;
        }
        if (delta > 0) StrBufCutLeft(Buf, delta);
 
-       if (Buf->BufUsed == 0) return;
-       while (isspace(Buf->buf[Buf->BufUsed - 1])){
-               Buf->BufUsed --;
-       }
-       Buf->buf[Buf->BufUsed] = '\0';
 }
 
 /**