From 683bc1ade3d5d936793456bc56f65ca1b2035568 Mon Sep 17 00:00:00 2001 From: michalbiesek <39981869+michalbiesek@users.noreply.github.com> Date: Sat, 7 Sep 2019 02:06:14 +0200 Subject: [PATCH] cc_bstring simplify and fix (#207) * Use isdigit instead of manual check for digit * Fix integer overflow in expression (-INT64_MIN) --- src/cc_bstring.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cc_bstring.c b/src/cc_bstring.c index 68374423c..2d10f64bd 100644 --- a/src/cc_bstring.c +++ b/src/cc_bstring.c @@ -20,6 +20,8 @@ #include #include +#include + /* * Byte string (struct bstring) is a sequence of unsigned char * The length of the string is pre-computed and explicitly available. @@ -131,14 +133,14 @@ bstring_atoi64(int64_t *i64, struct bstring *str) for (*i64 = 0LL; offset < str->len; offset++) { c = *(str->data + offset); - if (c < '0' || c > '9') { + if (isdigit(c) == 0) { return CC_ERROR; } // overflow check if (offset == CC_INT64_MAXLEN - 2) { if (sign < 0 && *i64 == INT64_MIN / 10 && - c - '0' > (uint64_t)(-INT64_MIN) % 10) { + c - '0' > -(INT64_MIN % 10)) { return CC_ERROR; } if (sign > 0 && *i64 == INT64_MAX / 10 && @@ -167,7 +169,7 @@ bstring_atou64(uint64_t *u64, struct bstring *str) for (offset = 0; offset < str->len; offset++) { c = *(str->data + offset); - if (c < '0' || c > '9') { + if (isdigit(c) == 0) { return CC_ERROR; }