1#! /bin/sh /usr/share/dpatch/dpatch-run
2## 02_dynamicbufferalloc.dpatch by <crafterm@debian.org>
3##
4## All lines beginning with `## DP:' are a description of the patch.
5## DP: Dynamic buffer allocation
6
7@DPATCH@
8
9diff -urN cvsps-2.1-orig/cache.c cvsps-2.1/cache.c
10--- cvsps-2.1-orig/cache.c 2005-05-25 22:39:40.000000000 -0500
11+++ cvsps-2.1/cache.c 2005-07-26 15:21:29.716569500 -0500
12@@ -108,10 +108,19 @@
13 int tag_flags = 0;
14 char branchbuff[LOG_STR_MAX] = "";
15 int branch_add = 0;
16- char logbuff[LOG_STR_MAX] = "";
17+ int logbufflen = LOG_STR_MAX + 1;
18+ char * logbuff = malloc(logbufflen);
19 time_t cache_date = -1;
20 int read_version;
21
22+ if (logbuff == NULL)
23+ {
24+ debug(DEBUG_SYSERROR, "could not malloc %d bytes for logbuff in read_cache", logbufflen);
25+ exit(1);
26+ }
27+
28+ logbuff[0] = 0;
29+
30 if (!(fp = cache_open("r")))
31 goto out;
32
33@@ -299,8 +308,19 @@
34 else
35 {
36 /* Make sure we have enough in the buffer */
37- if (strlen(logbuff)+strlen(buff)<LOG_STR_MAX)
38- strcat(logbuff, buff);
39+ int len = strlen(buff);
40+ if (strlen(logbuff) + len >= LOG_STR_MAX)
41+ {
42+ logbufflen += (len >= LOG_STR_MAX ? (len+1) : LOG_STR_MAX);
43+ char * newlogbuff = realloc(logbuff, logbufflen);
44+ if (newlogbuff == NULL)
45+ {
46+ debug(DEBUG_SYSERROR, "could not realloc %d bytes for logbuff in read_cache", logbufflen);
47+ exit(1);
48+ }
49+ logbuff = newlogbuff;
50+ }
51+ strcat(logbuff, buff);
52 }
53 break;
54 case CACHE_NEED_PS_MEMBERS:
55@@ -332,6 +352,7 @@
56 out_close:
57 fclose(fp);
58 out:
59+ free(logbuff);
60 return cache_date;
61 }
62
63diff -urN cvsps-2.1-orig/cvsps.c cvsps-2.1/cvsps.c
64--- cvsps-2.1-orig/cvsps.c 2005-05-25 22:39:40.000000000 -0500
65+++ cvsps-2.1/cvsps.c 2005-07-26 15:22:02.558230700 -0500
66@@ -265,7 +265,8 @@
67 PatchSetMember * psm = NULL;
68 char datebuff[20];
69 char authbuff[AUTH_STR_MAX];
70- char logbuff[LOG_STR_MAX + 1];
71+ int logbufflen = LOG_STR_MAX + 1;
72+ char * logbuff = malloc(logbufflen);
73 int loglen = 0;
74 int have_log = 0;
75 char cmd[BUFSIZ];
76@@ -273,6 +274,12 @@
77 char use_rep_buff[PATH_MAX];
78 char * ltype;
79
80+ if (logbuff == NULL)
81+ {
82+ debug(DEBUG_SYSERROR, "could not malloc %d bytes for logbuff in load_from_cvs", logbufflen);
83+ exit(1);
84+ }
85+
86 if (!no_rlog && !test_log_file && cvs_check_cap(CAP_HAVE_RLOG))
87 {
88 ltype = "rlog";
89@@ -480,24 +487,22 @@
90 */
91 if (have_log || !is_revision_metadata(buff))
92 {
93- /* if the log buffer is full, that's it.
94- *
95- * Also, read lines (fgets) always have \n in them
96- * which we count on. So if truncation happens,
97- * be careful to put a \n on.
98- *
99- * Buffer has LOG_STR_MAX + 1 for room for \0 if
100- * necessary
101- */
102- if (loglen < LOG_STR_MAX)
103+ /* If the log buffer is full, try to reallocate more. */
104+ if (loglen < logbufflen)
105 {
106 int len = strlen(buff);
107
108- if (len >= LOG_STR_MAX - loglen)
109+ if (len >= logbufflen - loglen)
110 {
111- debug(DEBUG_APPMSG1, "WARNING: maximum log length exceeded, truncating log");
112- len = LOG_STR_MAX - loglen;
113- buff[len - 1] = '\n';
114+ debug(DEBUG_STATUS, "reallocating logbufflen to %d bytes for file %s", logbufflen, file->filename);
115+ logbufflen += (len >= LOG_STR_MAX ? (len+1) : LOG_STR_MAX);
116+ char * newlogbuff = realloc(logbuff, logbufflen);
117+ if (newlogbuff == NULL)
118+ {
119+ debug(DEBUG_SYSERROR, "could not realloc %d bytes for logbuff in load_from_cvs", logbufflen);
120+ exit(1);
121+ }
122+ logbuff = newlogbuff;
123 }
124
125 debug(DEBUG_STATUS, "appending %s to log", buff);