]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pattern: make pat_ref_append() return the newly added element
authorWilly Tarreau <w@1wt.eu>
Wed, 28 Oct 2020 09:52:46 +0000 (10:52 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 31 Oct 2020 12:13:48 +0000 (13:13 +0100)
It's more convenient to return the element than to return just 0 or 1,
as the next thing we'll want to do is to act on this element! In addition
it was using variable arguments instead of consts, causing some reuse
constraints which were also addressed. This doesn't change its use as
a boolean, hence why call places were not modified.

include/haproxy/pattern.h
src/pattern.c

index 62fb5c04ce8f8f2b2dc9b6af8c2420c08dcf70a3..e689a134b438560db9e210f8ab6b2de41bf3efbe 100644 (file)
@@ -189,7 +189,7 @@ struct pat_ref *pat_ref_lookupid(int unique_id);
 struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags);
 struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags);
 struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key);
-int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line);
+struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line);
 int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
 int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
 int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err);
index 5de9de1abf548e11ae104a7a64614344e109ac09..cdacefba0b136f31a4a8a23122402c30a865499b 100644 (file)
@@ -1893,40 +1893,40 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f
        return ref;
 }
 
-/* This function adds entry to <ref>. It can failed with memory error.
- * If the function fails, it returns 0.
+/* This function adds entry to <ref>. It can fail on memory error. It returns
+ * the newly added element on success, or NULL on failure. The PATREF_LOCK on
+ * <ref> must be held.
  */
-int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
+struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line)
 {
        struct pat_ref_elt *elt;
 
        elt = malloc(sizeof(*elt));
        if (!elt)
-               return 0;
+               goto fail;
 
        elt->line = line;
 
        elt->pattern = strdup(pattern);
-       if (!elt->pattern) {
-               free(elt);
-               return 0;
-       }
+       if (!elt->pattern)
+               goto fail;
 
        if (sample) {
                elt->sample = strdup(sample);
-               if (!elt->sample) {
-                       free(elt->pattern);
-                       free(elt);
-                       return 0;
-               }
+               if (!elt->sample)
+                       goto fail;
        }
        else
                elt->sample = NULL;
 
        LIST_INIT(&elt->back_refs);
        LIST_ADDQ(&ref->head, &elt->list);
-
-       return 1;
+       return elt;
+ fail:
+       if (elt)
+               free(elt->pattern);
+       free(elt);
+       return NULL;
 }
 
 /* This function create sample found in <elt>, parse the pattern also