##// 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 40 static struct flist *lalloc(int size)
41 41 {
42 struct flist *a;
42 struct flist *a = NULL;
43 43
44 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 52 return a;
47 53 }
48 54
49 55 static void lfree(struct flist *a)
50 56 {
51 free(a->base);
52 free(a);
57 if (a) {
58 free(a->base);
59 free(a);
60 }
53 61 }
54 62
55 63 static int lsize(struct flist *a)
@@ -144,33 +152,37 b' static int discard(struct flist *src, in'
144 152 this deletes a and b and returns the resultant list. */
145 153 static struct flist *combine(struct flist *a, struct flist *b)
146 154 {
147 struct flist *c;
148 struct frag *bh = b->head, *ct;
155 struct flist *c = NULL;
156 struct frag *bh, *ct;
149 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) {
154 /* save old hunks */
155 offset = gather(c, a, bh->start, offset);
164 for (bh = b->head; bh != b->tail; bh++) {
165 /* save old hunks */
166 offset = gather(c, a, bh->start, offset);
156 167
157 /* discard replaced hunks */
158 post = discard(a, bh->end, offset);
168 /* discard replaced hunks */
169 post = discard(a, bh->end, offset);
159 170
160 /* insert new hunk */
161 ct = c->tail;
162 ct->start = bh->start - offset;
163 ct->end = bh->end - post;
164 ct->len = bh->len;
165 ct->data = bh->data;
166 c->tail++;
167 bh++;
168 offset = post;
171 /* insert new hunk */
172 ct = c->tail;
173 ct->start = bh->start - offset;
174 ct->end = bh->end - post;
175 ct->len = bh->len;
176 ct->data = bh->data;
177 c->tail++;
178 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 186 lfree(a);
175 187 lfree(b);
176 188 return c;
@@ -242,6 +254,8 b' static struct flist *fold(PyObject *bins'
242 254 if (start + 1 == end) {
243 255 /* trivial case, output a decoded list */
244 256 PyObject *tmp = PyList_GetItem(bins, start);
257 if (!tmp)
258 return NULL;
245 259 return decode(PyString_AsString(tmp), PyString_Size(tmp));
246 260 }
247 261
@@ -259,7 +273,7 b' patches(PyObject *self, PyObject *args)'
259 273 char *in, *out;
260 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 277 return NULL;
264 278
265 279 len = PyList_Size(bins);
@@ -270,13 +284,18 b' patches(PyObject *self, PyObject *args)'
270 284 }
271 285
272 286 patch = fold(bins, 0, len);
287 if (!patch)
288 return PyErr_NoMemory();
289
273 290 outlen = calcsize(PyString_Size(text), patch);
274 291 result = PyString_FromStringAndSize(NULL, outlen);
275 in = PyString_AsString(text);
276 out = PyString_AsString(result);
277 apply(out, in, PyString_Size(text), patch);
292 if (result) {
293 in = PyString_AsString(text);
294 out = PyString_AsString(result);
295 apply(out, in, PyString_Size(text), patch);
296 }
297
278 298 lfree(patch);
279
280 299 return result;
281 300 }
282 301
General Comments 0
You need to be logged in to leave comments. Login now