Catch all exceptions thrown by std::stoi()

This commit is contained in:
Raphael Coeffic
2022-01-01 13:51:20 +01:00
parent eb1e3078c8
commit 85a184f15e
5 changed files with 35 additions and 23 deletions
@@ -306,7 +306,11 @@ static int32_t YamlReadLimitValue(const YAML::Node& node, int32_t shift = 0)
return 10000 + std::stoi(val.substr(2)); return 10000 + std::stoi(val.substr(2));
} }
return std::stoi(val) + shift; try {
return std::stoi(val) + shift;
} catch(...) {
throw YAML::TypedBadConversion<int>(node.Mark());
}
} }
static std::string YamlWriteLimitValue(int32_t sval, int32_t shift = 0) static std::string YamlWriteLimitValue(int32_t sval, int32_t shift = 0)
@@ -593,7 +597,9 @@ struct convert<YamlTelemSource> {
if (str == "none") { if (str == "none") {
rhs.src = 0; rhs.src = 0;
} else { } else {
rhs.src = std::stoi(str) + 1; try {
rhs.src = std::stoi(str) + 1;
} catch(...) {}
} }
} }
return true; return true;
@@ -333,14 +333,16 @@ bool convert<ModuleData>::decode(const Node& node, ModuleData& rhs)
subType >> st_str; subType >> st_str;
if (!st_str.empty()) { if (!st_str.empty()) {
size_t pos = 0; size_t pos = 0;
int rfProtocol = std::stoi(st_str, &pos); try {
st_str = st_str.substr(pos + 1); int rfProtocol = std::stoi(st_str, &pos);
int rfSubType = std::stoi(st_str); st_str = st_str.substr(pos + 1);
convertMultiProtocolToEtx(&rfProtocol, &rfSubType); int rfSubType = std::stoi(st_str);
if (rfProtocol > 0) { convertMultiProtocolToEtx(&rfProtocol, &rfSubType);
rhs.multi.rfProtocol = rfProtocol - 1; if (rfProtocol > 0) {
rhs.subType = rfSubType; rhs.multi.rfProtocol = rfProtocol - 1;
} rhs.subType = rfSubType;
}
} catch(...) {}
} }
} break; } break;
} }
@@ -102,12 +102,13 @@ RawSource YamlRawSourceDecode(const std::string& src_str)
const char* val = src_str.data(); const char* val = src_str.data();
size_t val_len = src_str.size(); size_t val_len = src_str.size();
if (val_len > 0 && val[0] == 'I') { if (val_len > 1 && val[0] == 'I'
&& (val[1] >= '0') && (val[1] <= '9')) {
int idx = std::stoi(src_str.substr(1)); int idx = std::stoi(src_str.substr(1));
if (idx < CPN_MAX_INPUTS) if (idx < CPN_MAX_INPUTS) {
rhs = RawSource(SOURCE_TYPE_VIRTUAL_INPUT, idx); rhs = RawSource(SOURCE_TYPE_VIRTUAL_INPUT, idx);
}
} else if (val_len >= 2 } else if (val_len >= 2
&& val[0] == 'S' && val[0] == 'S'
&& val[1] >= 'A' && val[1] >= 'A'
@@ -101,9 +101,9 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
if (val_len >= 2 && val[0] == 'L' && (val[1] >= '0' && val[1] <= '9')) { if (val_len >= 2 && val[0] == 'L' && (val[1] >= '0' && val[1] <= '9')) {
if (std::stoi(sw_str_tmp.substr(1, val_len - 1)) < CPN_MAX_LOGICAL_SWITCHES) { int sw_idx = std::stoi(sw_str_tmp.substr(1, val_len - 1));
rhs = RawSwitch(SWITCH_TYPE_VIRTUAL, if (sw_idx < CPN_MAX_LOGICAL_SWITCHES) {
std::stoi(sw_str_tmp.substr(1, val_len - 1))); rhs = RawSwitch(SWITCH_TYPE_VIRTUAL, sw_idx);
} }
} else if (val_len > 3 && val[0] == '6' && val[1] == 'P' && } else if (val_len > 3 && val[0] == '6' && val[1] == 'P' &&
@@ -121,9 +121,10 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
} else if (val_len >= 2 && val[0] == 'T' && } else if (val_len >= 2 && val[0] == 'T' &&
(val[1] >= '0' && val[1] <= '9')) { (val[1] >= '0' && val[1] <= '9')) {
if (std::stoi(sw_str_tmp.substr(1, val_len - 1)) < CPN_MAX_SENSORS) { // starts at T1
rhs = RawSwitch(SWITCH_TYPE_SENSOR, int sensor_idx = std::stoi(sw_str_tmp.substr(1, val_len - 1));
std::stoi(sw_str_tmp.substr(1, val_len - 1)) - 1); if ((sensor_idx > 0) && (sensor_idx < CPN_MAX_SENSORS)) {
rhs = RawSwitch(SWITCH_TYPE_SENSOR, sensor_idx - 1);
} }
} else if (sw_str_tmp.substr(0, 4) == std::string("Trim")) { } else if (sw_str_tmp.substr(0, 4) == std::string("Trim")) {
@@ -50,10 +50,12 @@ const YamlLookupTable sliderConfigLut = {
int YamlStickLookup::name2idx(const std::string& name) int YamlStickLookup::name2idx(const std::string& name)
{ {
int idx = std::stoi(name); try {
if (idx < 4) { int idx = std::stoi(name);
return idx; if (idx < 4) {
} return idx;
}
} catch(...) {}
return -1; return -1;
} }