diff --git a/policy/irc-bot.bro b/policy/irc-bot.bro index 8375aa91d6..4bbe072b7d 100644 --- a/policy/irc-bot.bro +++ b/policy/irc-bot.bro @@ -500,7 +500,7 @@ event irc_channel_topic(c: connection, channel: string, topic: string) local conn = get_conn(c); local ch = get_channel(conn, channel); - ch$topic_history[|ch$topic_history| + 1] = ch$topic; + ch$topic_history[|ch$topic_history|] = ch$topic; ch$topic = topic; if ( c$id in bot_conns ) diff --git a/policy/portmapper.bro b/policy/portmapper.bro index 99ce096ee0..a67f24821b 100644 --- a/policy/portmapper.bro +++ b/policy/portmapper.bro @@ -295,8 +295,8 @@ function pm_mapping_to_text(server: addr, m: pm_mappings): string if ( [prog, p] !in mapping_seen ) { add mapping_seen[prog, p]; - addls[++num_addls] = fmt("%s -> %s", rpc_prog(prog), p); - + addls[num_addls] = fmt("%s -> %s", rpc_prog(prog), p); + ++num_addls; update_RPC_server_map(server, p, rpc_prog(prog)); } } diff --git a/policy/scan.bro b/policy/scan.bro index c1e956125b..d3ee0574c3 100644 --- a/policy/scan.bro +++ b/policy/scan.bro @@ -186,17 +186,17 @@ export { # More precisely, the counter is the next index of threshold vector. global shut_down_thresh_reached: table[addr] of bool &default=F; global rb_idx: table[addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; global rps_idx: table[addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; global rops_idx: table[addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; global rpts_idx: table[addr,addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; global rat_idx: table[addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; global rrat_idx: table[addr] of count - &default=1 &read_expire = 1 days &redef; + &default=0 &read_expire = 1 days &redef; } global thresh_check: function(v: vector of count, idx: table[addr] of count, @@ -609,7 +609,7 @@ function thresh_check(v: vector of count, idx: table[addr] of count, return F; } - if ( idx[orig] <= |v| && n >= v[idx[orig]] ) + if ( idx[orig] < |v| && n >= v[idx[orig]] ) { ++idx[orig]; return T; @@ -628,7 +628,7 @@ function thresh_check_2(v: vector of count, idx: table[addr, addr] of count, return F; } - if ( idx[orig,resp] <= |v| && n >= v[idx[orig, resp]] ) + if ( idx[orig,resp] < |v| && n >= v[idx[orig, resp]] ) { ++idx[orig,resp]; return T; diff --git a/src/Expr.cc b/src/Expr.cc index d70e7573a5..c3589facca 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -494,8 +494,7 @@ Val* UnaryExpr::Eval(Frame* f) const VectorVal* v_op = v->AsVectorVal(); VectorVal* result = new VectorVal(Type()->AsVectorType()); - for ( unsigned int i = VECTOR_MIN; - i < v_op->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < v_op->Size(); ++i ) { Val* v_i = v_op->Lookup(i); result->Assign(i, v_i ? Fold(v_i) : 0, this); @@ -633,8 +632,7 @@ Val* BinaryExpr::Eval(Frame* f) const VectorVal* v_result = new VectorVal(Type()->AsVectorType()); - for ( unsigned int i = VECTOR_MIN; - i < v_op1->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < v_op1->Size(); ++i ) { if ( v_op1->Lookup(i) && v_op2->Lookup(i) ) v_result->Assign(i, @@ -656,8 +654,7 @@ Val* BinaryExpr::Eval(Frame* f) const VectorVal* vv = (is_vec1 ? v1 : v2)->AsVectorVal(); VectorVal* v_result = new VectorVal(Type()->AsVectorType()); - for ( unsigned int i = VECTOR_MIN; - i < vv->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < vv->Size(); ++i ) { Val* vv_i = vv->Lookup(i); if ( vv_i ) @@ -1063,8 +1060,7 @@ Val* IncrExpr::Eval(Frame* f) const if ( is_vector(v) ) { VectorVal* v_vec = v->AsVectorVal(); - for ( unsigned int i = VECTOR_MIN; - i < v_vec->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < v_vec->Size(); ++i ) { Val* elt = v_vec->Lookup(i); if ( elt ) @@ -1941,7 +1937,7 @@ Val* BoolExpr::Eval(Frame* f) const { result = new VectorVal(Type()->AsVectorType()); result->Resize(vector_v->Size()); - result->AssignRepeat(VECTOR_MIN, result->Size(), + result->AssignRepeat(0, result->Size(), scalar_v, this); } else @@ -1970,8 +1966,7 @@ Val* BoolExpr::Eval(Frame* f) const VectorVal* result = new VectorVal(Type()->AsVectorType()); result->Resize(vec_v1->Size()); - for ( unsigned int i = VECTOR_MIN; - i < vec_v1->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < vec_v1->Size(); ++i ) { Val* op1 = vec_v1->Lookup(i); Val* op2 = vec_v2->Lookup(i); @@ -2353,7 +2348,7 @@ Val* CondExpr::Eval(Frame* f) const VectorVal* result = new VectorVal(Type()->AsVectorType()); result->Resize(cond->Size()); - for ( unsigned int i = VECTOR_MIN; i < cond->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < cond->Size(); ++i ) { Val* local_cond = cond->Lookup(i); if ( local_cond ) @@ -2951,8 +2946,7 @@ Val* IndexExpr::Eval(Frame* f) const return 0; } - for ( unsigned int i = VECTOR_MIN; - i < v_v2->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < v_v2->Size(); ++i ) { if ( v_v2->Lookup(i)->AsBool() ) v_result->Assign(v_result->Size() + 1, v_v1->Lookup(i), this); @@ -2964,8 +2958,7 @@ Val* IndexExpr::Eval(Frame* f) const // S does, i.e., by excluding those elements. // Probably only do this if *all* are negative. v_result->Resize(v_v2->Size()); - for ( unsigned int i = VECTOR_MIN; - i < v_v2->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < v_v2->Size(); ++i ) v_result->Assign(i, v_v1->Lookup(v_v2->Lookup(i)->CoerceToInt()), this); } } @@ -3551,9 +3544,9 @@ Val* VectorConstructorExpr::Eval(Frame* f) const { Expr* e = exprs[i]; Val* v = e->Eval(f); - if ( ! vec->Assign(i + VECTOR_MIN, v, e) ) + if ( ! vec->Assign(i, v, e) ) { - Error(fmt("type mismatch at index %d", i + VECTOR_MIN), e); + Error(fmt("type mismatch at index %d", i), e); return 0; } } @@ -3575,9 +3568,9 @@ Val* VectorConstructorExpr::InitVal(const BroType* t, Val* aggr) const Expr* e = exprs[i]; Val* v = check_and_promote(e->Eval(0), vt, 1); - if ( ! v || ! vec->Assign(i + VECTOR_MIN, v, e) ) + if ( ! v || ! vec->Assign(i, v, e) ) { - Error(fmt("initialization type mismatch at index %d", i + VECTOR_MIN), e); + Error(fmt("initialization type mismatch at index %d", i), e); return 0; } } @@ -3936,7 +3929,7 @@ Val* ArithCoerceExpr::Fold(Val* v) const VectorVal* vv = v->AsVectorVal(); VectorVal* result = new VectorVal(Type()->AsVectorType()); - for ( unsigned int i = VECTOR_MIN; i < vv->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < vv->Size(); ++i ) { Val* elt = vv->Lookup(i); if ( elt ) @@ -5053,9 +5046,9 @@ Val* ListExpr::InitVal(const BroType* t, Val* aggr) const { Expr* e = exprs[i]; Val* v = e->Eval(0); - if ( ! vec->Assign(i + VECTOR_MIN, v, e) ) + if ( ! vec->Assign(i, v, e) ) { - e->Error(fmt("type mismatch at index %d", i + VECTOR_MIN)); + e->Error(fmt("type mismatch at index %d", i)); return 0; } diff --git a/src/LogMgr.cc b/src/LogMgr.cc index 757db2183e..165f954dc4 100644 --- a/src/LogMgr.cc +++ b/src/LogMgr.cc @@ -1051,7 +1051,7 @@ LogVal* LogMgr::ValToLogVal(Val* val, BroType* ty) for ( int i = 0; i < lval->val.vector_val.size; i++ ) { lval->val.vector_val.vals[i] = - ValToLogVal(vec->Lookup(VECTOR_MIN + i), + ValToLogVal(vec->Lookup(i), vec->Type()->YieldType()); } diff --git a/src/Val.cc b/src/Val.cc index 965ea3d026..cf9ee031fd 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -3231,15 +3231,6 @@ bool VectorVal::Assign(unsigned int index, Val* element, const Expr* assigner, return false; } - if ( index == 0 || index > (1 << 30) ) - { - if ( assigner ) - assigner->Error(fmt("index (%d) must be positive", - index)); - Unref(element); - return true; // true = "no fatal error" - } - BroType* yt = Type()->AsVectorType()->YieldType(); if ( yt && yt->Tag() == TYPE_TABLE && @@ -3254,7 +3245,7 @@ bool VectorVal::Assign(unsigned int index, Val* element, const Expr* assigner, Val* ival = new Val(index, TYPE_COUNT); StateAccess::Log(new StateAccess(OP_ASSIGN_IDX, this, ival, element, - (*val.vector_val)[index - 1])); + (*val.vector_val)[index])); Unref(ival); } @@ -3264,10 +3255,10 @@ bool VectorVal::Assign(unsigned int index, Val* element, const Expr* assigner, } } - if ( index <= val.vector_val->size() ) - Unref((*val.vector_val)[index - 1]); + if ( index < val.vector_val->size() ) + Unref((*val.vector_val)[index]); else - val.vector_val->resize(index); + val.vector_val->resize(index + 1); if ( LoggingAccess() && op != OP_NONE ) { @@ -3278,14 +3269,14 @@ bool VectorVal::Assign(unsigned int index, Val* element, const Expr* assigner, StateAccess::Log(new StateAccess(op == OP_INCR ? OP_INCR_IDX : OP_ASSIGN_IDX, - this, ival, element, (*val.vector_val)[index - 1])); + this, ival, element, (*val.vector_val)[index])); Unref(ival); } // Note: we do *not* Ref() the element, if any, at this point. // AssignExpr::Eval() already does this; other callers must remember // to do it similarly. - (*val.vector_val)[index - 1] = element; + (*val.vector_val)[index] = element; Modified(); return true; @@ -3294,7 +3285,7 @@ bool VectorVal::Assign(unsigned int index, Val* element, const Expr* assigner, bool VectorVal::AssignRepeat(unsigned int index, unsigned int how_many, Val* element, const Expr* assigner) { - ResizeAtLeast(index + how_many - 1); + ResizeAtLeast(index + how_many); for ( unsigned int i = index; i < index + how_many; ++i ) if ( ! Assign(i, element, assigner) ) @@ -3306,10 +3297,10 @@ bool VectorVal::AssignRepeat(unsigned int index, unsigned int how_many, Val* VectorVal::Lookup(unsigned int index) const { - if ( index == 0 || index > val.vector_val->size() ) + if ( index >= val.vector_val->size() ) return 0; - return (*val.vector_val)[index - 1]; + return (*val.vector_val)[index]; } unsigned int VectorVal::Resize(unsigned int new_num_elements) @@ -3398,7 +3389,7 @@ bool VectorVal::DoUnserialize(UnserialInfo* info) { Val* v; UNSERIALIZE_OPTIONAL(v, Val::Unserialize(info, TYPE_ANY)); - Assign(i + VECTOR_MIN, v, 0); + Assign(i, v, 0); } return true; diff --git a/src/Val.h b/src/Val.h index 2dede2364e..76edea5429 100644 --- a/src/Val.h +++ b/src/Val.h @@ -969,9 +969,6 @@ protected: }; -// The minimum index for vectors (0 or 1). -const int VECTOR_MIN = 1; - class VectorVal : public MutableVal { public: VectorVal(VectorType* t); diff --git a/src/bro.bif b/src/bro.bif index b3280769e1..1d2c159f65 100644 --- a/src/bro.bif +++ b/src/bro.bif @@ -2395,7 +2395,7 @@ function any_set%(v: any%) : bool } VectorVal* vv = v->AsVectorVal(); - for ( unsigned int i = VECTOR_MIN; i < vv->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < vv->Size(); ++i ) if ( vv->Lookup(i) && vv->Lookup(i)->AsBool() ) return new Val(true, TYPE_BOOL); @@ -2413,7 +2413,7 @@ function all_set%(v: any%) : bool } VectorVal* vv = v->AsVectorVal(); - for ( unsigned int i = VECTOR_MIN; i < vv->Size() + VECTOR_MIN; ++i ) + for ( unsigned int i = 0; i < vv->Size(); ++i ) if ( ! vv->Lookup(i) || ! vv->Lookup(i)->AsBool() ) return new Val(false, TYPE_BOOL); @@ -2596,8 +2596,8 @@ function order%(v: any, ...%) : index_vec // adjusting indices as we do so. for ( i = 0; i < n; ++i ) { - int ind = ind_vv[i] + VECTOR_MIN; - result_v->Assign(i + VECTOR_MIN, new Val(ind, TYPE_COUNT), 0); + int ind = ind_vv[i]; + result_v->Assign(i, new Val(ind, TYPE_COUNT), 0); } return result_v; diff --git a/testing/btest/istate/sync.bro b/testing/btest/istate/sync.bro index d537ca10bc..639c1b2dfe 100644 --- a/testing/btest/istate/sync.bro +++ b/testing/btest/istate/sync.bro @@ -114,9 +114,9 @@ function modify() foo15 = 6667/tcp; - foo16[4] = 4; - foo16[2] = 20; - ++foo16[1]; + foo16[3] = 4; + foo16[1] = 20; + ++foo16[0]; local x: type1; x$a = "pop"; diff --git a/testing/btest/logging/vec.bro b/testing/btest/logging/vec.bro index 8d882d53bb..b9ed8f8d6d 100644 --- a/testing/btest/logging/vec.bro +++ b/testing/btest/logging/vec.bro @@ -18,8 +18,8 @@ event bro_init() local v: vector of string; - v[2] = "2"; - v[5] = "5"; + v[1] = "2"; + v[4] = "5"; Log::write(SSH, [$vec=v]); }