commit 95e3f2e9ce8389bb63b49cf728f3d93a3609507a
parent 95aba73afa70bb5d314541b98d003988c96398d6
Author: Karl Schultheisz <k@kdsch.org>
Date: Tue, 26 Apr 2022 12:13:35 -0400
check: reject return statement not in function
Fix segfaults for incorrect code like
let x: [return 0]int;
by failing gracefully if a return statement appears outside of
a function body. The parser accepts this syntax because it only looks
for an expression in the array length, not the subset of expressions
that could be valid array lengths.
Signed-off-by: Karl Schultheisz <k@kdsch.org>
Diffstat:
1 file changed, 4 insertions(+), 0 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -2129,6 +2129,10 @@ check_expr_return(struct context *ctx,
"Cannot return inside a defer expression");
return;
}
+ if (ctx->fntype == NULL) {
+ error(ctx, aexpr->loc, expr, "Cannot return outside a function body");
+ return;
+ }
if (ctx->fntype->func.flags & FN_NORETURN) {
error(ctx, aexpr->loc, expr,
"Cannot return inside @noreturn function");