Skip to content

Commit 17be387

Browse files
dmcardleBoringssl LUCI CQ
authored and
Boringssl LUCI CQ
committed
Check strtoul return for overflow error in GetUnsigned()
Currently, GetUnsigned() calls strtoul and checks whether the resulting unsigned long int is greater than UINT_MAX. This implicitly assumes that UINT_MAX < ULONG_MAX. Problematically, `unsigned long int` and `unsigned` have the same size on Windows [0] and on 32-bit architectures. For correctness, we now check whether strtoul failed because it would overflow the unsigned long int before checking whether the value fits in an unsigned type. [0]: https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-160 Change-Id: I49702febf4543bfb7991592717443e0b2adb954f Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48545 Commit-Queue: Dan McArdle <[email protected]> Commit-Queue: David Benjamin <[email protected]> Reviewed-by: David Benjamin <[email protected]>
1 parent 897a2ca commit 17be387

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

tool/args.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <string>
1616
#include <vector>
1717

18+
#include <errno.h>
1819
#include <limits.h>
1920
#include <stdio.h>
2021
#include <stdlib.h>
@@ -92,13 +93,16 @@ bool GetUnsigned(unsigned *out, const std::string &arg_name,
9293
return false;
9394
}
9495

96+
errno = 0;
9597
char *endptr;
9698
unsigned long int num = strtoul(value.c_str(), &endptr, 10);
97-
if (*endptr ||
98-
num > UINT_MAX) {
99+
if (num == ULONG_MAX && errno == ERANGE) {
99100
return false;
100101
}
102+
if (*endptr != 0 || num > UINT_MAX) {
103+
return false;
104+
}
105+
*out = static_cast<unsigned>(num);
101106

102-
*out = num;
103107
return true;
104108
}

0 commit comments

Comments
 (0)