Hi,
Same setup as my asm mail (mob aade7baf, ASan tcc_s). Four tccgen
bugs, also found by fuzzing, one patch each.
1) EOF right after '.' ('->' and a designator '.' too):
$ printf 'struct s { int x; } v;\nint f(void) { return v.' > c1.c
$ ./tcc_s -B. -c c1.c -o /dev/null
ERROR: AddressSanitizer: SEGV on unknown address 0x000000000018
#0 in unary tccgen.c:6247
find_field() gets TOK_EOF (-1), and !(v & SYM_FIELD) is false for -1.
Patch 1 tests v < SYM_FIELD.
2) ({...}) in a prototype at file scope:
$ echo 'int f(int a[({ 1; })]);' > c2.c; ./tcc_s -B. -c c2.c -o /dev/null
ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
#0 in new_scope tccgen.c:7138
local_scope is set in parameter lists, so cur_scope is NULL here (or
stale after a function body). Patch 2 resets it after gen_function()
and checks it.
3) cleanup() with a number crashes; table_ident[] is indexed with it:
$ echo 'int x __attribute__((cleanup(1)));' > c3.c; ./tcc_s -B. -c c3.c -o
/dev/null
ERROR: AddressSanitizer: SEGV on unknown address 0x526fffffff10
#0 in global_identifier_push tccgen.c:773
4) Wrong data, no crash:
$ echo 'int i = "abc"[0];' > c4.c; ./tcc_s -B. -c c4.c -o c4.o && objdump -s -j
.data c4.o
0000 61626300 abc.
gcc gives 61000000. init_putv() copies sizeof(int) bytes from the
string; patch 4 loads the char and converts it. "abc"[9] still reads
out of bounds, as before (not covered).
Test: make test passes; c1-c3 give normal errors, c4.c gives 61000000.
Regards,
Sechang Jang
The text and patch were prepared with help from an LLM (Claude).
From 43042fbd4bb6b204be5032c82c1bb892b1e39f49 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccgen: find_field() with tok == TOK_EOF
struct s { int x; } v;
int f(void) { return v.
!(v & SYM_FIELD) is false for -1, so the checks were skipped.
---
tccgen.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tccgen.c b/tccgen.c
index d9721a52..727c327c 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -4178,7 +4178,7 @@ static Sym * find_field (CType *type, int v, int *cumofs)
{
Sym *s = type->ref;
int v1 = v | SYM_FIELD;
- if (!(v & SYM_FIELD)) { /* top-level call */
+ if (v < SYM_FIELD) { /* top-level call, v may be TOK_EOF */
if ((type->t & VT_BTYPE) != VT_STRUCT)
expect("struct or union");
if (v < TOK_UIDENT)
@@ -4202,7 +4202,7 @@ static Sym * find_field (CType *type, int v, int *cumofs)
}
}
}
- if (!(v & SYM_FIELD))
+ if (v < SYM_FIELD)
tcc_error("field not found: %s", get_tok_str(v, NULL));
return s;
}
--
2.34.1
From e2ea96a56b539546698eaf8567c7dc53c7977420 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccgen: ({...}) in a prototype at file scope
int f(int a[({ 1; })]);
local_scope is set in parameter lists, so the check from 8b69059f
did not catch it and new_scope() used cur_scope, which is NULL or
still points to the 'f' of the last gen_function().
---
tccgen.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tccgen.c b/tccgen.c
index d9721a52..82eb25da 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -5775,7 +5775,7 @@ ST_FUNC void unary(void)
int saved_nocode_wanted = nocode_wanted;
if (CONST_WANTED && !NOEVAL_WANTED)
expect("constant");
- if (0 == local_scope)
+ if (0 == local_scope || !cur_scope)
tcc_error("statement expression outside of function");
/* save all registers */
save_regs(0);
@@ -8721,6 +8721,7 @@ static void gen_function(Sym *sym)
label_pop(&global_label_stack, NULL, 0);
sym_pop(&all_cleanups, NULL, 0);
local_scope = 0;
+ cur_scope = root_scope = NULL;
/* It's better to crash than to generate wrong code */
cur_text_section = NULL;
--
2.34.1
From 43a78af9c50b50eff0dd297494bd8c95c8bf7393 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccgen: cleanup() argument must be an identifier
int x __attribute__((cleanup(1)));
crashed in global_identifier_push().
---
tccgen.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tccgen.c b/tccgen.c
index d9721a52..4c37735c 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -3974,6 +3974,8 @@ redo:
Sym *s;
skip('(');
+ if (tok < TOK_UIDENT)
+ expect("identifier");
s = sym_find(tok);
if (!s) {
tcc_warning_c(warn_implicit_function_declaration)(
--
2.34.1
From 9132f1f7128374fe6bd9d6d906fdbe08935c9250 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccgen: static init from "abc"[0] into a different type
int i = "abc"[0];
stored 0x00636261 because init_putv() copied sizeof(int) bytes
from the string (and e.g. char *p = "abc"[2]; read 8 bytes past it).
Load the element of the string or compound literal and convert
it instead.
---
tccgen.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/tccgen.c b/tccgen.c
index d9721a52..05e36f2b 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -7949,6 +7949,25 @@ static void init_putv(init_params *p, CType *type, unsigned long c)
init_assert(p, c + size);
if (sec) {
+ bt = vtop->type.t & VT_BTYPE;
+ if ((vtop->r & (VT_SYM|VT_CONST|VT_LVAL)) == (VT_SYM|VT_CONST|VT_LVAL)
+ && vtop->sym->v >= SYM_FIRST_ANOM
+ && is_integer_btype(bt) && !(vtop->type.t & VT_BITFIELD)
+ && !is_compatible_unqualified_types(&vtop->type, &dtype)
+ && !NODATA_WANTED) {
+ /* e.g. int i = "abc"[0]; load the element, convert below */
+ ElfSym *esym = elfsym(vtop->sym);
+ unsigned char *q = tcc_state->sections[esym->st_shndx]->data
+ + esym->st_value + (int)vtop->c.i;
+ int i = type_size(&vtop->type, &align);
+ for (val = 0; i--; )
+ val = val << 8 | q[i];
+ if (!(vtop->type.t & VT_UNSIGNED))
+ val = bt == VT_BYTE ? (signed char)val
+ : bt == VT_SHORT ? (short)val : val;
+ vtop->r = VT_CONST;
+ vtop->c.i = val;
+ }
/* XXX: not portable */
/* XXX: generate error if incorrect relocation */
gen_assign_cast(&dtype);
--
2.34.1
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel