Hi,

Savannah bug #67900 (https://savannah.nongnu.org/bugs/?67900, no reply
yet) is still there on mob aade7baf (x86_64 Linux, ASan tcc_s).  I hit
the same bug independently by fuzzing:

$ cat t.c
int *p = L"\"" "" "" "";
$ ./tcc_s -B. -c t.c -o /dev/null
ERROR: AddressSanitizer: heap-buffer-overflow
    #2 in decl_initializer tccgen.c:8156

And wchar_t w[] = L"a" "bcd"; gives 0x61 0x646362 plus leftover bytes:
decl_initializer() appends the narrow parts unwidened, then
"initstr.size -= size1" drops 4 bytes after a 1-byte part.  The
attached patch widens a narrow part after a wide one, byte by byte
(unlike gcc, no UTF-8 decoding).

Test: make test passes; t.c and the input attached to #67900 give no
ASan error, and w[] is 0x61 0x62 0x63 0x64 0.

Regards,
Sechang Jang
The text and patch were prepared with help from an LLM (Claude).
From 9ddebc5332972c4170f79c009b6f53781dca19aa Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccgen: widen narrow parts after a wide string literal

    wchar_t w[] = L"a" "bcd";

The narrow bytes were appended unwidened, and "initstr.size -=
size1" could make the size negative (heap write before the
buffer).  Savannah #67900.
---
 tccgen.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tccgen.c b/tccgen.c
index d9721a52..3ba2d98d 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -8153,7 +8153,14 @@ static void decl_initializer(init_params *p, CType *type, unsigned long c, int f
                 else
                   len += tokc.str.size / sizeof(nwchar_t);
                 len--;
-                cstr_cat(&initstr, tokc.str.data, tokc.str.size);
+                if (tok == TOK_STR && size1 != 1) {
+                  /* narrow part of a wide string: widen it */
+                  for (i = 0; i < tokc.str.size; i++) {
+                    nwchar_t wc = ((unsigned char *)tokc.str.data)[i];
+                    cstr_cat(&initstr, (char *)&wc, sizeof wc);
+                  }
+                } else
+                  cstr_cat(&initstr, tokc.str.data, tokc.str.size);
                 next();
             }
             if (tok != ')' && tok != '}' && tok != ',' && tok != ';'
-- 
2.34.1

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to