Skip to content

Commit

Permalink
push_valtype/pop_valtype: refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed May 7, 2024
1 parent ec41fd3 commit 221e470
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
int
push_valtype(enum valtype type, struct validation_context *ctx)
{
assert(ctx->cframes.lsize > 0);
const struct ctrlframe *cframe = &VEC_LASTELEM(ctx->cframes);
const bool unreachable = cframe->unreachable;
int ret;

assert(type != TYPE_ANYREF);
Expand All @@ -31,15 +34,13 @@ push_valtype(enum valtype type, struct validation_context *ctx)
* select ;; here
* end
*/
assert(ctx->cframes.lsize > 0);
const struct ctrlframe *cframe = &VEC_LASTELEM(ctx->cframes);
assert(type != TYPE_UNKNOWN || cframe->unreachable);
assert(type != TYPE_UNKNOWN || unreachable);
ret = VEC_PREALLOC(ctx->valtypes, 1);
if (ret != 0) {
return ret;
}
*VEC_PUSH(ctx->valtypes) = type;
if (!cframe->unreachable) {
if (!unreachable) {
ctx->ncells += valtype_cellsize(type);
if (ctx->ncells > ctx->ei->maxcells) {
ctx->ei->maxcells = ctx->ncells;
Expand All @@ -52,24 +53,24 @@ int
pop_valtype(enum valtype expected_type, enum valtype *typep,
struct validation_context *ctx)
{
const struct ctrlframe *cframe;

assert(ctx->cframes.lsize > 0);
cframe = &VEC_LASTELEM(ctx->cframes);
const struct ctrlframe *cframe = &VEC_LASTELEM(ctx->cframes);
const bool unreachable = cframe->unreachable;

assert(ctx->valtypes.lsize >= cframe->height);
assert(ctx->ncells >= cframe->height_cell);
if (ctx->valtypes.lsize == cframe->height) {
assert(ctx->ncells == cframe->height_cell);
if (cframe->unreachable) {
if (unreachable) {
*typep = TYPE_UNKNOWN;
return 0;
}
return EINVAL;
}
enum valtype t = *VEC_POP(ctx->valtypes);
assert(t != TYPE_ANYREF);
assert(t != TYPE_UNKNOWN || cframe->unreachable);
if (!cframe->unreachable) {
assert(t != TYPE_UNKNOWN || unreachable);
if (!unreachable) {
uint32_t csz = valtype_cellsize(t);
assert(ctx->ncells >= csz);
ctx->ncells -= csz;
Expand Down

0 comments on commit 221e470

Please sign in to comment.