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