From 3f210970bf9450156005e28ee45f64f6d39bc936 Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Thu, 13 Apr 2023 14:33:52 +0200 Subject: [PATCH] BUG/MINOR: stick_table: alert when type len has incorrect characters Alert when the len argument of a stick table type contains incorrect characters. Replace atol by strtol. Could be backported in every maintained versions. --- include/haproxy/stick_table.h | 2 +- src/stick_table.c | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/haproxy/stick_table.h b/include/haproxy/stick_table.h index cbec13795..90ab6f3fe 100644 --- a/include/haproxy/stick_table.h +++ b/include/haproxy/stick_table.h @@ -47,7 +47,7 @@ int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcount); int stktable_get_key_shard(struct stktable *t, const void *key, size_t len); int stktable_init(struct stktable *t); -int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size); +int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size, const char *file, int linenum); int parse_stick_table(const char *file, int linenum, char **args, struct stktable *t, char *id, char *nid, struct peers *peers); struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key); diff --git a/src/stick_table.c b/src/stick_table.c index 7ff4c4722..1f46795af 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -788,7 +788,7 @@ struct stktable_type stktable_types[SMP_TYPES] = { * Returns 0 on successful parsing, else 1. * is set at next configuration index. */ -int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) +int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size, const char *file, int linenum) { for (*type = 0; *type < SMP_TYPES; (*type)++) { if (!stktable_types[*type].kw) @@ -801,10 +801,14 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { if (strcmp("len", args[*myidx]) == 0) { + char *stop; + (*myidx)++; - *key_size = atol(args[*myidx]); - if (!*key_size) - break; + *key_size = strtol(args[*myidx], &stop, 10); + if (*stop != '\0' || !*key_size) { + ha_alert("parsing [%s:%d] : 'len' expects a positive integer argument.\n", file, linenum); + return 1; + } if (*type == SMP_T_STR) { /* null terminated string needs +1 for '\0'. */ (*key_size)++; @@ -814,6 +818,7 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke } return 0; } + ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", file, linenum, args[0], args[*myidx]); return 1; } @@ -975,9 +980,7 @@ int parse_stick_table(const char *file, int linenum, char **args, } else if (strcmp(args[idx], "type") == 0) { idx++; - if (stktable_parse_type(args, &idx, &t->type, &t->key_size) != 0) { - ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", - file, linenum, args[0], args[idx]); + if (stktable_parse_type(args, &idx, &t->type, &t->key_size, file, linenum) != 0) { err_code |= ERR_ALERT | ERR_FATAL; goto out; }