Blame SOURCES/cvsps-2.2b1-dynamic-logbuf.patch

3c6710
# From: http://ydirson.free.fr/soft/git/cvsps.git
3c6710
3c6710
commit 76a9c2aaa0d2957de0bc8f0c0b994abfd1645a50
3c6710
Author: David D. Kilzer <ddkilzer@kilzer.net>
3c6710
Date:   Mon Jun 20 01:04:34 2005 +0200
3c6710
3c6710
    Dynamically allocate the log buffer to prevent warning messages
3c6710
    
3c6710
    On anoncvs.opensource.apple.com (Apple's anonymous CVS server for
3c6710
    WebKit), some very long log entries were included in CVS.  I got tired
3c6710
    of cvsps-2.1 truncating them, so I made the 'logbuff' buffer be
3c6710
    dynamically allocated.
3c6710
3c6710
diff --git i/cache.c w/cache.c
3c6710
index 4c51cf7..01a8ed3 100644
3c6710
--- i/cache.c
3c6710
+++ w/cache.c
3c6710
@@ -108,10 +108,19 @@ time_t read_cache()
3c6710
     int tag_flags = 0;
3c6710
     char branchbuff[LOG_STR_MAX] = "";
3c6710
     int branch_add = 0;
3c6710
-    char logbuff[LOG_STR_MAX] = "";
3c6710
+    int logbufflen = LOG_STR_MAX + 1;
3c6710
+    char * logbuff = malloc(logbufflen);
3c6710
     time_t cache_date = -1;
3c6710
     int read_version;
3c6710
 
3c6710
+    if (logbuff == NULL)
3c6710
+    {
3c6710
+	debug(DEBUG_SYSERROR, "could not malloc %d bytes for logbuff in read_cache", logbufflen);
3c6710
+	exit(1);
3c6710
+    }
3c6710
+
3c6710
+    logbuff[0] = 0;
3c6710
+
3c6710
     if (!(fp = cache_open("r")))
3c6710
 	goto out;
3c6710
 
3c6710
@@ -299,8 +308,19 @@ time_t read_cache()
3c6710
 	    else
3c6710
 	    {
3c6710
 		/* Make sure we have enough in the buffer */
3c6710
-		if (strlen(logbuff)+strlen(buff)
3c6710
-		    strcat(logbuff, buff);
3c6710
+		int len = strlen(buff);
3c6710
+		if (strlen(logbuff) + len >= LOG_STR_MAX)
3c6710
+		{
3c6710
+		    logbufflen += (len >= LOG_STR_MAX ? (len+1) : LOG_STR_MAX);
3c6710
+		    char * newlogbuff = realloc(logbuff, logbufflen);
3c6710
+		    if (newlogbuff == NULL)
3c6710
+		    {
3c6710
+			debug(DEBUG_SYSERROR, "could not realloc %d bytes for logbuff in read_cache", logbufflen);
3c6710
+			exit(1);
3c6710
+		    }
3c6710
+		    logbuff = newlogbuff;
3c6710
+		}
3c6710
+		strcat(logbuff, buff);
3c6710
 	    }
3c6710
 	    break;
3c6710
 	case CACHE_NEED_PS_MEMBERS:
3c6710
@@ -332,6 +352,7 @@ time_t read_cache()
3c6710
  out_close:
3c6710
     fclose(fp);
3c6710
  out:
3c6710
+    free(logbuff);
3c6710
     return cache_date;
3c6710
 }
3c6710
 
3c6710
diff --git i/cvsps.c w/cvsps.c
3c6710
index f0e7d29..db28d7c 100644
3c6710
--- i/cvsps.c
3c6710
+++ w/cvsps.c
3c6710
@@ -269,7 +269,8 @@ static void load_from_cvs()
3c6710
     PatchSetMember * psm = NULL;
3c6710
     char datebuff[26];
3c6710
     char authbuff[AUTH_STR_MAX];
3c6710
-    char logbuff[LOG_STR_MAX + 1];
3c6710
+    int logbufflen = LOG_STR_MAX + 1;
3c6710
+    char * logbuff = malloc(logbufflen);
3c6710
     int loglen = 0;
3c6710
     int have_log = 0;
3c6710
     char cmd[BUFSIZ];
3c6710
@@ -277,6 +278,12 @@ static void load_from_cvs()
3c6710
     char use_rep_buff[PATH_MAX];
3c6710
     char * ltype;
3c6710
 
3c6710
+    if (logbuff == NULL)
3c6710
+    {
3c6710
+	debug(DEBUG_SYSERROR, "could not malloc %d bytes for logbuff in load_from_cvs", logbufflen);
3c6710
+	exit(1);
3c6710
+    }
3c6710
+
3c6710
     if (!no_rlog && !test_log_file && cvs_check_cap(CAP_HAVE_RLOG))
3c6710
     {
3c6710
 	ltype = "rlog";
3c6710
@@ -484,25 +491,22 @@ static void load_from_cvs()
3c6710
 		 */
3c6710
 		if (have_log || !is_revision_metadata(buff))
3c6710
 		{
3c6710
-		    /* if the log buffer is full, that's it.  
3c6710
-		     * 
3c6710
-		     * Also, read lines (fgets) always have \n in them
3c6710
-		     * (unless truncation happens)
3c6710
-		     * which we count on.  So if truncation happens,
3c6710
-		     * be careful to put a \n on.
3c6710
-		     * 
3c6710
-		     * Buffer has LOG_STR_MAX + 1 for room for \0 if
3c6710
-		     * necessary
3c6710
-		     */
3c6710
-		    if (loglen < LOG_STR_MAX)
3c6710
+		    /* If the log buffer is full, try to reallocate more. */
3c6710
+		    if (loglen < logbufflen)
3c6710
 		    {
3c6710
 			int len = strlen(buff);
3c6710
 			
3c6710
-			if (len >= LOG_STR_MAX - loglen)
3c6710
+			if (len >= logbufflen - loglen)
3c6710
 			{
3c6710
-			    debug(DEBUG_APPMSG1, "WARNING: maximum log length exceeded, truncating log");
3c6710
-			    len = LOG_STR_MAX - loglen;
3c6710
-			    buff[len - 1] = '\n';
3c6710
+			    debug(DEBUG_STATUS, "reallocating logbufflen to %d bytes for file %s", logbufflen, file->filename);
3c6710
+			    logbufflen += (len >= LOG_STR_MAX ? (len+1) : LOG_STR_MAX);
3c6710
+			    char * newlogbuff = realloc(logbuff, logbufflen);
3c6710
+			    if (newlogbuff == NULL)
3c6710
+			    {
3c6710
+				debug(DEBUG_SYSERROR, "could not realloc %d bytes for logbuff in load_from_cvs", logbufflen);
3c6710
+				exit(1);
3c6710
+			    }
3c6710
+			    logbuff = newlogbuff;
3c6710
 			}
3c6710
 
3c6710
 			debug(DEBUG_STATUS, "appending %s to log", buff);