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.
This commit is contained in:
Amaury Denoyelle 2024-04-11 11:10:13 +02:00
parent bc3c117dc0
commit 83731c8048
2 changed files with 25 additions and 10 deletions

View 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 */

View 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");
if (!guid_is_valid_fmt(uid, errmsg))
goto err;
}
c = invalid_char(uid);
if (c) {
memprintf(errmsg, "invalid character '%c'", c[0]);
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.