##// END OF EJS Templates
Add safety checking to mpatch
mpm@selenic.com -
r128:d6afb6db default
parent child Browse files
Show More
@@ -39,17 +39,25 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 {
51 free(a->base);
57 if (a) {
52 free(a);
58 free(a->base);
59 free(a);
60 }
53 }
61 }
54
62
55 static int lsize(struct flist *a)
63 static int lsize(struct flist *a)
@@ -144,33 +152,37 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
151 c = lalloc((lsize(a) + lsize(b)) * 2);
159 if (a && b)
160 c = lalloc((lsize(a) + lsize(b)) * 2);
161
162 if (c) {
152
163
153 while (bh != b->tail) {
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
157 /* discard replaced hunks */
168 /* discard replaced hunks */
158 post = discard(a, bh->end, offset);
169 post = discard(a, bh->end, offset);
159
170
160 /* insert new hunk */
171 /* insert new hunk */
161 ct = c->tail;
172 ct = c->tail;
162 ct->start = bh->start - offset;
173 ct->start = bh->start - offset;
163 ct->end = bh->end - post;
174 ct->end = bh->end - post;
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++;
178 offset = post;
168 offset = post;
179 }
180
181 /* hold on to tail from a */
182 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
183 c->tail += lsize(a);
169 }
184 }
170
185
171 /* hold on to tail from a */
172 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
173 c->tail += lsize(a);
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);
275 in = PyString_AsString(text);
292 if (result) {
276 out = PyString_AsString(result);
293 in = PyString_AsString(text);
277 apply(out, in, PyString_Size(text), patch);
294 out = PyString_AsString(result);
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