harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 07e888e539e618dd5ccc16c0f0325d74d373a2b5
parent aef98a91675367e7d408d56488f82c2e1c78e29a
Author: Alexey Yerin <yyp@disroot.org>
Date:   Wed, 15 Feb 2023 16:20:20 +0300

check: disallow dereferencing void pointers

The following code used to crash harec:

    export fn main() void = {
    	let a: *void = &12;
    	*a;
    };

Signed-off-by: Alexey Yerin <yyp@disroot.org>

Diffstat:
Msrc/check.c | 5+++++
Mtests/03-pointers.ha | 8++++++++
2 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -3107,6 +3107,11 @@ check_expr_unarithm(struct context *ctx, "Cannot dereference nullable pointer type"); return; } + if (type_dealias(operand->result)->pointer.referent->size == 0) { + error(ctx, aexpr->unarithm.operand->loc, expr, + "Cannot dereference pointer to zero-sized type"); + return; + } if (type_dealias(operand->result)->pointer.referent->size == SIZE_UNDEFINED) { error(ctx, aexpr->unarithm.operand->loc, expr, diff --git a/tests/03-pointers.ha b/tests/03-pointers.ha @@ -128,6 +128,14 @@ fn reject() void = { let b: *int = &a; let c: int = *b; ") as exited != EXIT_SUCCESS); + + // dereference of a zero-sized type + assert(compile(" + fn test() void = { + let a: *void = &12; + *a; + }; + ") as exited != EXIT_SUCCESS); }; export fn main() void = {