Fix |...| operator for inner ports/enums

The `|...|` (sizeof) operator applies to ports/enums. That has returned
a count, but it causes an internal error when used as an inner value in
a record. This fixes that internal error for ports by just making the
sizeof operator return a count.

Enums could technically be negative before this change, but that is
rejected at parse time. It seems reasonable to modify the enum value
to only be non-negative, which makes the sizeof operator easier since it
can just return a count. Changing that to return an int would
potentially break scripts that use the sizeof operator and assign it to
a count.

This could technically break code that internally sets the enum value to
a negative value, but I don't think that's a very likely use.
This commit is contained in:
Evan Typanski 2024-09-11 17:46:42 +02:00
parent ba91de59b0
commit a26d2dd56c
9 changed files with 37 additions and 10 deletions

View file

@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
expression warning in <...>/sizeof.zeek, line 73: count underflow (5 - 9)
expression warning in <...>/sizeof.zeek, line 83: count underflow (5 - 9)

View file

@ -7,11 +7,13 @@ Expr: 18446744073709551612
Signed Expr: 4
Double -1.23: 1.230000
Enum ENUM3: 2
Enum in record: 2
File 21.000000
Function add_interface: 2
Integer -10: 10
Interval -5.0 secs: 5.000000
Port 80/tcp: 65616
Port in record: 65616
Record [i=10, j=<uninitialized>, k=<uninitialized>]: 3
Set: 3
String 'Hello': 5

View file

@ -20,6 +20,14 @@ type example_record: record {
k: int &optional;
};
type example_record_with_enum: record {
e: count &default = |ENUM3|;
} &redef;
type example_record_with_port: record {
p: count &default = |80/tcp|;
} &redef;
global a: addr = 1.2.3.4;
global a6: addr = [::1];
global b: bool = T;
@ -36,6 +44,8 @@ global sn: subnet = 192.168.0.0/24;
global t: table[string] of string;
global ti: time = current_time();
global v: vector of string;
global with_enum: example_record_with_enum;
global with_port: example_record_with_port;
# Additional initialization
#
@ -80,6 +90,9 @@ print fmt("Double %s: %f", d, |d|);
# Size of enum: returns numeric value of enum constant.
print fmt("Enum %s: %d", ENUM3, |ENUM3|);
# Within a record, enum sizeof should still be ok
print fmt("Enum in record: %d", |with_enum$e|);
# Size of file: returns current file size.
# Note that this is a double so that file sizes >> 4GB
# can be expressed.
@ -97,6 +110,9 @@ print fmt("Interval %s: %f", iv, |iv|);
# Size of port: returns port number as a count.
print fmt("Port %s: %d", p, |p|);
# Within a record, port sizeof should still be ok
print fmt("Port in record: %d", |with_port$p|);
# Size of record: returns number of fields (assigned + unassigned)
print fmt("Record %s: %d", r, |r|);