Merge branch 'read-int-zero' of git://github.com/dextero/mbed-os into dev_rollup

pull/11366/head
Martin Kojtal 2019-08-28 18:37:46 +01:00
commit de627dad6a
2 changed files with 15 additions and 3 deletions

View File

@ -722,7 +722,19 @@ int32_t ATHandler::read_int()
return -1;
}
return std::strtol(buff, NULL, 10);
errno = 0;
char *endptr;
long result = std::strtol(buff, &endptr, 10);
if ((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) {
return -1; // overflow/underflow
}
if (result < 0) {
return -1; // negative values are unsupported
}
if (*buff == '\0') {
return -1; // empty string
}
return (int32_t) result;
}
void ATHandler::set_delimiter(char delimiter)

View File

@ -416,9 +416,9 @@ public:
*/
ssize_t read_hex_string(char *str, size_t size);
/** Reads as string and converts result to integer. Supports only positive integers.
/** Reads as string and converts result to integer. Supports only non-negative integers.
*
* @return the positive integer or -1 in case of error.
* @return the non-negative integer or -1 in case of error.
*/
int32_t read_int();