kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 341a111a2f5f822b1c05f8533592d50a441fb7fe
parent 25cf8dc659b11b6d416365a648392f4584801427
Author: Joris Vink <joris@coders.se>
Date:   Mon, 21 Jul 2014 01:51:51 +0200

Rework kore_strtonum64() so we can catch overflows for u_int64_t.

Seems that strtoull() if a negative string is supplied rather just
wraps the value, which I don't want here.

Diffstat:
src/utils.c | 34++++++++++++++++++++++++----------
1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/utils.c b/src/utils.c @@ -129,29 +129,43 @@ u_int64_t kore_strtonum64(const char *str, int sign, int *err) { u_int64_t l; + long long ll; char *ep; + int check; - errno = 0; - l = strtoull(str, &ep, 10); - if (errno != 0 || str == ep || *ep != '\0') { - *err = KORE_RESULT_ERROR; - return (0); + check = 1; + + ll = strtoll(str, &ep, 10); + if ((errno == EINVAL || errno == ERANGE) && + (ll == LLONG_MIN || ll == LLONG_MAX)) { + if (sign) { + *err = KORE_RESULT_ERROR; + return (0); + } + + check = 0; } - if (sign) { - if ((int64_t)l < LLONG_MIN || l > LLONG_MAX) { + if (!sign) { + l = strtoull(str, &ep, 10); + if ((errno == EINVAL || errno == ERANGE) && l == ULONG_MAX) { *err = KORE_RESULT_ERROR; return (0); } - } else { - if (l > ULLONG_MAX) { + + if (check && ll < 0) { *err = KORE_RESULT_ERROR; return (0); } } + if (str == ep || *ep != '\0') { + *err = KORE_RESULT_ERROR; + return (0); + } + *err = KORE_RESULT_OK; - return (l); + return ((sign) ? (u_int64_t)ll : l); } int