mpreduce: Don't crash if we've accumulated no instructions.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 4 Apr 2006 16:17:45 +0000 (17:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 4 Apr 2006 16:17:45 +0000 (17:17 +0100)
Return failure; I think we might be able to do better, but think about
this later.

mpreduce.c
tests/mpreduce

index 02ce2cf..1c39564 100644 (file)
@@ -119,7 +119,7 @@ int mpreduce_create(mpreduce *r, mp *p)
        INSTR(op | !!b, w, b);
     }
   }
-  if ((DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
+  if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
     mp_drop(r->p);
     DA_DESTROY(&iv);
     return (-1);
@@ -130,7 +130,9 @@ int mpreduce_create(mpreduce *r, mp *p)
   /* --- Wrap up --- */
 
   r->in = DA_LEN(&iv);
-  if (!r->s) {
+  if (!r->in)
+    r->iv = 0;
+  else if (!r->s) {
     r->iv = xmalloc(r->in * sizeof(mpreduce_instr));
     memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr));
   } else {
@@ -167,7 +169,7 @@ int mpreduce_create(mpreduce *r, mp *p)
 void mpreduce_destroy(mpreduce *r)
 {
   mp_drop(r->p);
-  xfree(r->iv);
+  if (r->iv) xfree(r->iv);
 }
 
 /* --- @mpreduce_dump@ --- *
index 4caad8b..faa84d2 100644 (file)
@@ -4,6 +4,7 @@
 
 reduce {
   0xc000 0x16cb3 0xacb3;
+  0x8000 0x345545 0x5545;
 
   0x72e2c37447f8bca34c4a39b130ea8e5c9a7d8b54564aa88ea773
   0x367aa8f5ba9ac4e8e2ea198b8af2c3b3081deab392ffc05715783b245a62a6fa