Merge remote-tracking branch 'origin/topic/awelzel/3640-void-size-expr-assign-stmt'

* origin/topic/awelzel/3640-void-size-expr-assign-stmt:
  Expr: Handle TYPE_VOID in SizeExpr and AssignExpr::Typecheck()
This commit is contained in:
Arne Welzel 2024-03-12 10:31:19 +01:00
commit 2d1588277e
11 changed files with 79 additions and 3 deletions

10
CHANGES
View file

@ -1,3 +1,13 @@
7.0.0-dev.54 | 2024-03-12 10:31:19 +0100
* GH-3640: Expr: Handle TYPE_VOID in SizeExpr and AssignExpr::Typecheck() (Arne Welzel, Corelight)
@vpax reported surprising behavior when working with "void values".
While these are not exposed to script land, plumb the places he
pointed out are causing confusing behavior.
Closes #3640.
7.0.0-dev.52 | 2024-03-08 19:10:06 +0100
* AST location fixes for -O gen-C++ (Vern Paxson, Corelight)

View file

@ -1 +1 @@
7.0.0-dev.52
7.0.0-dev.54

View file

@ -1313,7 +1313,9 @@ SizeExpr::SizeExpr(ExprPtr arg_op) : UnaryExpr(EXPR_SIZE, std::move(arg_op)) {
auto& t = op->GetType();
if ( t->Tag() == TYPE_ANY )
if ( t->Tag() == TYPE_VOID )
SetError("cannot take size of void");
else if ( t->Tag() == TYPE_ANY )
SetType(base_type(TYPE_ANY));
else if ( t->Tag() == TYPE_FILE || t->Tag() == TYPE_SUBNET || t->InternalType() == TYPE_INTERNAL_DOUBLE )
SetType(base_type(TYPE_DOUBLE));
@ -2234,6 +2236,11 @@ bool AssignExpr::TypeCheck(const AttributesPtr& attrs) {
TypeTag bt1 = op1->GetType()->Tag();
TypeTag bt2 = op2->GetType()->Tag();
if ( bt2 == TYPE_VOID ) {
ExprError("can't assign void value");
return false;
}
if ( bt1 == TYPE_LIST && bt2 == TYPE_ANY )
// This is ok because we cannot explicitly declare lists on
// the script level.
@ -2271,7 +2278,7 @@ bool AssignExpr::TypeCheck(const AttributesPtr& attrs) {
}
}
if ( op1->GetType()->Tag() == TYPE_RECORD && op2->GetType()->Tag() == TYPE_RECORD ) {
if ( bt1 == TYPE_RECORD && bt2 == TYPE_RECORD ) {
if ( same_type(op1->GetType(), op2->GetType()) )
return true;

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 2: identifier not defined: void

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 1: identifier not defined: void

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 2: return statement cannot have an expression (return (a))

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 4: cannot take size of void (sizeofx[3.4.5.6])

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 2: can't assign void value (y = x[3])

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 5: can't assign void value (z = x())
error in <...>/void-errors.zeek, line 6: value of type void illegal (print x())
error in <...>/void-errors.zeek, line 7: cannot take size of void (sizeofx())

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/void-errors.zeek, line 7: identifier not defined: void

View file

@ -0,0 +1,43 @@
# @TEST-DOC: Zeek does not have a script land void type, but internally it does exist (indexing a set, or a function returning no result). Regression test #3640.
#
# @TEST-EXEC-FAIL: zeek -b %INPUT >output 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output
# identifier void not defined...
global x: void = 1;
# @TEST-START-NEXT
type R: record {
x: void;
};
# @TEST-START-NEXT
function x(): void {
return "a";
};
# @TEST-START-NEXT
function x() {
return "a";
};
# @TEST-START-NEXT
global x = set(3.4.5.6, 9.8.7.6);
print |x|;
print |x[3.4.5.6]|;
# @TEST-START-NEXT
local x = set(5, 3);
local y: any = x[3];
print y;
# @TEST-START-NEXT
function x() {
print "x()";
}
global z: any = x();
print x();
print |x()|;