Improve error message of index assignment expression failures

This commit is contained in:
Jon Siwek 2018-11-02 16:38:55 -05:00
parent 802b4f876e
commit a7ba44089b
3 changed files with 62 additions and 2 deletions

View file

@ -3119,12 +3119,40 @@ void IndexExpr::Assign(Frame* f, Val* v, Opcode op)
switch ( v1->Type()->Tag() ) {
case TYPE_VECTOR:
if ( ! v1->AsVectorVal()->Assign(v2, v, op) )
Internal("assignment failed");
{
if ( v )
{
ODesc d;
v->Describe(&d);
auto vt = v->Type();
auto vtt = vt->Tag();
std::string tn = vtt == TYPE_RECORD ? vt->GetName() : type_name(vtt);
Internal(fmt(
"vector index assignment failed for invalid type '%s', value: %s",
tn.data(), d.Description()));
}
else
Internal("assignment failed with null value");
}
break;
case TYPE_TABLE:
if ( ! v1->AsTableVal()->Assign(v2, v, op) )
Internal("assignment failed");
{
if ( v )
{
ODesc d;
v->Describe(&d);
auto vt = v->Type();
auto vtt = vt->Tag();
std::string tn = vtt == TYPE_RECORD ? vt->GetName() : type_name(vtt);
Internal(fmt(
"table index assignment failed for invalid type '%s', value: %s",
tn.data(), d.Description()));
}
else
Internal("assignment failed with null value");
}
break;
case TYPE_STRING:

View file

@ -0,0 +1 @@
internal error in /Users/jon/projects/bro/bro/scripts/base/utils/queue.bro, line 152: vector index assignment failed for invalid type 'myrec', value: [a=T, b=hi, c=<uninitialized>] (Queue::ret[Queue::j])

View file

@ -0,0 +1,31 @@
# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
@load base/utils/queue
global q: Queue::Queue = Queue::init();
type myrec: record {
a: bool &default=T;
b: string &default="hi";
c: string &optional;
};
function foo(mr: myrec)
{
print mr$a;
print mr$c;
print mr$b;
}
event bro_init()
{
Queue::put(q, "hello");
Queue::put(q, "goodbye");
Queue::put(q, "test");
Queue::put(q, myrec());
local rval: vector of string = vector();
Queue::get_vector(q, rval);
print rval;
}