gen-zam: Fix a pile of Coverity findings

This commit is contained in:
Tim Wojtulewicz 2025-07-21 14:07:43 -07:00
parent 3845fced55
commit 526238299d
2 changed files with 24 additions and 19 deletions

View file

@ -661,7 +661,7 @@ static bool skippable_ot(ZAM_OperandClass oc) {
string ZAM_OpTemplate::ExpandParams(const OCVec& oc, string eval, const vector<string>& accessors) const { string ZAM_OpTemplate::ExpandParams(const OCVec& oc, string eval, const vector<string>& accessors) const {
auto have_target = eval.find("$$") != string::npos; auto have_target = eval.find("$$") != string::npos;
auto fl = GetOp1Flavor(); const auto& fl = GetOp1Flavor();
auto need_target = fl == "OP1_WRITE"; auto need_target = fl == "OP1_WRITE";
auto oc_size = oc.size(); auto oc_size = oc.size();
@ -699,16 +699,19 @@ string ZAM_OpTemplate::ExpandParams(const OCVec& oc, string eval, const vector<s
case 4: case 4:
if ( ! has_d4 ) if ( ! has_d4 )
Gripe("eval missing $4", eval); Gripe("eval missing $4", eval);
[[fallthrough]];
case 3: case 3:
if ( ! has_d3 ) if ( ! has_d3 )
Gripe("eval missing $3", eval); Gripe("eval missing $3", eval);
[[fallthrough]];
case 2: case 2:
if ( ! has_d2 ) if ( ! has_d2 )
Gripe("eval missing $2", eval); Gripe("eval missing $2", eval);
[[fallthrough]];
case 1: case 1:
if ( ! has_d1 ) if ( ! has_d1 )
Gripe("eval missing $1", eval); Gripe("eval missing $1", eval);
[[fallthrough]];
case 0: break; case 0: break;
default: Gripe("unexpected param size", to_string(max_param) + " - " + eval); break; default: Gripe("unexpected param size", to_string(max_param) + " - " + eval); break;
@ -718,15 +721,19 @@ string ZAM_OpTemplate::ExpandParams(const OCVec& oc, string eval, const vector<s
case 0: case 0:
if ( has_d1 ) if ( has_d1 )
Gripe("extraneous $1 in eval", eval); Gripe("extraneous $1 in eval", eval);
[[fallthrough]];
case 1: case 1:
if ( has_d2 ) if ( has_d2 )
Gripe("extraneous $2 in eval", eval); Gripe("extraneous $2 in eval", eval);
[[fallthrough]];
case 2: case 2:
if ( has_d3 ) if ( has_d3 )
Gripe("extraneous $3 in eval", eval); Gripe("extraneous $3 in eval", eval);
[[fallthrough]];
case 3: case 3:
if ( has_d4 ) if ( has_d4 )
Gripe("extraneous $4 in eval", eval); Gripe("extraneous $4 in eval", eval);
[[fallthrough]];
case 4: break; case 4: break;
@ -892,8 +899,8 @@ void ZAM_OpTemplate::InstantiateAssignOp(const OCVec& oc, const string& suffix)
EmitTo(AssignFlavor); EmitTo(AssignFlavor);
Emit(flavor_ind + " = empty_map;"); Emit(flavor_ind + " = empty_map;");
auto eval = GetEval(); const auto& eval = GetEval();
auto v = GetAssignVal(); const auto& v = GetAssignVal();
for ( auto& ti : ZAM_type_info ) { for ( auto& ti : ZAM_type_info ) {
auto op = g->GenOpCode(this, op_string + "_" + ti.suffix); auto op = g->GenOpCode(this, op_string + "_" + ti.suffix);
@ -1026,7 +1033,7 @@ void ZAM_OpTemplate::GenAssignOpCore(const OCVec& oc, const string& eval, const
void ZAM_OpTemplate::GenAssignOpValCore(const OCVec& oc, const string& orig_eval, const string& accessor, void ZAM_OpTemplate::GenAssignOpValCore(const OCVec& oc, const string& orig_eval, const string& accessor,
bool is_managed) { bool is_managed) {
auto v = GetAssignVal(); const auto& v = GetAssignVal();
// Maps Zeek types to how to get the underlying value from a ValPtr. // Maps Zeek types to how to get the underlying value from a ValPtr.
static unordered_map<string, string> val_accessors = { static unordered_map<string, string> val_accessors = {
@ -1040,7 +1047,7 @@ void ZAM_OpTemplate::GenAssignOpValCore(const OCVec& oc, const string& orig_eval
{"Record", "->AsRecordVal()"}, {"Type", "->AsTypeVal()"}, {"Record", "->AsRecordVal()"}, {"Type", "->AsTypeVal()"},
}; };
auto val_accessor = val_accessors[accessor]; const auto& val_accessor = val_accessors[accessor];
string rhs; string rhs;
if ( IsInternalOp() ) if ( IsInternalOp() )
@ -1407,17 +1414,15 @@ void ZAM_ExprOpTemplate::GenMethodTest(ZAM_Type et1, ZAM_Type et2, const string&
if ( if_tests.count(et1) == 0 || if_tests.count(et2) == 0 ) if ( if_tests.count(et1) == 0 || if_tests.count(et2) == 0 )
Gripe("bad op-type"); Gripe("bad op-type");
auto if_test1 = if_tests[et1]; const auto& [var, val] = if_tests[et1];
auto if_var1 = if_test1.first + "1"; auto if_var1 = var + "1";
auto if_val1 = if_test1.second;
string test = if_var1 + " == " + if_val1; string test = if_var1 + " == " + val;
if ( Arity() > 1 ) { if ( Arity() > 1 ) {
auto if_test2 = if_tests[et2]; const auto& [var2, val2] = if_tests[et2];
auto if_var2 = if_test2.first + "2"; auto if_var2 = var2 + "2";
auto if_val2 = if_test2.second; test = test + " && " + if_var2 + " == " + val2;
test = test + " && " + if_var2 + " == " + if_val2;
} }
test = "if ( " + test + " )"; test = "if ( " + test + " )";
@ -2032,7 +2037,7 @@ void ZAM_InternalOpTemplate::ParseCall(const string& line, const Words& words) {
eval += "ZAM_PROFILE_PRE_CALL\n"; eval += "ZAM_PROFILE_PRE_CALL\n";
if ( HasAssignVal() ) { if ( HasAssignVal() ) {
auto av = GetAssignVal(); const auto& av = GetAssignVal();
eval += "auto " + av + " = " + func + "->Invoke(&args, f);\n"; eval += "auto " + av + " = " + func + "->Invoke(&args, f);\n";
eval += "if ( ! " + av + " ) { ZAM_error = true; break; }\n"; eval += "if ( ! " + av + " ) { ZAM_error = true; break; }\n";
@ -2393,14 +2398,14 @@ bool ZAMGen::ParseTemplate() {
if ( words.size() < 2 ) if ( words.size() < 2 )
Gripe("too few words at start of template", line); Gripe("too few words at start of template", line);
auto op = words[0]; const auto& op = words[0];
if ( op == "macro" ) { if ( op == "macro" ) {
ReadMacro(line); ReadMacro(line);
return true; return true;
} }
auto op_name = words[1]; const auto& op_name = words[1];
// We track issues with the wrong number of template arguments // We track issues with the wrong number of template arguments
// up front, to avoid mis-invoking constructors, but we don't // up front, to avoid mis-invoking constructors, but we don't

View file

@ -370,8 +370,8 @@ protected:
// operation that explictly omits any assignment. // operation that explictly omits any assignment.
bool HasAssignmentLess() const { return ! assignment_less_op.empty(); } bool HasAssignmentLess() const { return ! assignment_less_op.empty(); }
void SetAssignmentLess(string op, string op_class) { void SetAssignmentLess(string op, string op_class) {
assignment_less_op = op; assignment_less_op = std::move(op);
assignment_less_op_class = op_class; assignment_less_op_class = std::move(op_class);
} }
const string& AssignmentLessOp() const { return assignment_less_op; } const string& AssignmentLessOp() const { return assignment_less_op; }
const string& AssignmentLessOpClass() const { return assignment_less_op_class; } const string& AssignmentLessOpClass() const { return assignment_less_op_class; }