diff --git a/src/Expr.cc b/src/Expr.cc index c338ef13e2..d5764ad8ee 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4797,8 +4797,7 @@ IntrusivePtr CastExpr::Eval(Frame* f) const if ( ! v ) return 0; - IntrusivePtr nv{AdoptRef{}, cast_value_to_type(v.get(), Type())}; - + auto nv = cast_value_to_type(v.get(), Type()); if ( nv ) return nv; diff --git a/src/Stmt.cc b/src/Stmt.cc index 495a3ed432..0b5a78511f 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -834,7 +834,7 @@ Val* SwitchStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const if ( matching_id ) { auto cv = cast_value_to_type(v, matching_id->Type()); - f->SetElement(matching_id, cv); + f->SetElement(matching_id, cv.release()); } flow = FLOW_NEXT; diff --git a/src/Val.cc b/src/Val.cc index 86ee5ff38e..41dfc3b6b8 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -3206,7 +3206,7 @@ void delete_vals(val_list* vals) } } -Val* cast_value_to_type(Val* v, BroType* t) +IntrusivePtr cast_value_to_type(Val* v, BroType* t) { // Note: when changing this function, adapt all three of // cast_value_to_type()/can_cast_value_to_type()/can_cast_value_to_type(). @@ -3217,7 +3217,7 @@ Val* cast_value_to_type(Val* v, BroType* t) // Always allow casting to same type. This also covers casting 'any' // to the actual type. if ( same_type(v->Type(), t) ) - return v->Ref(); + return {NewRef{}, v}; if ( same_type(v->Type(), bro_broker::DataVal::ScriptDataType()) ) { @@ -3226,7 +3226,7 @@ Val* cast_value_to_type(Val* v, BroType* t) if ( ! dv ) return nullptr; - return static_cast(dv)->castTo(t).release(); + return static_cast(dv)->castTo(t); } return nullptr; diff --git a/src/Val.h b/src/Val.h index 5f32b65935..9e4289a546 100644 --- a/src/Val.h +++ b/src/Val.h @@ -1037,10 +1037,9 @@ extern void delete_vals(val_list* vals); inline bool is_vector(Val* v) { return v->Type()->Tag() == TYPE_VECTOR; } // Returns v casted to type T if the type supports that. Returns null if not. -// The returned value will be ref'ed. // // Note: This implements the script-level cast operator. -extern Val* cast_value_to_type(Val* v, BroType* t); +extern IntrusivePtr cast_value_to_type(Val* v, BroType* t); // Returns true if v can be casted to type T. If so, check_and_cast() will // succeed as well.