From 19589288cb8cdde4c9a9cb783e9fe4d0af51d6ed Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Thu, 16 Oct 2025 07:35:27 +0200 Subject: [PATCH] Some fixes and cleanups tcc.h, tccgen.c: Add and use IS_BT_ARRAY tccpp.c: free memory when size is 0 in realloc code tccdbg.c: move common code to seperate function remove_type_info --- tcc.h | 1 + tccdbg.c | 30 ++++++++++++++---------------- tccgen.c | 2 +- tccpp.c | 5 ++++- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tcc.h b/tcc.h index ee34b169..3157379d 100644 --- a/tcc.h +++ b/tcc.h @@ -1098,6 +1098,7 @@ struct filespec { /* base type is array (from typedef/typeof) */ #define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT) +#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY) /* general: set/get the pseudo-bitfield value for bit-mask M */ #define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N)) diff --git a/tccdbg.c b/tccdbg.c index 41648d0f..ccd944c8 100644 --- a/tccdbg.c +++ b/tccdbg.c @@ -1769,6 +1769,16 @@ static int stabs_struct_find(TCCState *s1, Sym *t, int *p_id) return 1; } +static int remove_type_info(int type) +{ + type &= ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA); + if ((type & VT_BTYPE) != VT_BYTE) + type &= ~VT_DEFSIGN; + if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM) + type &= ~VT_STRUCT_MASK; + return type; +} + static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result) { int type; @@ -1778,11 +1788,7 @@ static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result) CString str; for (;;) { - type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA); - if ((type & VT_BTYPE) != VT_BYTE) - type &= ~VT_DEFSIGN; - if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM) - type &= ~VT_STRUCT_MASK; + type = remove_type_info (t->type.t); if (type == VT_PTR || type == (VT_PTR | VT_ARRAY)) n++, t = t->type.ref; else @@ -1861,9 +1867,7 @@ static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result) cstr_printf (result, "%d=", ++debug_next_type); t = s; for (;;) { - type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA); - if ((type & VT_BTYPE) != VT_BYTE) - type &= ~VT_DEFSIGN; + type = remove_type_info (t->type.t); if (type == VT_PTR) cstr_printf (result, "%d=*", ++debug_next_type); else if (type == (VT_PTR | VT_ARRAY)) @@ -1901,11 +1905,7 @@ static int tcc_get_dwarf_info(TCCState *s1, Sym *s) if (new_file) put_new_file(s1); for (;;) { - type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA); - if ((type & VT_BTYPE) != VT_BYTE) - type &= ~VT_DEFSIGN; - if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM) - type &= ~VT_STRUCT_MASK; + type = remove_type_info (t->type.t); if (type == VT_PTR || type == (VT_PTR | VT_ARRAY)) t = t->type.ref; else @@ -2052,9 +2052,7 @@ static int tcc_get_dwarf_info(TCCState *s1, Sym *s) e = NULL; t = s; for (;;) { - type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA); - if ((type & VT_BTYPE) != VT_BYTE) - type &= ~VT_DEFSIGN; + type = remove_type_info (t->type.t); if (type == VT_PTR) { i = dwarf_info_section->data_offset; if (retval == debug_type) diff --git a/tccgen.c b/tccgen.c index b5a385e7..99e48126 100644 --- a/tccgen.c +++ b/tccgen.c @@ -8204,7 +8204,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, we will overwrite the unknown size by the real one for this decl. We need to unshare the ref symbol holding that size. */ - if (type->t & VT_BT_ARRAY) + if (IS_BT_ARRAY(type->t)) type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c); p.flex_array_ref = type->ref; diff --git a/tccpp.c b/tccpp.c index 96e36028..96385f5f 100644 --- a/tccpp.c +++ b/tccpp.c @@ -159,6 +159,7 @@ static void *tcc_realloc_impl(void *p, unsigned size) PP_ALLOC_INSERT(alloc); return alloc + 1; } + tcc_free(alloc); return NULL; } #else @@ -372,8 +373,10 @@ tail_call: PP_ALLOC_INSERT(alloc); ret = alloc + 1; } - else + else { + tcc_free(alloc); ret = NULL; + } } #ifdef TAL_INFO al->nb_missed++;