Skip to content

Commit 6fd8e98

Browse files
authored
Merge pull request #3129 from compnerd/integer-parsing
2 parents 81bc0ee + f29e614 commit 6fd8e98

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

include/swift/Reflection/MetadataSource.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
using llvm::cast;
2929

30+
#include <climits>
3031
#include <iostream>
3132

3233
namespace swift {
@@ -45,18 +46,17 @@ class MetadataSource {
4546
const std::string::const_iterator &end,
4647
unsigned &result) {
4748
auto begin = it;
48-
while (it != end) {
49-
if (*it >= '0' && *it <= '9')
50-
++it;
51-
else
52-
break;
53-
}
49+
for (; it < end && *it >= '0' && *it <= '9'; ++it)
50+
;
51+
52+
if (std::distance(begin, it) == 0)
53+
return false;
5454

55-
std::string natural(begin, it);
56-
if (natural.empty())
55+
long int decoded = std::strtol(&*begin, nullptr, 10);
56+
if ((decoded == LONG_MAX || decoded == LONG_MIN) && errno == ERANGE)
5757
return false;
5858

59-
result = std::atoi(natural.c_str());
59+
result = static_cast<unsigned>(decoded);
6060
return true;
6161
}
6262

0 commit comments

Comments
 (0)