[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: assert() inside type x = struct { ...
From: |
apache2 |
Subject: |
Re: assert() inside type x = struct { ... |
Date: |
Sat, 9 Apr 2022 13:46:15 +0200 |
User-agent: |
Mutt/1.9.3 (2018-01-21) |
On Fri, Apr 08, 2022 at 05:12:24PM +0200, Jose E. Marchesi wrote:
>
> > Is it intentional that assert() does not work inside type definitions like
> > this?
> >
> > type foo = struct { string yo; };
> > type x = struct {
> > uint<8> len;
> > union {
> > foo myfoo;
> > byte[len] unknown;
> > } payload;
> > assert(payload'size'magnitude/8 == len);
> > };
>
> Statements inside struct types are not supported, no.
> Only declarations.
So what would be the idiomatic way to achieve this?
It looks like I can call it from inside a lambda in a constraint like this:
#!!# type foo = struct { string yo; };
#!!# type x = struct {
uint<8> len;
union {
foo myfoo;
byte[len] unknown;
} payload : (lambda int:{assert(payload'size'magnitude/8 == len);return
1;})();
}
#!!# var r = open("*data*");
#!!# var z = byte[] @ r : 0#b;
#!!# z[0]=1;
#!!# x @ r : 0#b;
x {
len=0x1UB,
payload=struct {
myfoo=foo {
yo=""
}
}
}
This way I can get a crash (from the unhandled assertion failure) instead of a
mere
constraint violation, so e.g. this will fail rather than decode as "int yy":
#!!# type ee = union { x xx; int yy; };
#!!# ee @ r : 0#b;
unhandled assertion failure exception
<stdin>:6:28:
Another way to trigger that lambda without exposing it could be to capture the
result
in a variable I guess.