Commit 62e964b6
62e964b6b327Merged
zhware <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix(codegen): root parts of >=5 string-concat chain to prevent UAF
Details
- Commit
- 62e964b6b327
- Parent
- b4ef1d7089f0
spinel_codegen.rbmodifiedView file
@@ -6522,6 +6522,11 @@
65226522
rt = infer_type(@nd_receiver[nid])65236523
if rt == "string"65246524
@needs_string_helpers = 16525+
# Long string concat chains emit SP_GC_ROOT temps, so the6526+
# enclosing function needs SP_GC_SAVE() in its header.6527+
if mname == "+"6528+
@needs_gc = 16529+
end65256530
end65266531
end65276532
end@@ -13057,18 +13062,33 @@
1305713062
return "sp_str_concat4(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ", " + parts[3] + ")"1305813063
end1305913064
if parts.length >= 513060-
# Variable-length: single malloc for N parts via sp_str_concat_arr13065+
# Variable-length: single malloc for N parts via sp_str_concat_arr.13066+
# Hoist each part into a rooted temp first — the compound-literal13067+
# initializer order is unspecified, and any part that is a fresh13068+
# GC string would otherwise sit unrooted on the C stack while13069+
# later parts evaluate (and may trigger sp_gc_collect).13070+
# @needs_gc is set in scan_features for any string `+`, ensuring13071+
# SP_GC_SAVE() is in the function header before we emit roots here.13072+
tnames = "".split(",")13073+
k = 013074+
while k < parts.length13075+
t = new_temp13076+
emit(" const char * " + t + " = " + parts[k] + ";")13077+
emit(" SP_GC_ROOT(" + t + ");")13078+
tnames.push(t)13079+
k = k + 113080+
end1306113081
arr = "(const char *const[]){"1306213082
k = 013063-
while k < parts.length13083+
while k < tnames.length1306413084
if k > 01306513085
arr = arr + ", "1306613086
end13067-
arr = arr + parts[k]13087+
arr = arr + tnames[k]1306813088
k = k + 11306913089
end1307013090
arr = arr + "}"13071-
return "sp_str_concat_arr(" + arr + ", " + parts.length.to_s + ")"13091+
return "sp_str_concat_arr(" + arr + ", " + tnames.length.to_s + ")"1307213092
end1307313093
return "sp_str_concat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"1307413094
end