[augeas-devel] [PATCH 02 of 11] Cleanly free regexp patterns; put empty_pattern into ro storage

David Lutterkort dlutter at redhat.com
Thu May 15 20:01:52 UTC 2008


1 file changed, 14 insertions(+), 8 deletions(-)
src/regexp.c |   22 ++++++++++++++--------


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1210881424 25200
# Node ID f184d5b6fea0a5d5901382c4f9b55c42189156fe
# Parent  f9e72fd730eb94bed6d0844335ca69c2a69d814e
Cleanly free regexp patterns; put empty_pattern into ro storage

diff -r f9e72fd730eb -r f184d5b6fea0 src/regexp.c
--- a/src/regexp.c	Thu May 15 12:56:01 2008 -0700
+++ b/src/regexp.c	Thu May 15 12:57:04 2008 -0700
@@ -22,7 +22,11 @@
 
 #include "syntax.h"
 
-static struct string *empty_pattern = NULL;
+static const struct string empty_pattern_string = {
+    .ref = REF_MAX, .str = "()"
+};
+
+static const struct string *const empty_pattern = &empty_pattern_string;
 
 void print_regexp(FILE *out, struct regexp *r) {
     if (r == NULL) {
@@ -55,7 +59,10 @@ void free_regexp(struct regexp *regexp) 
     assert(regexp->ref == 0);
     unref(regexp->info, info);
     unref(regexp->pattern, string);
-    free(regexp->re);
+    if (regexp->re != NULL) {
+        regfree(regexp->re);
+        free(regexp->re);
+    }
     free(regexp);
 }
 
@@ -177,13 +184,12 @@ struct regexp *regexp_make_empty(struct 
 struct regexp *regexp_make_empty(struct info *info) {
     struct regexp *regexp;
 
-    if (empty_pattern == NULL) {
-        regexp = make_regexp(info, strdup("()"));
-        empty_pattern = ref(regexp->pattern);
-    } else {
-        make_ref(regexp);
+    make_ref(regexp);
+    if (regexp != NULL) {
         regexp->info = ref(info);
-        regexp->pattern = ref(empty_pattern);
+        /* Casting away the CONST for EMPTY_PATTERN is ok since it
+           is protected against changes because REF == REF_MAX */
+        regexp->pattern = (struct string *) empty_pattern;
     }
     return regexp;
 }




More information about the augeas-devel mailing list