Skip to content

Commit

Permalink
Validate Zvl ISA string correctly
Browse files Browse the repository at this point in the history
See #1810 for explanation of how this can go wrong.

Resolves #1810
  • Loading branch information
aswaterman committed Sep 20, 2024
1 parent bfe9173 commit 881d13b
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions disasm/isa_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ static std::string strtolower(const char* str)
return res;
}

static unsigned long safe_stoul(const std::string& s)
{
int old_errno = errno;
errno = 0;

char* endp;
unsigned long ret = strtoul(s.c_str(), &endp, 10);

int new_errno = errno;
errno = old_errno;

if (endp == s.c_str() || *endp)
throw std::invalid_argument("stoul");

if (new_errno)
throw std::out_of_range("stoul");

return ret;
}

static void bad_option_string(const char *option, const char *value,
const char *msg)
{
Expand Down Expand Up @@ -327,17 +347,17 @@ isa_parser_t::isa_parser_t(const char* str, const char *priv)
} else if (ext_str.substr(0, 3) == "zvl") {
reg_t new_vlen;
try {
new_vlen = std::stol(ext_str.substr(3, ext_str.size() - 4));
new_vlen = safe_stoul(ext_str.substr(3, ext_str.size() - 4));
} catch (std::logic_error& e) {
new_vlen = 0;
}
if ((new_vlen & (new_vlen - 1)) != 0 || new_vlen < 32)
if ((new_vlen & (new_vlen - 1)) != 0 || new_vlen < 32 || ext_str.back() != 'b')
bad_isa_string(str, ("Invalid Zvl string: " + ext_str).c_str());
vlen = std::max(vlen, new_vlen);
} else if (ext_str.substr(0, 3) == "zve") {
reg_t new_elen;
try {
new_elen = std::stol(ext_str.substr(3, ext_str.size() - 4));
new_elen = safe_stoul(ext_str.substr(3, ext_str.size() - 4));
} catch (std::logic_error& e) {
new_elen = 0;
}
Expand Down

0 comments on commit 881d13b

Please sign in to comment.