Skip to content

Allow ATHandler::read_int to return 0 successfully #11248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions features/cellular/framework/AT/ATHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ class ATHandler {
*/
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();

Expand Down