From 06a1bb335e0971a7b986d7726c48d1502e2517c4 Mon Sep 17 00:00:00 2001 From: zwelch Date: Wed, 17 Jun 2009 00:30:29 +0000 Subject: [PATCH] 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 --- src/helper/command.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/helper/command.c b/src/helper/command.c index 30c5b1210..9a8907d6a 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -863,16 +863,20 @@ long jim_global_long(const char *variable) 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) \ { \ + if (!*str) \ + return ERROR_COMMAND_SYNTAX_ERROR; \ char *end; \ *ul = func(str, &end, 0); \ - bool is_okay = *str && !*end && (max != *ul); \ - return is_okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR; \ + if (*end) \ + 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(_ullong, unsigned long long, strtoull, ULLONG_MAX) -DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MAX) -DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MAX) - +DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX) +DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX) +DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX) +DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)