From: Amaury Denoyelle Date: Thu, 11 Apr 2024 09:10:13 +0000 (+0200) Subject: MINOR: guid: define guid_is_valid_fmt() X-Git-Tag: v3.0-dev9~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=83731c8048eb51bf4c6393ceba9885d84f8c8883;p=thirdparty%2Fhaproxy.git MINOR: guid: define guid_is_valid_fmt() 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. --- diff --git a/include/haproxy/guid.h b/include/haproxy/guid.h index 8c88a8866f..ecfeb6a496 100644 --- a/include/haproxy/guid.h +++ b/include/haproxy/guid.h @@ -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 */ diff --git a/src/guid.c b/src/guid.c index d3773d4a45..f1365b621c 100644 --- a/src/guid.c +++ b/src/guid.c @@ -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 respects GUID format. If 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 * node. The string is dynamically allocated and the caller is responsible to * free it.