[PATH] (take three) Error checking for mpatch.c

Christopher Li hg at chrisli.org
Sat May 21 15:10:18 UTC 2005


On Sat, May 21, 2005 at 09:59:55AM -0400, Christopher Li wrote:
> 
> I leak patch here. Bad. bad.

I am still leaking result string on bad text input.

This version address it. I promise it is the lats one.

Sorry about that.

Chris


Avoid crash on bad bins.

Signed-Off-by: Christopher Li<hg at chrisli.org>

Index: hg/mercurial/mpatch.c
===================================================================
--- hg.orig/mercurial/mpatch.c	2005-05-21 10:56:32.000000000 -0400
+++ hg/mercurial/mpatch.c	2005-05-21 11:06:08.000000000 -0400
@@ -42,7 +42,13 @@
 	struct flist *a;
 
 	a = malloc(sizeof(struct flist));
+	if (!a)
+		return NULL;
 	a->head = a->tail = a->base = malloc(sizeof(struct frag) * size);
+	if (!a->base) {
+		free(a);
+		return NULL;
+	}
 	return a;
 }
 
@@ -52,6 +58,15 @@
 	free(a);
 }
 
+static inline void * cleanup(struct flist *a, struct flist *b)
+{
+	if (a)
+		lfree(a);
+	if (b)
+		lfree(b);
+	return NULL;
+}
+
 static int lsize(struct flist *a)
 {
 	return a->tail - a->head;
@@ -149,6 +164,8 @@
 	int offset = 0, post;
 
 	c = lalloc((lsize(a) + lsize(b)) * 2);
+	if (!a || !b || !c)
+		return cleanup(a, b);
 
 	while (bh != b->tail) {
 		/* save old hunks */
@@ -269,10 +286,17 @@
 		return text;
 	}
 
+	in = PyString_AsString(text);
+	if (!in)
+		return NULL;
+
 	patch = fold(bins, 0, len);
+	if (!patch)
+		return PyErr_NoMemory();
 	outlen = calcsize(PyString_Size(text), patch);
 	result = PyString_FromStringAndSize(NULL, outlen);
-	in = PyString_AsString(text);
+	if (!result)
+		return cleanup(patch, NULL);
 	out = PyString_AsString(result);
 	apply(out, in, PyString_Size(text), patch);
 	lfree(patch);



More information about the Mercurial mailing list