Instead of throwing return 0 for invalid values. This happens anyway if the input is invalid so it makes the most sense to return 0 for invalid ranges.
Originally committed to SVN as r5343.
This commit is contained in:
parent
ef7b3c04e3
commit
2c6abb9a98
2 changed files with 4 additions and 7 deletions
|
@ -44,11 +44,8 @@ void str_lower(std::string &str) {
|
||||||
int strtoi(std::string &str) {
|
int strtoi(std::string &str) {
|
||||||
long l = strtol(str.c_str(), NULL, 10);
|
long l = strtol(str.c_str(), NULL, 10);
|
||||||
|
|
||||||
if (errno == ERANGE)
|
if ((errno == ERANGE) || (l < INT_MIN) || (l > INT_MAX))
|
||||||
throw("Cannot convert to long (invalid range)");
|
return 0;
|
||||||
|
|
||||||
if ((l < INT_MIN) || (l > INT_MAX))
|
|
||||||
throw("Cannot convert to int (invalid range)");
|
|
||||||
|
|
||||||
return (int)l;
|
return (int)l;
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,10 +73,10 @@ TEST_F(lagi_util, Utilstr_lower) {
|
||||||
|
|
||||||
TEST_F(lagi_util, UtilstrtoiInvalidRange) {
|
TEST_F(lagi_util, UtilstrtoiInvalidRange) {
|
||||||
std::string str("2147483650");
|
std::string str("2147483650");
|
||||||
EXPECT_ANY_THROW(util::strtoi(str));
|
EXPECT_EQ(util::strtoi(str), 0);
|
||||||
|
|
||||||
str.assign("-2147483650");
|
str.assign("-2147483650");
|
||||||
EXPECT_ANY_THROW(util::strtoi(str));
|
EXPECT_EQ(util::strtoi(str), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(lagi_util, UtilstrtoiInvalidString) {
|
TEST_F(lagi_util, UtilstrtoiInvalidString) {
|
||||||
|
|
Loading…
Reference in a new issue