converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both

This commit is contained in:
2025-10-13 16:39:17 +02:00
parent 98c72efd31
commit c80dc9bdf3
3 changed files with 26 additions and 6 deletions

View File

@@ -1480,13 +1480,20 @@ std::string Caller::define(int action) {
WrongNumberOfParameters(2);
}
if (mode == "reg") {
bool value_found = false;
try {
// get name from address
StringTo<int>(args[1]);
value_found = true;
} catch (...) {
}
// get name from address
if (value_found) {
int addr = StringTo<int>(args[1]);
auto t = det->getRegisterDefinitionByValue(addr);
os << t << '\n';
} catch (...) {
// get address from name
}
// get address from name
else {
auto t = det->getRegisterDefinitionByName(args[1]);
os << ToStringHex(t) << '\n';
}

View File

@@ -2092,7 +2092,7 @@ std::string DetectorImpl::getRegisterDefinitionByValue(const int value) const {
auto val = ctb_shm()->getRegisterName(value);
if (!val.has_value()) {
throw RuntimeError("No register definition found for address: " +
value);
ToStringHex(value));
}
return val.value();
}

View File

@@ -1409,8 +1409,21 @@ TEST_CASE("define", "[.cmdcall]") {
REQUIRE_NOTHROW(
caller.call("define", {"bit", "MICRO_BIT", "4"}, -1, PUT));
std::ostringstream oss, oss1;
caller.call("define", {"reg", "REG_MACRO"}, -1, GET, oss);
caller.call("define", {"bit", "MACRO_BIT"}, -1, GET, oss1);
REQUIRE_NOTHROW(
caller.call("define", {"reg", "REG_MACRO"}, -1, GET, oss));
REQUIRE_NOTHROW(
caller.call("define", {"bit", "MACRO_BIT"}, -1, GET, oss1));
REQUIRE_NOTHROW(caller.call("define", {"reg", "0x202"}, -1, GET));
REQUIRE_THROWS(caller.call("define", {"reg", "0x203"}, -1, GET));
try {
caller.call("define", {"reg", "0x203"}, -1, GET, oss);
} catch (const std::exception &e) {
REQUIRE(std::string(e.what()) ==
"No register definition found for address: 0x203");
}
REQUIRE(oss.str() == "define 0x202\n");
REQUIRE(oss1.str() == "define 2\n");
}