]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: guid: define guid_is_valid_fmt()
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 11 Apr 2024 09:10:13 +0000 (11:10 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 26 Apr 2024 09:29:25 +0000 (11:29 +0200)
Extract GUID format validation in a dedicated function named
guid_is_valid_fmt(). For the moment, it is only used on guid_insert().

This will be reused when parsing stats-file, to ensure GUID has a valid
format before tree lookup.

include/haproxy/guid.h
src/guid.c

index 8c88a8866fcbdabfdbf1dfa946ca75fea7e81fb2..ecfeb6a49646aebc4463b63346fff7e213cf2b23 100644 (file)
@@ -10,6 +10,7 @@ int guid_insert(enum obj_type *obj_type, const char *uid, char **errmsg);
 void guid_remove(struct guid_node *guid);
 struct guid_node *guid_lookup(const char *uid);
 
+int guid_is_valid_fmt(const char *uid, char **errmsg);
 char *guid_name(const struct guid_node *guid);
 
 #endif /* _HAPROXY_GUID_H */
index d3773d4a459036d3bdecf6853d7dc93ec28c11b3..f1365b621ccd6b7dec6b53c3be27d0c1978a18fe 100644 (file)
@@ -30,18 +30,9 @@ int guid_insert(enum obj_type *objt, const char *uid, char **errmsg)
        struct ebpt_node *node;
        char *key = NULL;
        char *dup_name = NULL;
-       const char *c;
-
-       if (strlen(uid) > GUID_MAX_LEN) {
-               memprintf(errmsg, "UID too big");
-               goto err;
-       }
 
-       c = invalid_char(uid);
-       if (c) {
-               memprintf(errmsg, "invalid character '%c'", c[0]);
+       if (!guid_is_valid_fmt(uid, errmsg))
                goto err;
-       }
 
        switch (obj_type(objt)) {
        case OBJ_TYPE_PROXY:
@@ -111,6 +102,29 @@ struct guid_node *guid_lookup(const char *uid)
        return guid;
 }
 
+/* Returns a boolean checking if <uid> respects GUID format. If <errmsg> is not
+ * NULL, it will be allocated with an error description in case of invalid
+ * format.
+ */
+int guid_is_valid_fmt(const char *uid, char **errmsg)
+{
+       const size_t len = strlen(uid);
+       const char *c;
+
+       if (!len || len > GUID_MAX_LEN) {
+               memprintf(errmsg, "invalid length");
+               return 0;
+       }
+
+       c = invalid_char(uid);
+       if (c) {
+               memprintf(errmsg, "invalid character '%c'", c[0]);
+               return 0;
+       }
+
+       return 1;
+}
+
 /* Generate a user-friendly description for the instance attached via <guid>
  * node. The string is dynamically allocated and the caller is responsible to
  * free it.