commit 623b887f7fdc34131a81efe93f02cb158fad91f5
parent 830efea3e9d6945f3be7ae58791f3ffe0f8a1753
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Mon, 2 Nov 2015 17:12:13 +0100
Bugfig on cstr_to_uint
Bug was due to int/long identity on Win64
Fixed by adding new cstr_to_ulong API call
The new cstr_to_uint conforms to the libc when processing negative numbers
(by returning positive overflow-produced values instead of errors)
Diffstat:
1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/src/cstr.h b/src/cstr.h
@@ -88,17 +88,35 @@ cstr_to_int(const char* str, int* dst)
}
static INLINE res_T
+cstr_to_ulong(const char* str, unsigned long* dst)
+{
+ char* end;
+ ASSERT(dst);
+ if (!str) return RES_BAD_ARG;
+ errno = 0;
+ *dst = strtoul(str, &end, 10/* base */);
+ if(end == str || errno == ERANGE)
+ return RES_BAD_ARG;
+ ASSERT(errno == 0);
+ for(; *end != '\0'; ++end) {
+ if(*end != ' ' && *end != '\t')
+ return RES_BAD_ARG;
+ }
+ return RES_OK;
+}
+
+static INLINE res_T
cstr_to_uint(const char* str, unsigned* dst)
{
- long l;
+ unsigned long l;
res_T res;
ASSERT(dst);
- res = cstr_to_long(str, &l);
- if(res != RES_OK)
+ res = cstr_to_ulong(str, &l);
+ if (res != RES_OK)
return res;
- if(l < 0 || (sizeof(long) > sizeof(unsigned) && l > (long)UINT_MAX))
+ if (l > (unsigned long) UINT_MAX)
return RES_BAD_ARG;
- *dst = (unsigned)l;
+ *dst = (unsigned) l;
return RES_OK;
}