Convert core parse_type implementations to check for underflow errors.

git-svn-id: svn://svn.berlios.de/openocd/trunk@2256 b42882b7-edfa-0310-969c-e2dbd0fdcd60
This commit is contained in:
zwelch 2009-06-17 00:30:29 +00:00
parent a830197f59
commit 06a1bb335e
1 changed files with 12 additions and 8 deletions

View File

@ -863,16 +863,20 @@ long jim_global_long(const char *variable)
return 0; return 0;
} }
#define DEFINE_PARSE_NUM_TYPE(name, type, func, max) \ #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
int parse##name(const char *str, type *ul) \ int parse##name(const char *str, type *ul) \
{ \ { \
if (!*str) \
return ERROR_COMMAND_SYNTAX_ERROR; \
char *end; \ char *end; \
*ul = func(str, &end, 0); \ *ul = func(str, &end, 0); \
bool is_okay = *str && !*end && (max != *ul); \ if (*end) \
return is_okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR; \ return ERROR_COMMAND_SYNTAX_ERROR; \
if (*ul == max || (min && min == *ul)) \
return ERROR_COMMAND_SYNTAX_ERROR; \
return ERROR_OK; \
} }
DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, ULONG_MAX) DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX)
DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, ULLONG_MAX) DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MAX) DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX)
DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MAX) DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)