##// 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 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 {
57 if (a) {
51 58 free(a->base);
52 59 free(a);
53 60 }
61 }
54 62
55 63 static int lsize(struct flist *a)
56 64 {
@@ -144,13 +152,16 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
159 if (a && b)
151 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 165 /* save old hunks */
155 166 offset = gather(c, a, bh->start, offset);
156 167
@@ -164,13 +175,14 b' static struct flist *combine(struct flis'
164 175 ct->len = bh->len;
165 176 ct->data = bh->data;
166 177 c->tail++;
167 bh++;
168 178 offset = post;
169 179 }
170 180
171 181 /* hold on to tail from a */
172 182 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
173 183 c->tail += lsize(a);
184 }
185
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);
292 if (result) {
275 293 in = PyString_AsString(text);
276 294 out = PyString_AsString(result);
277 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