Fix possible endless loop conditions
authorWilfried Goesgens <dothebart@citadel.org>
Tue, 3 May 2011 19:49:23 +0000 (19:49 +0000)
committerWilfried Goesgens <dothebart@citadel.org>
Tue, 3 May 2011 19:49:23 +0000 (19:49 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c
libcitadel/lib/urlhandling.c

index d223a506ecf55fec4d30da66b125c881d2a9a594..936f9117c85328f92e870664a81812307e2ebb46 100644 (file)
@@ -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 */
 
index f621255c8224363b10fd9a0ba31783f405e0aa58..4e4328916edb2bb86e54740f3ce278e84d4afaed 100644 (file)
@@ -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<e) && !IsEmptyStr(s)) {
+       while ((s < e) && !IsEmptyStr(s)) {
                if (*s == separator) {
                        ++current_token;
                }
@@ -1620,7 +1636,7 @@ int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator
        //cit_backtrace();
        //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
 
-       while ((s<EndBuffer) && !IsEmptyStr(s)) {
+       while ((s < EndBuffer) && !IsEmptyStr(s)) {
                if (*s == separator) {
                        ++current_token;
                }
index 6f7ebb58c628b945e7d20a36a668fba2b79b132c..44be3f598a6aa1e77dffb2b6ea684ecefaca4d57 100644 (file)
@@ -23,6 +23,8 @@ void FreeURL(ParsedURL** Url)
 {
        if (*Url != NULL) {
                FreeStrBuf(&(*Url)->URL);
+               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);
+       }
+}