From: Wilfried Goesgens Date: Tue, 3 May 2011 19:49:23 +0000 (+0000) Subject: Fix possible endless loop conditions X-Git-Tag: v7.87~26 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=36665aae19fb2b9eb963299139a9dd7da2793a43 Fix possible endless loop conditions --- diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index ed17a0a0c..1408669b0 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -259,9 +259,12 @@ static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize) return -1; if (DestSize > 0) - while (NewSize <= DestSize) + while ((NewSize <= DestSize) && (NewSize != 0)) NewSize *= 2; + if (NewSize == 0) + return -1; + NewBuf= (char*) malloc(NewSize); if (NewBuf == NULL) return -1; @@ -393,9 +396,14 @@ StrBuf* NewStrBufPlain(const char* ptr, int nChars) else CopySize = nChars; - while (Siz <= CopySize) + while ((Siz <= CopySize) && (Siz != 0)) Siz *= 2; + if (Siz == 0) + { + return NULL; + } + NewBuf->buf = (char*) malloc(Siz); NewBuf->BufSize = Siz; if (ptr != NULL) { @@ -441,9 +449,14 @@ int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars) else CopySize = nChars; - while (Siz <= CopySize) + while ((Siz <= CopySize) && (Siz != 0)) Siz *= 2; + if (Siz == 0) { + FlushStrBuf(Buf); + return -1; + } + if (Siz != Buf->BufSize) IncreaseBuf(Buf, 0, Siz); memcpy(Buf->buf, ptr, CopySize); @@ -767,7 +780,8 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap) va_end(apl); newused = Offset + nWritten; if (newused >= Buf->BufSize) { - IncreaseBuf(Buf, 1, newused); + if (IncreaseBuf(Buf, 1, newused) == -1) + return; /* TODO: error handling? */ newused = Buf->BufSize + 1; } else { @@ -808,7 +822,8 @@ void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...) va_end(arg_ptr); newused = Buf->BufUsed + nWritten; if (newused >= Buf->BufSize) { - IncreaseBuf(Buf, 1, newused); + if (IncreaseBuf(Buf, 1, newused) == -1) + return; /* TODO: error handling? */ newused = Buf->BufSize + 1; } else { @@ -839,7 +854,8 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...) nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr); va_end(arg_ptr); if (nWritten >= Buf->BufSize) { - IncreaseBuf(Buf, 0, 0); + if (IncreaseBuf(Buf, 0, 0) == -1) + return; /* TODO: error handling? */ nWritten = Buf->BufSize + 1; continue; } @@ -1222,7 +1238,7 @@ int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char se //cit_backtrace(); //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); - while ((s: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); - while ((s