[augeas-devel] [PATCH 3 of 4] Check for (some) allocation failures

James Antill james.antill at redhat.com
Fri May 9 04:50:15 UTC 2008


On Thu, 2008-05-08 at 18:33 -0700, David Lutterkort wrote:
>  
>  static struct re *make_re_rep(struct re *exp, int min, int max) {
>      struct re *re = make_re(ITER);
> -    re->exp = exp;
> -    re->min = min;
> -    re->max = max;
> +    if (re) {
> +        re->exp = exp;
> +        re->min = min;
> +        re->max = max;
> +    }
>      return re;
>  }

 IMO you have to be really careful when you implicitly pass reference
ownership, I think what you really want here (dito. make_re_binop) is:

 static struct re *make_re_rep(struct re *exp, int min, int max) {
     struct re *re = make_re(ITER);
-    re->exp = exp;
-    re->min = min;
-    re->max = max;
+    if (re) {
+        re->exp = ref(exp);
+        re->min = min;
+        re->max = max;
+    }
+    unref(exp, re); /* don't leak what was passed in on failure */
     return re;
 }

...so the interface always consumes the reference, this makes the
calling code correct:

    if (match(regexp, '?')) {
        re = make_re_rep(re, 0, 1);
    } else if (match(regexp, '*')) {
        re = make_re_rep(re, 0, -1);
    } else if (match(regexp, '+')) {
        re = make_re_rep(re, 1, -1);
    }

...although the three different usages of the "re" symbol now, in that
one function is a bit ... interesting :).

-- 
James Antill <james.antill at redhat.com>
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20080509/49ebcb8d/attachment.sig>


More information about the augeas-devel mailing list