commit 2a2f1ced2e2e40f7efaa499a5d619bcfb33e2954
parent 72323d78aeb57be20b4058e4f57256af1cbdccf8
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 25 Dec 2020 10:31:45 -0500
check: prevent bindings of undefined or zero size
Diffstat:
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/include/types.h b/include/types.h
@@ -37,8 +37,6 @@ enum type_storage {
TYPE_STORAGE_UNION,
};
-#define SIZE_UNDEFINED ((size_t)-1)
-
struct type;
enum variadism {
@@ -76,6 +74,9 @@ enum type_flags {
TYPE_CONST = 1 << 0,
};
+#define SIZE_UNDEFINED ((size_t)-1)
+#define ALIGN_UNDEFINED ((size_t)-1)
+
struct type {
enum type_storage storage;
unsigned int flags;
diff --git a/src/check.c b/src/check.c
@@ -168,6 +168,8 @@ check_expr_binding(struct context *ctx,
type = type_store_lookup_with_flags(&ctx->store,
initializer->result, abinding->flags);
}
+ expect(type->size != 0 && type->size != SIZE_UNDEFINED,
+ "Cannot create binding for type of zero or undefined size");
// TODO: Check assignability of initializer
diff --git a/src/type_store.c b/src/type_store.c
@@ -342,6 +342,8 @@ type_init_from_atype(struct type_store *store,
case TYPE_STORAGE_ENUM:
assert(0); // TODO
case TYPE_STORAGE_FUNCTION:
+ type->size = SIZE_UNDEFINED;
+ type->align = SIZE_UNDEFINED;
type->func.result =
type_store_lookup_atype(store, atype->func.result);
type->func.variadism = atype->func.variadism;
@@ -355,6 +357,8 @@ type_init_from_atype(struct type_store *store,
}
break;
case TYPE_STORAGE_POINTER:
+ type->size = 8; // XXX: ARCH
+ type->align = 8;
type->pointer.flags = atype->pointer.flags;
type->pointer.referent = type_store_lookup_atype(
store, atype->pointer.referent);
@@ -404,6 +408,8 @@ type_init_from_type(struct type_store *store,
case TYPE_STORAGE_FUNCTION:
assert(0); // TODO
case TYPE_STORAGE_POINTER:
+ new->size = 8; // XXX: ARCH
+ new->align = 8;
new->pointer.flags = old->pointer.flags;
new->pointer.referent = type_store_lookup_type(
store, old->pointer.referent);