From: Wilfried Goesgens Date: Tue, 3 May 2011 19:49:23 +0000 (+0000) Subject: Fix possible endless loop conditions X-Git-Tag: v8.11~1073 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=f05150ceed3e6b58f07173a40b54862754761dac Fix possible endless loop conditions --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index d223a506e..936f9117c 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -358,11 +358,14 @@ const char *GetIconFilename(char *MimeType, size_t len); typedef struct ParsedURL ParsedURL; struct ParsedURL { StrBuf *URL; + StrBuf *UrlWithoutCred; + StrBuf *CurlCreds; unsigned Port; const char *Host; const char *User; const char *Pass; const char *LocalPart; + const char *PlainUrl; int IsIP; int IPv6; int af; @@ -373,6 +376,7 @@ struct ParsedURL { void FreeURL(ParsedURL** Url); int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort); +void CurlPrepareURL(ParsedURL *Url); /* tools */ diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index f621255c8..4e4328916 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -276,9 +276,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; @@ -479,9 +482,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); if (NewBuf->buf == NULL) { @@ -532,9 +540,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); @@ -858,7 +871,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 { @@ -899,7 +913,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 { @@ -930,7 +945,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; } @@ -1333,7 +1349,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 ((sURL); + FreeStrBuf(&(*Url)->UrlWithoutCred); + FreeStrBuf(&(*Url)->CurlCreds); if ((*Url)->Next != NULL) FreeURL(&(*Url)->Next); free(*Url); @@ -109,3 +111,25 @@ int ParseURL(ParsedURL **Url, StrBuf *UrlStr, unsigned short DefaultPort) *Url = url; return 1; } + +void CurlPrepareURL(ParsedURL *Url) +{ + if (!strcmp(ChrPtr(Url->URL), "http")) + Url->UrlWithoutCred = NewStrBufPlain(ChrPtr(Url->URL), -1); + else + Url->UrlWithoutCred = NewStrBufPlain(HKEY("http://")); + StrBufAppendBufPlain(Url->UrlWithoutCred, Url->Host, -1, 0); + StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY(":"), 0); + + StrBufAppendPrintf(Url->UrlWithoutCred, "%u", Url->Port); + StrBufAppendBufPlain(Url->UrlWithoutCred, HKEY("/"), 0); + if (Url->LocalPart) + StrBufAppendBufPlain(Url->UrlWithoutCred, Url->LocalPart, -1, 0); + if (Url->User != NULL) + { + Url->CurlCreds = NewStrBufPlain(Url->User, -1); + StrBufAppendBufPlain(Url->CurlCreds, HKEY(":"), 0); + if (Url->Pass != NULL) + StrBufAppendBufPlain(Url->CurlCreds, Url->Pass, -1, 0); + } +}