From b6d2f7e3029356d18097f99fcf54478415eca7bb Mon Sep 17 00:00:00 2001 From: Zdenek Styblik Date: Tue, 24 Jan 2012 12:14:21 +0000 Subject: [PATCH] Commit introduces set of functions to safely convert user input to numbers. These functions utilize strtol() and strtoul() to convert input and check for invalid input as well as for *flows. Functions added: str2long(), str2ulong(), str2int(), str2short(), str2uchar(); --- ipmitool/include/ipmitool/helper.h | 7 ++ ipmitool/lib/helper.c | 127 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/ipmitool/include/ipmitool/helper.h b/ipmitool/include/ipmitool/helper.h index 7033766..ed9cb07 100644 --- a/ipmitool/include/ipmitool/helper.h +++ b/ipmitool/include/ipmitool/helper.h @@ -64,6 +64,13 @@ struct oemvalstr { const char * val2str(uint16_t val, const struct valstr * vs); const char * oemval2str(uint32_t oem,uint16_t val, const struct oemvalstr * vs); + +int str2long(const char * str, int64_t * lng_ptr); +int str2ulong(const char * str, uint64_t * ulng_ptr); +int str2int(const char * str, int32_t * int_ptr); +int str2short(const char * str, int16_t * shrt_ptr); +int str2uchar(const char * str, uint8_t * uchr_ptr); + uint16_t str2val(const char * str, const struct valstr * vs); void print_valstr(const struct valstr * vs, const char * title, int loglevel); void print_valstr_2col(const struct valstr * vs, const char * title, int loglevel); diff --git a/ipmitool/lib/helper.c b/ipmitool/lib/helper.c index 2f68102..e258609 100644 --- a/ipmitool/lib/helper.c +++ b/ipmitool/lib/helper.c @@ -35,6 +35,7 @@ #include /* For TIOCNOTTY */ #include +#include #include #include #include @@ -144,6 +145,132 @@ const char * oemval2str(uint32_t oem, uint16_t val, return un_str; } +/* str2long - safely convert string to int64_t + * + * @str: source string to convert from + * @lng_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 str2long(const char * str, int64_t * lng_ptr) +{ + char * end_ptr = 0; + if (!str || !lng_ptr) + return (-1); + + *lng_ptr = 0; + errno = 0; + *lng_ptr = strtol(str, &end_ptr, 0); + + if (end_ptr != '\0') + return (-2); + + if (errno != 0) + return (-3); + + return 0; +} /* str2long(...) */ + +/* str2ulong - safely convert string to uint64_t + * + * @str: source string to convert from + * @ulng_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 str2ulong(const char * str, uint64_t * ulng_ptr) +{ + char * end_ptr = 0; + if (!str || !ulng_ptr) + return (-1); + + *ulng_ptr = 0; + errno = 0; + *ulng_ptr = strtoul(str, &end_ptr, 0); + + if (end_ptr != '\0') + return (-2); + + if (errno != 0) + return (-3); + + return 0; +} /* str2ulong(...) */ + +/* str2int - safely convert string to int32_t + * + * @str: source string to convert from + * @int_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 str2int(const char * str, int32_t * int_ptr) +{ + int rc = 0; + int64_t arg_long = 0; + if ( (rc = str2long(str, &arg_long)) != 0 ) { + *int_ptr = 0; + return rc; + } + + if (arg_long < INT32_MIN || arg_long > INT32_MAX) + return (-3); + + *int_ptr = (int32_t)arg_long; + return 0; +} /* str2int(...) */ + +/* str2short - safely convert string to int16_t + * + * @str: source string to convert from + * @shrt_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 str2short(const char * str, int16_t * shrt_ptr) +{ + int rc = (-3); + int64_t arg_long = 0; + if ( (rc = str2long(str, &arg_long)) != 0 ) { + *shrt_ptr = 0; + return rc; + } + + if (arg_long < INT16_MIN || arg_long > INT16_MAX) + return (-3); + + *shrt_ptr = (int16_t)arg_long; + return 0; +} /* str2short(...) */ + +/* str2uchar - safely convert string to uint8 + * + * @str: source string to convert from + * @uchr_ptr: pointer where to store result + * + * returns zero on success + * returns (-1) if one of args is NULL, (-2) or (-3) if conversion fails + */ +int str2uchar(const char * str, uint8_t * uchr_ptr) +{ + int rc = (-3); + int64_t arg_long = 0; + if ( (rc = str2ulong(str, &arg_long)) != 0 ) { + *uchr_ptr = 0; + return rc; + } + + if (arg_long > UINT8_MAX) + return (-3); + + *uchr_ptr = (uint8_t)arg_long; + return 0; +} /* str2uchar(...) */ + uint16_t str2val(const char *str, const struct valstr *vs) { int i;