From 60862f5916dff99c91dd6d6319c958d02cb535d8 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 3 Jan 2012 11:07:00 -0800 Subject: libglusterfs: Add boundary conditions to data_to_int8() function. Change-Id: Iff50d44568895a75fc8743a10346990f764cf8fa BUG: 769692 Signed-off-by: Harshavardhana Reviewed-on: http://review.gluster.com/2556 Tested-by: Gluster Build System Reviewed-by: Jeff Darcy Reviewed-by: Vijay Bellur --- libglusterfs/src/dict.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'libglusterfs/src') diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index 2ad1ae1abff..833f117fc67 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -1104,6 +1104,8 @@ data_to_int16 (data_t *data) int8_t data_to_int8 (data_t *data) { + int32_t value = 0; + if (!data) { gf_log_callingfn ("dict", GF_LOG_WARNING, "data is NULL"); return -1; @@ -1116,7 +1118,17 @@ data_to_int8 (data_t *data) memcpy (str, data->data, data->len); str[data->len] = '\0'; - return (int8_t)strtol (str, NULL, 0); + errno = 0; + value = strtol (str, NULL, 0); + + if ((SCHAR_MAX > value) || (SCHAR_MIN < value)) { + errno = ERANGE; + gf_log_callingfn ("dict", GF_LOG_WARNING, + "Error in data conversion: detected overflow"); + return -1; + } + + return (int8_t)value; } @@ -1189,7 +1201,9 @@ data_to_uint8 (data_t *data) if ((UCHAR_MAX - value) < 0) { errno = ERANGE; - gf_log_callingfn ("dict", GF_LOG_WARNING, "data conversion overflow detected (%s)", strerror(errno)); + gf_log_callingfn ("dict", GF_LOG_WARNING, + "data conversion overflow detected (%s)", + strerror(errno)); return -1; } -- cgit