Skip to content

Commit

Permalink
Add errno check for strtod and strtol
Browse files Browse the repository at this point in the history
  • Loading branch information
changneng committed Feb 26, 2022
1 parent 9e029be commit 0bd2f33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
32 changes: 26 additions & 6 deletions tools/ldb_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,21 @@ bool LDBCommand::ParseDoubleOption(
LDBCommandExecuteResult& exec_state) {
auto itr = option_map_.find(option);
if (itr != option_map_.end()) {
try {
#if defined(CYGWIN)
value = std::strtod(itr->second.c_str(), 0);
char* str_end = nullptr;
value = std::strtod(itr->second.c_str(), &str_end);
if (str_end == itr->second.c_str()) {
exec_state =
LDBCommandExecuteResult::Failed(option + " has an invalid value.");
} else if (errno == ERANGE) {
exec_state = LDBCommandExecuteResult::Failed(
option + " has a value out-of-range.");
} else {
return true;
}
#else
try {
value = std::stod(itr->second);
#endif
return true;
} catch (const std::invalid_argument&) {
exec_state =
Expand All @@ -565,6 +574,7 @@ bool LDBCommand::ParseDoubleOption(
exec_state = LDBCommandExecuteResult::Failed(
option + " has a value out-of-range.");
}
#endif
}
return false;
}
Expand All @@ -582,12 +592,21 @@ bool LDBCommand::ParseIntOption(
LDBCommandExecuteResult& exec_state) {
auto itr = option_map_.find(option);
if (itr != option_map_.end()) {
try {
#if defined(CYGWIN)
value = strtol(itr->second.c_str(), 0, 10);
char* str_end = nullptr;
value = strtol(itr->second.c_str(), &str_end, 10);
if (str_end == itr->second.c_str()) {
exec_state =
LDBCommandExecuteResult::Failed(option + " has an invalid value.");
} else if (errno == ERANGE) {
exec_state = LDBCommandExecuteResult::Failed(
option + " has a value out-of-range.");
} else {
return true;
}
#else
try {
value = std::stoi(itr->second);
#endif
return true;
} catch (const std::invalid_argument&) {
exec_state =
Expand All @@ -596,6 +615,7 @@ bool LDBCommand::ParseIntOption(
exec_state = LDBCommandExecuteResult::Failed(
option + " has a value out-of-range.");
}
#endif
}
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions tools/ldb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,7 @@ def testDumpLoad(self):
self.assertTrue(self.dumpDb(
"--db=%s" % (origDbPath), dumpFilePath))
self.assertTrue(self.loadDb(
"--db=%s %s --create_if_missing" % (loadedDbPath, blobParams),
dumpFilePath))
self.assertTrue(self.loadDb(
"--db=%s %s" % (loadedDbPath, blobParams),
"--db=%s %s --create_if_missing --disable_wal" % (loadedDbPath, blobParams),
dumpFilePath))
self.assertRunOKFull("scan --db=%s" % loadedDbPath,
"x1 : y1\nx2 : y2\nx3 : y3\nx4 : y4")
Expand Down

0 comments on commit 0bd2f33

Please sign in to comment.