From f681fe89b71956a24a24fc1809af7530f89f7694 Mon Sep 17 00:00:00 2001 From: Zdenek Styblik Date: Thu, 26 Jan 2012 08:22:11 +0000 Subject: [PATCH] Adds function str2uint() to convert from string to uint32_t with checks for valid input. --- ipmitool/lib/helper.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ipmitool/lib/helper.c b/ipmitool/lib/helper.c index 85cbe56..1fcd2e0 100644 --- a/ipmitool/lib/helper.c +++ b/ipmitool/lib/helper.c @@ -223,6 +223,30 @@ int str2int(const char * str, int32_t * int_ptr) return 0; } /* str2int(...) */ +/* str2uint - safely convert string to uint32_t + * + * @str: source string to convert from + * @uint_ptr: pointer where to store result + * + * returns zero on success + * returns (-1) if one of args is NULL, (-2) invalid input, (-3) for *flow + */ +int str2uint(const char * str, uint32_t * uint_ptr) +{ + int rc = 0; + uint64_t arg_ulong = 0; + if ( (rc = str2ulong(str, &arg_ulong)) != 0) { + *uint_ptr = 0; + return rc; + } + + if (arg_ulong > UINT32_MAX) + return (-3); + + *uint_ptr = (uint32_t)arg_ulong; + return 0; +} /* str2uint(...) */ + /* str2short - safely convert string to int16_t * * @str: source string to convert from