Merge remote-tracking branch 'origin/topic/jsiwek/coverity'

* origin/topic/jsiwek/coverity:
  Fix uninitialized (or unused) fields.
  Remove logically dead code.
  Remove dead/unfinished code in unary not expr.
  Fix logic for failed DNS TXT lookups.
  A couple null ptr checks.
  Improve return value checking and error handling.
  Remove unused variable assignments, dead code.
  Prevent division/modulo by zero in scripts.
  Fix unintentional always-false condition.
  Fix invalidated iterator usage.
  Fix DNS_Mgr iterator mismatch.
  Set safe umask when creating script profiler tmp files.
  Fix nesting/indent level whitespace mismatch.
  Add checks to avoid improper negative values use.

BIT-1085 #merged
This commit is contained in:
Robin Sommer 2013-10-02 11:03:29 -07:00
commit d127d8d01d
86 changed files with 517 additions and 242 deletions

View file

@ -779,9 +779,58 @@ Val* BinaryExpr::Fold(Val* v1, Val* v2) const
case EXPR_SUB: DO_FOLD(-); break;
case EXPR_REMOVE_FROM: DO_FOLD(-); break;
case EXPR_TIMES: DO_FOLD(*); break;
case EXPR_DIVIDE: DO_FOLD(/); break;
case EXPR_DIVIDE:
{
if ( is_integral )
{
if ( i2 == 0 )
reporter->ExprRuntimeError(this, "division by zero");
i3 = i1 / i2;
}
else if ( is_unsigned )
{
if ( u2 == 0 )
reporter->ExprRuntimeError(this, "division by zero");
u3 = u1 / u2;
}
else
{
if ( d2 == 0 )
reporter->ExprRuntimeError(this, "division by zero");
d3 = d1 / d2;
}
}
break;
case EXPR_MOD:
{
if ( is_integral )
{
if ( i2 == 0 )
reporter->ExprRuntimeError(this, "modulo by zero");
i3 = i1 % i2;
}
else if ( is_unsigned )
{
if ( u2 == 0 )
reporter->ExprRuntimeError(this, "modulo by zero");
u3 = u1 % u2;
}
else
Internal("bad type in BinaryExpr::Fold");
}
break;
case EXPR_MOD: DO_INT_FOLD(%); break;
case EXPR_AND: DO_INT_FOLD(&&); break;
case EXPR_OR: DO_INT_FOLD(||); break;
@ -1090,15 +1139,10 @@ NotExpr::NotExpr(Expr* arg_op) : UnaryExpr(EXPR_NOT, arg_op)
return;
BroType* t = op->Type();
if ( IsVector(t->Tag()) )
t = t->AsVectorType()->YieldType();
TypeTag bt = t->Tag();
if ( ! IsIntegral(bt) && bt != TYPE_BOOL )
ExprError("requires an integral or boolean operand");
else if ( IsVector(bt) )
SetType(new VectorType(base_type(TYPE_BOOL)));
else
SetType(base_type(TYPE_BOOL));
}
@ -1112,7 +1156,7 @@ Expr* NotExpr::DoSimplify()
// !!x == x
return ((NotExpr*) op)->Op()->Ref();
if ( op->IsConst() && ! is_vector(op->ExprVal()) )
if ( op->IsConst() )
return new ConstExpr(Fold(op->ExprVal()));
return this;
@ -2438,6 +2482,7 @@ AssignExpr::AssignExpr(Expr* arg_op1, Expr* arg_op2, int arg_is_init,
: BinaryExpr(EXPR_ASSIGN,
arg_is_init ? arg_op1 : arg_op1->MakeLvalue(), arg_op2)
{
val = 0;
is_init = arg_is_init;
if ( IsError() )
@ -3137,12 +3182,14 @@ FieldExpr::FieldExpr(Expr* arg_op, const char* arg_field_name)
{
RecordType* rt = op->Type()->AsRecordType();
field = rt->FieldOffset(field_name);
td = rt->FieldDecl(field);
if ( field < 0 )
ExprError("no such field in record");
else
{
SetType(rt->FieldType(field)->Ref());
td = rt->FieldDecl(field);
}
}
}
@ -3309,14 +3356,14 @@ bool HasFieldExpr::DoSerialize(SerialInfo* info) const
{
DO_SERIALIZE(SER_HAS_FIELD_EXPR, UnaryExpr);
// Serialize the former "bool is_attr" first for backwards compatibility.
// Serialize former "bool is_attr" member first for backwards compatibility.
return SERIALIZE(false) && SERIALIZE(field_name) && SERIALIZE(field);
}
bool HasFieldExpr::DoUnserialize(UnserialInfo* info)
{
DO_UNSERIALIZE(UnaryExpr);
// Unserialize the former "bool is_attr" first for backwards compatibility.
// Unserialize former "bool is_attr" member for backwards compatibility.
bool not_used;
return UNSERIALIZE(&not_used) && UNSERIALIZE_STR(&field_name, 0) && UNSERIALIZE(&field);
}
@ -3325,6 +3372,8 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list,
BroType* arg_type)
: UnaryExpr(EXPR_RECORD_CONSTRUCTOR, constructor_list)
{
ctor_type = 0;
if ( IsError() )
return;
@ -3465,6 +3514,8 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list,
attr_list* arg_attrs, BroType* arg_type)
: UnaryExpr(EXPR_TABLE_CONSTRUCTOR, constructor_list)
{
attrs = 0;
if ( IsError() )
return;
@ -3589,6 +3640,8 @@ SetConstructorExpr::SetConstructorExpr(ListExpr* constructor_list,
attr_list* arg_attrs, BroType* arg_type)
: UnaryExpr(EXPR_SET_CONSTRUCTOR, constructor_list)
{
attrs = 0;
if ( IsError() )
return;
@ -3852,7 +3905,15 @@ void FieldAssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f)
Val* v = op->Eval(f);
if ( v )
rec->Assign(rt->FieldOffset(field_name.c_str()), v);
{
int idx = rt->FieldOffset(field_name.c_str());
if ( idx < 0 )
reporter->InternalError("Missing record field: %s",
field_name.c_str());
rec->Assign(idx, v);
}
}
int FieldAssignExpr::IsRecordElement(TypeDecl* td) const
@ -5386,7 +5447,7 @@ TraversalCode ListExpr::Traverse(TraversalCallback* cb) const
loop_over_list(exprs, i)
{
exprs[i]->Traverse(cb);
tc = exprs[i]->Traverse(cb);
HANDLE_TC_EXPR_PRE(tc);
}