Skip to content

Commit c0032c9

Browse files
author
Marcin Radomski
committed
ATHandler::read_int: allow returning 0 successfully
1 parent 033fffe commit c0032c9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,22 @@ int32_t ATHandler::read_int()
722722
return -1;
723723
}
724724

725-
return std::strtol(buff, NULL, 10);
725+
errno = 0;
726+
char *endptr;
727+
long result = std::strtol(buff, &endptr, 10);
728+
if ((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) {
729+
return -1; // overflow/underflow
730+
}
731+
if (result < 0) {
732+
return -1; // negative values are unsupported
733+
}
734+
if (*buff == '\0') {
735+
return -1; // empty string
736+
}
737+
if (*endptr != '\0') {
738+
return -1; // trailing garbage
739+
}
740+
return (int32_t) result;
726741
}
727742

728743
void ATHandler::set_delimiter(char delimiter)

features/cellular/framework/AT/ATHandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ class ATHandler {
410410
*/
411411
ssize_t read_hex_string(char *str, size_t size);
412412

413-
/** Reads as string and converts result to integer. Supports only positive integers.
413+
/** Reads as string and converts result to integer. Supports only non-negative integers.
414414
*
415-
* @return the positive integer or -1 in case of error.
415+
* @return the non-negative integer or -1 in case of error.
416416
*/
417417
int32_t read_int();
418418

0 commit comments

Comments
 (0)