Skip to content

Commit f6bf8fc

Browse files
committed
fix(parse): reject a tuple % whose operand count disagrees with the format
Python raises TypeError when the operand count does not match the conversion count, but the tuple path silently tolerated both shapes: extra operands were dropped (the second call found no conversion left and returned the string unchanged) and too few left a literal %s in the value. Either way a wrong value reached the field instead of the field being skipped. Counts conversions (skipping %% escapes) and errors on a mismatch, so the assignment fails and the field is omitted — the same skip-don't-guess invariant the += fix restored. Thanks to the Copilot review for catching it.
1 parent abff808 commit f6bf8fc

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

‎src/eb_parse.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,17 @@ impl<'a> Parser<'a> {
541541
Value::Str(python_percent_format_one(&fmt, &arg.to_string()))
542542
}
543543
(Value::Str(fmt), Value::Tuple(args)) => {
544+
// Python raises TypeError when the operand count does not
545+
// match the conversion count. Silently dropping extra
546+
// operands (or leaving a `%s` unfilled) would put a wrong
547+
// value in the field instead of skipping it, so refuse.
548+
let need = count_percent_conversions(&fmt);
549+
if need != args.len() {
550+
return Err(self.err(format!(
551+
"% format has {need} conversion(s) but {} operand(s)",
552+
args.len()
553+
)));
554+
}
544555
let mut out = fmt;
545556
for arg in &args {
546557
let s = match arg {
@@ -811,6 +822,33 @@ fn unescape_python_str(s: &str) -> String {
811822
s.to_string()
812823
}
813824

825+
/// Count `%s` / `%d` conversions, skipping `%%` escapes. Used to reject a
826+
/// tuple `%` whose operand count disagrees with the format string, which
827+
/// Python treats as a TypeError rather than a partial substitution.
828+
fn count_percent_conversions(fmt: &str) -> usize {
829+
let b = fmt.as_bytes();
830+
let mut i = 0;
831+
let mut n = 0;
832+
while i + 1 < b.len() {
833+
if b[i] == b'%' {
834+
match b[i + 1] {
835+
b'%' => {
836+
i += 2;
837+
continue;
838+
}
839+
b's' | b'd' => {
840+
n += 1;
841+
i += 2;
842+
continue;
843+
}
844+
_ => {}
845+
}
846+
}
847+
i += 1;
848+
}
849+
n
850+
}
851+
814852
/// Minimal Python-style `%s` / `%d` formatting used in easyconfigs. Replaces
815853
/// the leftmost conversion so tuple args substitute in positional order.
816854
fn python_percent_format_one(fmt: &str, arg: &str) -> String {
@@ -2860,4 +2898,38 @@ toolchain = {'name': 'GCCcore', 'version': '14.3.0'}
28602898
let r = resolve_easyconfig_str(src).expect("resolve");
28612899
assert_eq!(r.homepage.as_deref(), Some("https://example.org/v3/demo"));
28622900
}
2901+
#[test]
2902+
fn percent_format_arity_mismatch_skips_the_field_instead_of_guessing() {
2903+
// Python raises TypeError for both shapes. The parser must not emit a
2904+
// partially-substituted value: before the arity check, `too_many` silently
2905+
// dropped `version` and `too_few` left a literal `%s` in the URL.
2906+
let too_many = r#"
2907+
name = 'demo'
2908+
version = '1.0'
2909+
homepage = 'https://example.org/%s' % (name, version)
2910+
toolchain = {'name': 'GCCcore', 'version': '14.3.0'}
2911+
"#;
2912+
let r = resolve_easyconfig_str(too_many).expect("file still resolves");
2913+
assert_eq!(
2914+
r.homepage, None,
2915+
"extra operand must skip the field, not be dropped silently"
2916+
);
2917+
2918+
let too_few = r#"
2919+
name = 'demo'
2920+
version = '1.0'
2921+
homepage = 'https://example.org/%s/%s' % (name,)
2922+
toolchain = {'name': 'GCCcore', 'version': '14.3.0'}
2923+
"#;
2924+
let r = resolve_easyconfig_str(too_few).expect("file still resolves");
2925+
assert_eq!(r.homepage, None, "unfilled conversion must skip the field");
2926+
}
2927+
2928+
#[test]
2929+
fn percent_escape_is_not_counted_as_a_conversion() {
2930+
// `%%` is a literal percent, so this format has exactly one real conversion.
2931+
assert_eq!(count_percent_conversions("100%% of %s"), 1);
2932+
assert_eq!(count_percent_conversions("%s-%s"), 2);
2933+
assert_eq!(count_percent_conversions("no conversions here"), 0);
2934+
}
28632935
}

0 commit comments

Comments
 (0)