i18n: a catalogue text is data, never a printf format
The interview note built its buffer with the translation as the format
string:
snprintf(note, sizeof(note), sp_td("interview.saved", ...))
A '%' in a translation would be read as a conversion - clformat-security named it. The text goes through "%s" now.
tools/check_i18n.py grew the guard that keeps the shape out: a sp_t()/
sp_td() call may be the format only when arguments follow it; with none
behind it the conversions are unintended.
clang -Wformat-security: 1 -> no warning at that call
check_i18n with the bug restored: fires, rc=1
check_i18n with the fix: ok (229 keys, 3 catalogues)
make check: all tests passed, rc=0
This commit is contained in:
1 parent
cfe0af2688
commit
4507a3c57c
2 files changed
+79
-1
No files matched your search
@@ -1386,7 +1386,13 @@ turn_thread(void *arg)
|
||||
if (rc == 0 && capture_interview(e, &e->answer)) {
|
||||
char note[700];
|
||||
|
||||
snprintf(note, sizeof(note), sp_td("interview.saved",
|
||||
/*
|
||||
* The catalogue text is data, not a format: a % in a
|
||||
* translation went to snprintf as a conversion
|
||||
* (-Wformat-security, clang 2026-11-22).
|
||||
*/
|
||||
snprintf(note, sizeof(note), "%s",
|
||||
sp_td("interview.saved",
|
||||
"interview saved to USER.txt"));
|
||||
ev_push(e, SP_EV_ENTRY, 0, "splinter", note);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,72 @@ def hardcoded():
|
||||
return bad
|
||||
|
||||
|
||||
# A catalogue text is data, never a format: a "%" in a translation that reaches
|
||||
# printf as the format is read as a conversion (undefined behaviour, and clang's
|
||||
# -Wformat-security says so). sp_t()/sp_td() may be an argument, never the
|
||||
# format itself.
|
||||
PRINTF = re.compile(r'\b(snprintf|vsnprintf|sp_buf_printf|fprintf|printf|'
|
||||
r'sprintf|syslog)\s*\(')
|
||||
# which argument is the format, per callee
|
||||
FMT_ARG = {"snprintf": 2, "vsnprintf": 2, "sp_buf_printf": 1, "fprintf": 1,
|
||||
"printf": 0, "sprintf": 1, "syslog": 1}
|
||||
|
||||
|
||||
def top_level_args(text, start):
|
||||
"""The arguments of the call whose '(' is at text[start], split at depth 1."""
|
||||
args, depth, i, cur = [], 1, start + 1, start + 1
|
||||
quote = None
|
||||
while i < len(text):
|
||||
c = text[i]
|
||||
if quote is not None:
|
||||
if c == "\\":
|
||||
i += 2
|
||||
continue
|
||||
if c == quote:
|
||||
quote = None
|
||||
elif c in "\"'":
|
||||
quote = c
|
||||
elif c == "(":
|
||||
depth += 1
|
||||
elif c == ")":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
args.append(text[cur:i])
|
||||
return args
|
||||
elif c == "," and depth == 1:
|
||||
args.append(text[cur:i])
|
||||
cur = i + 1
|
||||
i += 1
|
||||
return args
|
||||
|
||||
|
||||
def catalogue_as_format():
|
||||
"""A catalogue text may be a format only when arguments follow it.
|
||||
|
||||
With no argument behind it, a '%' that creeps into a translation (or into
|
||||
an English entry written by hand) is read as a conversion: clang's
|
||||
-Wformat-security names exactly this call shape.
|
||||
"""
|
||||
bad = []
|
||||
for path in ROOT.glob("src/**/*.c"):
|
||||
text = path.read_text(encoding="utf-8", errors="replace")
|
||||
for m in PRINTF.finditer(text):
|
||||
args = top_level_args(text, m.end() - 1)
|
||||
want = FMT_ARG.get(m.group(1), 1)
|
||||
if len(args) <= want:
|
||||
continue
|
||||
fmt = args[want].strip()
|
||||
if not (fmt.startswith("sp_t(") or fmt.startswith("sp_td(")):
|
||||
continue
|
||||
if len(args) > want + 1:
|
||||
continue # arguments follow: the conversions are meant
|
||||
line = text.count("\n", 0, m.start()) + 1
|
||||
bad.append(f"{path.name}:{line}: {m.group(1)}(...) takes a "
|
||||
f"catalogue text as its format, with no argument "
|
||||
f"behind it")
|
||||
return bad
|
||||
|
||||
|
||||
# Roles are keys too: add_entry() takes an identifier, the label comes from the
|
||||
# catalogue. An unknown identifier would ship as untranslated text.
|
||||
ROLE_CALL = re.compile(r'add_entry\(\s*&?[A-Za-z_>.-]*\s*,\s*"([a-z_]+)"')
|
||||
@@ -105,6 +171,12 @@ def main():
|
||||
print("hardcoded text on screen (use sp_t/sp_td):", file=sys.stderr)
|
||||
for item in lit:
|
||||
print(" " + item, file=sys.stderr)
|
||||
fmt = catalogue_as_format()
|
||||
if fmt:
|
||||
bad = 1
|
||||
print("catalogue text used as a printf format:", file=sys.stderr)
|
||||
for item in fmt:
|
||||
print(" " + item, file=sys.stderr)
|
||||
if bad == 0:
|
||||
print(f"check_i18n: ok ({len(used)} keys in {len(cats)} catalogues)")
|
||||
return bad
|
||||
|
||||
Reference in new issue
Block a user