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 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)
@@ -593,7 +597,9 @@ struct convert<YamlTelemSource> {
if (str == "none") {
rhs.src = 0;
} else {
rhs.src = std::stoi(str) + 1;
try {
rhs.src = std::stoi(str) + 1;
} catch(...) {}
}
}
return true;
@@ -333,14 +333,16 @@ bool convert<ModuleData>::decode(const Node& node, ModuleData& rhs)
subType >> st_str;
if (!st_str.empty()) {
size_t pos = 0;
int rfProtocol = std::stoi(st_str, &pos);
st_str = st_str.substr(pos + 1);
int rfSubType = std::stoi(st_str);
convertMultiProtocolToEtx(&rfProtocol, &rfSubType);
if (rfProtocol > 0) {
rhs.multi.rfProtocol = rfProtocol - 1;
rhs.subType = rfSubType;
}
try {
int rfProtocol = std::stoi(st_str, &pos);
st_str = st_str.substr(pos + 1);
int rfSubType = std::stoi(st_str);
convertMultiProtocolToEtx(&rfProtocol, &rfSubType);
if (rfProtocol > 0) {
rhs.multi.rfProtocol = rfProtocol - 1;
rhs.subType = rfSubType;
}
} catch(...) {}
}
} break;
}
@@ -102,12 +102,13 @@ RawSource YamlRawSourceDecode(const std::string& src_str)
const char* val = src_str.data();
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));
if (idx < CPN_MAX_INPUTS)
if (idx < CPN_MAX_INPUTS) {
rhs = RawSource(SOURCE_TYPE_VIRTUAL_INPUT, idx);
}
} else if (val_len >= 2
&& val[0] == 'S'
&& 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 (std::stoi(sw_str_tmp.substr(1, val_len - 1)) < CPN_MAX_LOGICAL_SWITCHES) {
rhs = RawSwitch(SWITCH_TYPE_VIRTUAL,
std::stoi(sw_str_tmp.substr(1, val_len - 1)));
int sw_idx = std::stoi(sw_str_tmp.substr(1, val_len - 1));
if (sw_idx < CPN_MAX_LOGICAL_SWITCHES) {
rhs = RawSwitch(SWITCH_TYPE_VIRTUAL, sw_idx);
}
} 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' &&
(val[1] >= '0' && val[1] <= '9')) {
if (std::stoi(sw_str_tmp.substr(1, val_len - 1)) < CPN_MAX_SENSORS) {
rhs = RawSwitch(SWITCH_TYPE_SENSOR,
std::stoi(sw_str_tmp.substr(1, val_len - 1)) - 1);
// starts at T1
int sensor_idx = std::stoi(sw_str_tmp.substr(1, val_len - 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")) {
@@ -50,10 +50,12 @@ const YamlLookupTable sliderConfigLut = {
int YamlStickLookup::name2idx(const std::string& name)
{
int idx = std::stoi(name);
if (idx < 4) {
return idx;
}
try {
int idx = std::stoi(name);
if (idx < 4) {
return idx;
}
} catch(...) {}
return -1;
}