##// END OF EJS Templates
Add safety checking to mpatch
mpm@selenic.com -
r128:d6afb6db default
parent child Browse files
Show More
@@ -39,18 +39,26 b' struct flist {'
39
39
40 static struct flist *lalloc(int size)
40 static struct flist *lalloc(int size)
41 {
41 {
42 struct flist *a;
42 struct flist *a = NULL;
43
43
44 a = malloc(sizeof(struct flist));
44 a = malloc(sizeof(struct flist));
45 a->head = a->tail = a->base = malloc(sizeof(struct frag) * size);
45 if (a) {
46 a->base = malloc(sizeof(struct frag) * size);
47 if (!a->base)
48 free(a);
49 else
50 a->head = a->tail = a->base;
51 }
46 return a;
52 return a;
47 }
53 }
48
54
49 static void lfree(struct flist *a)
55 static void lfree(struct flist *a)
50 {
56 {
57 if (a) {
51 free(a->base);
58 free(a->base);
52 free(a);
59 free(a);
53 }
60 }
61 }
54
62
55 static int lsize(struct flist *a)
63 static int lsize(struct flist *a)
56 {
64 {
@@ -144,13 +152,16 b' static int discard(struct flist *src, in'
144 this deletes a and b and returns the resultant list. */
152 this deletes a and b and returns the resultant list. */
145 static struct flist *combine(struct flist *a, struct flist *b)
153 static struct flist *combine(struct flist *a, struct flist *b)
146 {
154 {
147 struct flist *c;
155 struct flist *c = NULL;
148 struct frag *bh = b->head, *ct;
156 struct frag *bh, *ct;
149 int offset = 0, post;
157 int offset = 0, post;
150
158
159 if (a && b)
151 c = lalloc((lsize(a) + lsize(b)) * 2);
160 c = lalloc((lsize(a) + lsize(b)) * 2);
152
161
153 while (bh != b->tail) {
162 if (c) {
163
164 for (bh = b->head; bh != b->tail; bh++) {
154 /* save old hunks */
165 /* save old hunks */
155 offset = gather(c, a, bh->start, offset);
166 offset = gather(c, a, bh->start, offset);
156
167
@@ -164,13 +175,14 b' static struct flist *combine(struct flis'
164 ct->len = bh->len;
175 ct->len = bh->len;
165 ct->data = bh->data;
176 ct->data = bh->data;
166 c->tail++;
177 c->tail++;
167 bh++;
168 offset = post;
178 offset = post;
169 }
179 }
170
180
171 /* hold on to tail from a */
181 /* hold on to tail from a */
172 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
182 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
173 c->tail += lsize(a);
183 c->tail += lsize(a);
184 }
185
174 lfree(a);
186 lfree(a);
175 lfree(b);
187 lfree(b);
176 return c;
188 return c;
@@ -242,6 +254,8 b' static struct flist *fold(PyObject *bins'
242 if (start + 1 == end) {
254 if (start + 1 == end) {
243 /* trivial case, output a decoded list */
255 /* trivial case, output a decoded list */
244 PyObject *tmp = PyList_GetItem(bins, start);
256 PyObject *tmp = PyList_GetItem(bins, start);
257 if (!tmp)
258 return NULL;
245 return decode(PyString_AsString(tmp), PyString_Size(tmp));
259 return decode(PyString_AsString(tmp), PyString_Size(tmp));
246 }
260 }
247
261
@@ -259,7 +273,7 b' patches(PyObject *self, PyObject *args)'
259 char *in, *out;
273 char *in, *out;
260 int len, outlen;
274 int len, outlen;
261
275
262 if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
276 if (!PyArg_ParseTuple(args, "SO:mpatch", &text, &bins))
263 return NULL;
277 return NULL;
264
278
265 len = PyList_Size(bins);
279 len = PyList_Size(bins);
@@ -270,13 +284,18 b' patches(PyObject *self, PyObject *args)'
270 }
284 }
271
285
272 patch = fold(bins, 0, len);
286 patch = fold(bins, 0, len);
287 if (!patch)
288 return PyErr_NoMemory();
289
273 outlen = calcsize(PyString_Size(text), patch);
290 outlen = calcsize(PyString_Size(text), patch);
274 result = PyString_FromStringAndSize(NULL, outlen);
291 result = PyString_FromStringAndSize(NULL, outlen);
292 if (result) {
275 in = PyString_AsString(text);
293 in = PyString_AsString(text);
276 out = PyString_AsString(result);
294 out = PyString_AsString(result);
277 apply(out, in, PyString_Size(text), patch);
295 apply(out, in, PyString_Size(text), patch);
296 }
297
278 lfree(patch);
298 lfree(patch);
279
280 return result;
299 return result;
281 }
300 }
282
301
General Comments 0
You need to be logged in to leave comments. Login now