Skip to content

fix(hy-check): reject statically-known non-string format and input operands on fmt scan family (fixes #4265) - #4267

Open
Anekenonso wants to merge 1 commit into
limitlessprojects:mainfrom
Anekenonso:fix/fmt-scan-family-nonstring-operands-4265
Open

Anekenonso wants to merge 1 commit into
limitlessprojects:mainfrom
Anekenonso:fix/fmt-scan-family-nonstring-operands-4265

Conversation

@Anekenonso

Copy link
Copy Markdown
Contributor

Summary & Motivation

Fixes #4265.

While PR #4196 (resolving #4195) introduced Collector::nonstring_format_kind(&Expr) to reject statically-known non-string expressions (such as resolved integer locals or binary operations) passed to fmt.printf, fmt.sprintf, fmt.eprintf, fmt.errorf, and fmt.fprintf, the scan-family arms in compiler/crates/hy-check/src/buildable.rs (lines 8107–8240) were omitted.

As demonstrated in Issue #4265, passing non-string operands via local variables (e.g. x := 42) or binary expressions (e.g. 40 + 2) to fmt.scanf, fmt.sscanf, fmt.fscanf, fmt.sscan, or fmt.sscanln passed hyb check with 0 diagnostics (Verdict: Accept). Emitted Go code called fmt.Scanf(x, ...), fmt.Sscanf(x, ...), fmt.Fscanf(..., x, ...), or fmt.Sscan(x, ...), which fails downstream go build with type mismatch errors (violating GUARANTEES.md Row 13 BUILDABLE).

Changes

  1. Format & Input Operand Validation in buildable.rs:
    • fmt.scanf: checks self.nonstring_format_kind(bare) and rejects statically-known non-string format operands with HYB-E1002.
    • fmt.sscanf: checks self.nonstring_format_kind on both bare0 (input string) and bare1 (format string), rejecting non-string operands with HYB-E1002.
    • fmt.fscanf: checks self.nonstring_format_kind(bare1) (format string) and rejects non-string operands with HYB-E1002.
    • fmt.sscan & fmt.sscanln: checks self.nonstring_format_kind(bare0) (input string) and rejects non-string operands with HYB-E1002.
  2. Integration Test Suite:
    • Extended compiler/crates/hy-check/tests/fmt_scan_family_buildable.rs with 7 new negative regression tests covering non-string local variables (x := 42, b := true), binary operations (40 + 2, 10 * 2), across all scan functions.
    • Added positive control test confirming that string local variables (fmt_str := "%d", input_str := "42") continue to compile and pass with 0 errors.

Verification

  • cargo test -p hy-check --test fmt_scan_family_buildable: 21 passed / 0 failed.
  • cargo test -p hy-check --test fmt_special_member_calls_buildable: 21 passed / 0 failed.
  • cargo check -p hy-check: 0 errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:land-candidate Shepherd: fresh, gate-clean, verifiable (set from classify())

Projects

None yet

1 participant