From 65c6e3706f5290947179922c9e3b8f05ea6ae91c Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 6 Jan 2012 16:00:25 -0800 Subject: libglusterfs/dict: Add boundary conditions Functions modified: 1. data_to_int16() 2. data_to_uint16() Change-Id: I6775ca5d39f519c24ca3a07ce41018472e238cdf BUG: 769692 Signed-off-by: Harshavardhana Reviewed-on: http://review.gluster.com/2599 Tested-by: Gluster Build System Reviewed-by: Amar Tumballi Reviewed-by: Anand Avati --- libglusterfs/src/dict.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index a9d67ebc7ae..67bac435fa3 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -1085,6 +1085,8 @@ data_to_int32 (data_t *data) int16_t data_to_int16 (data_t *data) { + int16_t value = 0; + if (!data) { gf_log_callingfn ("dict", GF_LOG_WARNING, "data is NULL"); return -1; @@ -1097,7 +1099,18 @@ data_to_int16 (data_t *data) memcpy (str, data->data, data->len); str[data->len] = '\0'; - return strtol (str, NULL, 0); + errno = 0; + value = strtol (str, NULL, 0); + + if ((SHRT_MAX > value) || (SHRT_MIN < value)) { + errno = ERANGE; + gf_log_callingfn ("dict", GF_LOG_WARNING, + "Error in data conversion: " + "detected overflow"); + return -1; + } + + return (int16_t)value; } @@ -1124,7 +1137,8 @@ data_to_int8 (data_t *data) if ((SCHAR_MAX > value) || (SCHAR_MIN < value)) { errno = ERANGE; gf_log_callingfn ("dict", GF_LOG_WARNING, - "Error in data conversion: detected overflow"); + "Error in data conversion: " + "detected overflow"); return -1; } @@ -1166,6 +1180,8 @@ data_to_uint32 (data_t *data) uint16_t data_to_uint16 (data_t *data) { + uint16_t value = 0; + if (!data) return -1; @@ -1176,7 +1192,18 @@ data_to_uint16 (data_t *data) memcpy (str, data->data, data->len); str[data->len] = '\0'; - return strtol (str, NULL, 0); + errno = 0; + value = strtol (str, NULL, 0); + + if ((USHRT_MAX - value) < 0) { + errno = ERANGE; + gf_log_callingfn ("dict", GF_LOG_WARNING, + "Error in data conversion: " + "overflow detected"); + return -1; + } + + return (uint16_t)value; } uint8_t -- cgit