commit 4a535032d787fd4f7aeab7327e578621ebcee630
parent d53bd3b6c96effd10dcd4728eb01bda898103c73
Author: Ember Sawady <ecs@d2evs.net>
Date: Mon, 6 Feb 2023 16:34:27 +0000
Don't crash on slice alloc with [*]t initializer
Signed-off-by: Ember Sawady <ecs@d2evs.net>
Diffstat:
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -381,8 +381,13 @@ check_expr_alloc_slice(struct context *ctx,
check_expression(ctx, aexpr->alloc.init, expr->alloc.init, hint);
const struct type *objtype = expr->alloc.init->result;
- if (type_dealias(objtype)->storage != STORAGE_ARRAY
- && type_dealias(objtype)->storage != STORAGE_SLICE) {
+ if (type_dealias(objtype)->storage == STORAGE_ARRAY) {
+ if (type_dealias(objtype)->array.length == SIZE_UNDEFINED) {
+ error(ctx, aexpr->alloc.init->loc, expr,
+ "Slice initializer must have defined length");
+ return;
+ }
+ } else if (type_dealias(objtype)->storage != STORAGE_SLICE) {
error(ctx, aexpr->alloc.init->loc, expr,
"Slice initializer must be of slice or array type, not %s",
type_storage_unparse(type_dealias(objtype)->storage));
diff --git a/tests/26-regression.ha b/tests/26-regression.ha
@@ -110,4 +110,5 @@ export fn main() void = {
) as rt::exited != rt::EXIT_SUCCESS);
assert(rt::compile("def A = x && true;") as rt::exited != rt::EXIT_SUCCESS);
assert(rt::compile("type a = struct { b: fn() void };") as rt::exited != rt::EXIT_SUCCESS);
+ assert(rt::compile("fn a() []int = alloc([]: [*]int, 0);") as rt::exited != rt::EXIT_SUCCESS);
};