From b19cd0b87df490635481ccceb6002efa2d744be4 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 5 Jul 2025 01:38:44 +0200 Subject: [PATCH] Casting a fault to a pointer would trigger an assert. --- releasenotes.md | 1 + src/compiler/compiler_internal.h | 9 +++------ src/compiler/diagnostics.c | 1 - src/compiler/enums.h | 2 +- src/compiler/headers.c | 1 - src/compiler/sema_casts.c | 3 ++- src/compiler/sema_expr.c | 2 +- src/utils/json.c | 2 -- 8 files changed, 8 insertions(+), 13 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index c0fcbf7cb..51ee28c09 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -26,6 +26,7 @@ - Const slice indexing was not bounds checked. - Initialize pool correctly in print_backtrace. - `--max-mem` now works correctly again. +- Casting a fault to a pointer would trigger an assert. ### Stdlib changes diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 75b3b0869..6df722ed3 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -2573,7 +2573,6 @@ INLINE Type *type_vector_type(Type *type); static inline CanonicalType *type_pointer_type(Type *type); static inline CanonicalType *type_flatten(Type *type); -static inline bool type_flat_is_char_array(Type *type); static inline Type *type_base(Type *type); INLINE TypeInfo *type_info_new(TypeInfoKind kind, SourceSpan span); @@ -3498,8 +3497,6 @@ static inline void expr_set_span(Expr *expr, SourceSpan loc) default: return; } - expr_list_set_span(expr->expression_list, loc); - return; case EXPR_CAST: exprid_set_span(expr->cast_expr.expr, loc); return; @@ -4240,9 +4237,9 @@ INLINE bool check_module_name(Path *path) } #ifdef NDEBUG -#define ASSERT_SPANF(node__, check__, format__, ...) do { } while(0) -#define ASSERT_SPAN(node__, check__) do { } while(0) -#define ASSERT_AT(span__, check__) do { } while(0) +#define ASSERT_SPANF(node__, check__, format__, ...) do { (void)(check__); } while(0) +#define ASSERT_SPAN(node__, check__) do { (void)(check__); } while(0) +#define ASSERT_AT(span__, check__) do { (void)(check__);} while(0) #else #define ASSERT_SPANF(node__, check__, format__, ...) do { if (!(check__)) { assert_print_line((node__)->span); eprintf(format__, __VA_ARGS__); ASSERT(check__); } } while(0) #define ASSERT_SPAN(node__, check__) do { if (!(check__)) { assert_print_line((node__)->span); ASSERT(check__); } } while(0) diff --git a/src/compiler/diagnostics.c b/src/compiler/diagnostics.c index bf82eb010..c8aa0d82e 100644 --- a/src/compiler/diagnostics.c +++ b/src/compiler/diagnostics.c @@ -75,7 +75,6 @@ static void print_error_type_at(SourceSpan location, const char *message, PrintT } else if (compiler.build.test_output || compiler.build.benchmark_output) { - bool ansi = use_ansi(); switch (print_type) { case PRINT_TYPE_ERROR: diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 48255a1b4..b50051ddb 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -260,7 +260,7 @@ typedef enum FLAG_ATTR #define EXPORTED_USER_DEFINED_TYPES (ATTR_ENUM | ATTR_UNION | ATTR_STRUCT | ATTR_FAULT) #define CALLABLE_TYPE (ATTR_FUNC | ATTR_INTERFACE_METHOD | ATTR_MACRO | ATTR_FNTYPE) -#define USER_DEFINED_TYPES EXPORTED_USER_DEFINED_TYPES | ATTR_BITSTRUCT | ATTR_DISTINCT +#define USER_DEFINED_TYPES (EXPORTED_USER_DEFINED_TYPES | ATTR_BITSTRUCT | ATTR_DISTINCT) typedef enum { diff --git a/src/compiler/headers.c b/src/compiler/headers.c index 6117e8aed..5de9beabb 100644 --- a/src/compiler/headers.c +++ b/src/compiler/headers.c @@ -556,7 +556,6 @@ RETRY: TODO; case TYPE_FUNC_RAW: UNREACHABLE - return; case TYPE_STRUCT: case TYPE_UNION: header_gen_struct_union_top(c, type->decl, is_pointer ? GEN_POINTER : GEN_FULL); diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index daf342713..0d465ccff 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1854,6 +1854,7 @@ static void cast_int_to_enum(Expr *expr, Type *type) { Int to_convert = expr->const_expr.ixx; unsigned max_enums = vec_size(decl->enums.values); + (void)max_enums; assert(max_enums > to_convert.i.low); expr->expr_kind = EXPR_CONST; expr->const_expr = (ExprConst) { @@ -2499,7 +2500,7 @@ CastFunction cast_function[CONV_LAST + 1][CONV_LAST + 1] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // CONST ENUM {XX2VO, 0, PT2BO, PT2IN, 0, 0, 0, EX2VC, 0, 0, 0, 0, 0, 0, 0, 0, 0, PT2PT, 0, 0, PT2PT, 0, 0, 0 }, // FUNC {XX2VO, 0, TI2BO, TI2IN, 0, TI2PT, 0, EX2VC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TI2PT, TI2PT, 0, 0 }, // TYPEID - {XX2VO, 0, AF2BO, FA2IN, 0, FA2IN, 0, EX2VC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FA2IN, FA2IN, 0, 0 }, // ANYFAULT + {XX2VO, 0, AF2BO, FA2IN, 0, FA2PT, 0, EX2VC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FA2IN, FA2IN, 0, 0 }, // ANYFAULT {XX2VO, 0, PT2BO, PT2IN, 0, PT2PT, 0, EX2VC, 0, 0, 0, 0, 0, PT2AY, PT2AY, 0, 0, PT2PT, 0, 0, 0, PT2PT, 0, 0 }, // VOIDPTR {XX2VO, 0, PT2BO, PT2IN, 0, PT2PT, AP2SL, EX2VC, 0, 0, 0, 0, 0, PT2AY, PT2AY, 0, 0, 0, 0, 0, PT2PT, PT2PT, PT2FE, 0 }, // ARRAYPTR { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // INFERRED diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index d770275ef..052dc00bf 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -6659,7 +6659,7 @@ static bool sema_binary_analyse_ct_subscript_op_assign(SemaContext *context, Exp ArrayIndex idx = left->ct_subscript_expr.index; Expr *lhs_expr = left_var->var.init_expr; ASSERT_SPAN(lhs_expr, lhs_expr->expr_kind == EXPR_CONST); - Expr *value = expr_from_const_expr_at_index(left_var->var.init_expr, idx); + Expr *value = expr_from_const_expr_at_index(lhs_expr, idx); BinaryOp op = binaryop_assign_base_op(expr->binary_expr.operator); expr->binary_expr = (ExprBinary) { .left = exprid(value), .right = expr->binary_expr.right, .operator = op }; diff --git a/src/utils/json.c b/src/utils/json.c index bd30f55f9..6a8281652 100644 --- a/src/utils/json.c +++ b/src/utils/json.c @@ -278,8 +278,6 @@ JSONObject *json_parse_array(JsonParser *parser) return array; } - size_t capacity = 16; - size_t index = 0; while (1) { JSONObject *parsed = json_parse(parser);