You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Transpiler miscompiles (array<u8,N> as string) inside a string-concat flatten #577
A cast of an array-of-u8 to string used directly inside a string concatenation is miscompiled to C: instead of using the buffer pointer as a string, the C generator emits snprintf(buf, 64, "%d", ptr), printing the pointer value as an integer (up to ~10 chars) into a 64-byte stack buffer.
Repro
extern {
function printf(fmt: string, val: i64) -> i32
function strlen(s: string) -> i64
}
function main() -> i32 {
let mut ov: array<u8, 6000> = []
let mut oi: i32 = 0
while oi < 5000 {
ov[oi] = 112
oi = oi + 1
}
ov[5000] = 0
let sql: string = "INSERT INTO t VALUES (1, '" + (ov as string) + "');"
let n: i64 = strlen(sql)
printf("sql len=%lld\n", n) # prints 39, not 5029
return 0
}
Generated C (truncated):
char*sql=flow_strcat(flow_strcat("INSERT INTO t VALUES (1, '", ({ char_flow_strval_15694000[64]; snprintf(_flow_strval_15694000, sizeof(_flow_strval_15694000), "%d", ((char*)(ov))); _flow_strval_15694000; })), "');");
strlen of the result is 39: the 5000 'p' chars are replaced by the decimal pointer value (10 digits). Note the string then contains the raw digits, so this is also an aliasing/type bug, not just truncation.
Workaround
Assigning the cast to a variable first compiles correctly:
let ovs: string = ov as string
let sql: string = "INSERT INTO t VALUES (1, '" + ovs + "');" # correct, len 5029
Found while building flow-sqlite (overflow-page records > 4095 bytes in the self-check harness).
Activity
String concatenation is a string, whatever the other operand is (#577) (