File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
features/cellular/framework/AT Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -722,7 +722,22 @@ int32_t ATHandler::read_int()
722
722
return -1 ;
723
723
}
724
724
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;
726
741
}
727
742
728
743
void ATHandler::set_delimiter (char delimiter)
Original file line number Diff line number Diff line change @@ -410,9 +410,9 @@ class ATHandler {
410
410
*/
411
411
ssize_t read_hex_string (char *str, size_t size);
412
412
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.
414
414
*
415
- * @return the positive integer or -1 in case of error.
415
+ * @return the non-negative integer or -1 in case of error.
416
416
*/
417
417
int32_t read_int ();
418
418
You can’t perform that action at this time.
0 commit comments