]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/stringbuf.c
* more testcoverage
[citadel.git] / libcitadel / lib / stringbuf.c
index 88c984ae5961e07f71e504856169d1c68a817108..c41be76fcdd2d4482827ac8da341b89168019001 100644 (file)
@@ -521,7 +521,7 @@ char *SmashStrBuf (StrBuf **SmashMe)
 {
        char *Ret;
 
-       if (*SmashMe == NULL)
+       if ((SmashMe == NULL) || (*SmashMe == NULL))
                return NULL;
 #ifdef SIZE_DEBUG
        if (hFreeDbglog == -1){
@@ -1438,8 +1438,8 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
        /* Find desired @parameter */
        end = Source->buf + Source->BufUsed;
        d = Source->buf;
-       while ((count < parmnum) &&
-              (d <= end))
+       while ((d <= end) && 
+              (count < parmnum))
        {
                /* End of string, bail! */
                if (!*d) {
@@ -1456,8 +1456,8 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
 
        /* Find next @parameter */
        s = d;
-       while ((*s && *s != separator) &&
-              (s <= end))
+       while ((s <= end) && 
+              (*s && *s != separator))
        {
                s++;
        }
@@ -1466,16 +1466,20 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
        ReducedBy = d - s;
 
        /* Hack and slash */
-       if (*s) {
+       if (s >= end) {
+               return 0;
+       }
+       else if (*s) {
                memmove(d, s, Source->BufUsed - (s - Source->buf));
                Source->BufUsed += ReducedBy;
+               Source->buf[Source->BufUsed] = '\0';
        }
        else if (d == Source->buf) {
                *d = 0;
                Source->BufUsed = 0;
        }
        else {
-               *--d = 0;
+               *--d = '\0';
                Source->BufUsed += ReducedBy;
        }
        /*
@@ -2040,6 +2044,7 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
 
 }
 
+static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
 /**
@@ -2073,6 +2078,17 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
        int IsNonBlock;
        struct timeval tv;
        
+       if ((Line == NULL) ||
+           (Pos == NULL) ||
+           (IOBuf == NULL) ||
+           (*fd == -1))
+       {
+               if (Pos != NULL)
+                       *Pos = NULL;
+               *Error = ErrRBLF_PreConditionFailed;
+               return -1;
+       }
+
        pos = *Pos;
        if ((IOBuf->BufUsed > 0) && 
            (pos != NULL) && 
@@ -2107,6 +2123,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
        
        if (IOBuf->BufSize - IOBuf->BufUsed < 10) {
                IncreaseBuf(IOBuf, 1, -1);
+               *Pos = NULL;
        }
 
        fdflags = fcntl(*fd, F_GETFL);
@@ -2151,6 +2168,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
                        IOBuf->buf[IOBuf->BufUsed] = '\0';
                        if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
                                IncreaseBuf(IOBuf, 1, -1);
+                               *Pos = NULL;
                        }
                        
                        pche = IOBuf->buf + IOBuf->BufUsed;
@@ -2177,6 +2195,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
 
 }
 
+static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong arguments or invalid Filedescriptor";
 /**
  * @ingroup StrBuf_IO
  * @brief Input binary data from socket
@@ -2198,8 +2217,12 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
        int IsNonBlock;
        struct timeval tv;
        fd_set rfds;
+
        if ((Buf == NULL) || (*fd == -1))
+       {
+               *Error = ErrRBLF_BLOBPreConditionFailed;
                return -1;
+       }
        if (!append)
                FlushStrBuf(Buf);
        if (Buf->BufUsed + nBytes >= Buf->BufSize)
@@ -2252,6 +2275,7 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
        return nRead;
 }
 
+const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
 const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
 /**
  * @ingroup StrBuf_BufferedIO
@@ -2293,7 +2317,13 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        int nSuccessLess;
 
        if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL))
+       {
+               if (*Pos != NULL)
+                       *Pos = NULL;
+               *Error = ErrRBB_BLOBFPreConditionFailed;
                return -1;
+       }
+
        if (!append)
                FlushStrBuf(Blob);
        if (Blob->BufUsed + nBytes >= Blob->BufSize) 
@@ -2335,6 +2365,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        }
 
        FlushStrBuf(IOBuf);
+       *Pos = NULL;
        if (IOBuf->BufSize < nBytes - nRead)
                IncreaseBuf(IOBuf, 0, nBytes - nRead);
        ptr = IOBuf->buf;