Error checking for mpatch.c

Thomas Arendsen Hein thomas at intevation.de
Sat May 21 12:28:12 UTC 2005


* Christopher Li <hg at chrisli.org> [20050521 11:18]:
> Avoid crash on bad bins.
> --- hg.orig/mercurial/mpatch.c	2005-05-20 21:33:11.000000000 -0400
> +++ hg/mercurial/mpatch.c	2005-05-21 01:58:22.000000000 -0400

Attached are two patches (One fix and goto-remove, one cosmetic) to
be applied after this patch, just look at their descriptions.

Thomas

-- 
Email: thomas at intevation.de
http://intevation.de/~thomas/
-------------- next part --------------
# HG changeset patch
# User Thomas Arendsen Hein <thomas at intevation.de>
# Node ID 7b74eb8e56f2eb286c4ca87821a153ef8880dc58
# Parent  4a9f7895b186e5ae414b2e5a64ae359bbde8eef1

combine(): Don't use 'goto'. Do b->head only if b != NULL.

--- a/mercurial/mpatch.c Sat May 21 12:00:50 2005
+++ b/mercurial/mpatch.c Sat May 21 12:19:28 2005
@@ -152,45 +152,37 @@
    this deletes a and b and returns the resultant list. */
 static struct flist *combine(struct flist *a, struct flist *b)
 {
-	struct flist *c;
-	struct frag *bh = b->head, *ct;
+	struct flist *c = NULL;
+	struct frag *bh, *ct;
 	int offset = 0, post;
 
-        if (!a || !b)
-                goto abort;
-	c = lalloc((lsize(a) + lsize(b)) * 2);
-        if (!c)
-                goto abort;
-
-	while (bh != b->tail) {
-		/* save old hunks */
-		offset = gather(c, a, bh->start, offset);
-
-		/* discard replaced hunks */
-		post = discard(a, bh->end, offset);
-
-		/* insert new hunk */
-		ct = c->tail;
-		ct->start = bh->start - offset;
-		ct->end = bh->end - post;
-		ct->len = bh->len;
-		ct->data = bh->data;
-		c->tail++;
-		bh++;
-		offset = post;
-	}
-
-	/* hold on to tail from a */
-	memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
-	c->tail += lsize(a);
+	if (a && b && (c = lalloc((lsize(a) + lsize(b)) * 2))) {
+		bh = b->head;
+		while (bh != b->tail) {
+			/* save old hunks */
+			offset = gather(c, a, bh->start, offset);
+
+			/* discard replaced hunks */
+			post = discard(a, bh->end, offset);
+
+			/* insert new hunk */
+			ct = c->tail;
+			ct->start = bh->start - offset;
+			ct->end = bh->end - post;
+			ct->len = bh->len;
+			ct->data = bh->data;
+			c->tail++;
+			bh++;
+			offset = post;
+		}
+
+		/* hold on to tail from a */
+		memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a));
+		c->tail += lsize(a);
+	}
 	lfree(a);
 	lfree(b);
 	return c;
-
-abort:
-        lfree(a);
-        lfree(b);
-        return NULL;
 }
 
 /* decode a binary patch into a hunk list */
-------------- next part --------------
# HG changeset patch
# User Thomas Arendsen Hein <thomas at intevation.de>
# Node ID f3b4e5d39a091a9183ad97bc933f102853c89ff9
# Parent  7b74eb8e56f2eb286c4ca87821a153ef8880dc58

Use <Tab> instead of spaces.

--- a/mercurial/mpatch.c Sat May 21 12:19:28 2005
+++ b/mercurial/mpatch.c Sat May 21 12:22:28 2005
@@ -42,22 +42,22 @@
 	struct flist *a;
 
 	a = malloc(sizeof(struct flist));
-        if (!a)
-                return NULL;
+	if (!a)
+		return NULL;
 	a->head = a->tail = a->base = malloc(sizeof(struct frag) * size);
-        if (a->base)
-	        return a;
-        free(a);
-        return NULL;
+	if (a->base)
+		return a;
+	free(a);
+	return NULL;
 
 }
 
 static void lfree(struct flist *a)
 {
-        if (a) {
-	        free(a->base);
-	        free(a);
-        }
+	if (a) {
+		free(a->base);
+		free(a);
+	}
 }
 
 static int lsize(struct flist *a)
@@ -279,18 +279,18 @@
 	}
 
 	patch = fold(bins, 0, len);
-        if (!patch)
-                return PyErr_NoMemory();
+	if (!patch)
+		return PyErr_NoMemory();
 	outlen = calcsize(PyString_Size(text), patch);
 	result = PyString_FromStringAndSize(NULL, outlen);
-        if (!result)
-                return NULL;
+	if (!result)
+		return NULL;
 	in = PyString_AsString(text);
-        if (!in)
-                return NULL;
+	if (!in)
+		return NULL;
 	out = PyString_AsString(result);
-        if (!out)
-                return NULL;
+	if (!out)
+		return NULL;
 	apply(out, in, PyString_Size(text), patch);
 	lfree(patch);
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.mercurial-scm.org/pipermail/mercurial/attachments/20050521/a313dd0b/attachment-0001.asc>


More information about the Mercurial mailing list