From 9dc874288cb6dcfa1b09411f70cc44f5c532e067 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Sun, 4 Dec 2022 21:38:17 -1000 Subject: [PATCH] Avoid use of sprintf in disassembler None of these cases are perf-critical. It was easy to change one of them to use std::string, but the others would have required more refactoring. So, simply change them to use snprintf instead. --- disasm/disasm.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/disasm/disasm.cc b/disasm/disasm.cc index 856a651c..f522df9d 100644 --- a/disasm/disasm.cc +++ b/disasm/disasm.cc @@ -1289,14 +1289,12 @@ void disassembler_t::add_instructions(const isa_parser_t* isa) 0x10000000, 0x10005000, 0x10006000, 0x10007000}; for (unsigned nf = 0; nf <= 7; ++nf) { - char seg_str[8] = ""; - if (nf) - sprintf(seg_str, "seg%u", nf + 1); + const auto seg_str = nf ? "seg" + std::to_string(nf + 1) : ""; for (auto item : template_insn) { const reg_t match_nf = nf << 29; char buf[128]; - sprintf(buf, item.fmt, seg_str, 8 << elt); + snprintf(buf, sizeof(buf), item.fmt, seg_str.c_str(), 8 << elt); add_insn(new disasm_insn_t( buf, ((item.match | match_nf) & ~mask_vldst) | elt_map[elt], @@ -1314,7 +1312,7 @@ void disassembler_t::add_instructions(const isa_parser_t* isa) for (auto item : template_insn2) { const reg_t match_nf = nf << 29; char buf[128]; - sprintf(buf, item.fmt, nf + 1, 8 << elt); + snprintf(buf, sizeof(buf), item.fmt, nf + 1, 8 << elt); add_insn(new disasm_insn_t( buf, item.match | match_nf | elt_map[elt], @@ -1675,7 +1673,7 @@ void disassembler_t::add_instructions(const isa_parser_t* isa) for (size_t idx = 0; idx < sizeof(amo_map) / sizeof(amo_map[0]); ++idx) { for (auto item : template_insn) { char buf[128]; - sprintf(buf, item.fmt, amo_map[idx].first, 8 << elt); + snprintf(buf, sizeof(buf), item.fmt, amo_map[idx].first, 8 << elt); add_insn(new disasm_insn_t(buf, item.match | amo_map[idx].second | elt_map[elt], item.mask,