Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(effects): detect putc, fputc, dprintf, vprintf, and vfprintf in C (#1500) (#1501)

isodev4dc21b86208d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 46f78591..b6eb593f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+- C: `putc`, `fputc`, `dprintf`, `vprintf` and `vfprintf` count as printing, as `printf` does,
+ so `bridge` keeps a function that calls one in C with the reason ``prints (`putc`)``. Before, a
+ port that dropped the output passed `diff`. A member call such as `ops->putc(c)` is still not a
+ print (#1500, #1501 by @iyazici00).
- C#: `Environment.ProcessId`, `Environment.ProcessorCount`, `Environment.OSVersion`,
`Environment.UserDomainName`, `Environment.GetCommandLineArgs` and `AppContext.BaseDirectory`
count as reading the environment, as `Environment.CurrentDirectory` does, so `bridge` keeps a
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 193af427..5cbd6ac6 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -92,7 +92,18 @@ fn prints(lang: Lang) -> &'static [&'static str] {
],
Lang::Python => &["print(", "sys.stdout.write(", "sys.stderr.write("],
Lang::C => &[
- "printf(", "puts(", "putchar(", "fputs(", "fprintf(", "fwrite(", "perror(",
+ "printf(",
+ "puts(",
+ "putchar(",
+ "putc(",
+ "fputc(",
+ "fputs(",
+ "fprintf(",
+ "dprintf(",
+ "vprintf(",
+ "vfprintf(",
+ "fwrite(",
+ "perror(",
],
Lang::Go => &[
"fmt.Print",
@@ -1720,6 +1731,38 @@ mod tests {
assert!(reason(Lang::Ruby, &[], "def f\n puts 1\n 2\nend").is_some());
assert!(reason(Lang::Php, &[], "function f() { echo 1; return 2; }").is_some());
assert!(reason(Lang::Powershell, &[], "function F { Write-Host 1; 2 }").is_some());
+ assert!(
+ reason(
+ Lang::C,
+ &[],
+ "int f(int d) { putc('0' + d, stdout); return d; }"
+ )
+ .is_some()
+ );
+ assert!(
+ reason(
+ Lang::C,
+ &[],
+ "int f(int d) { fputc('*', stdout); return d; }"
+ )
+ .is_some()
+ );
+ assert!(
+ reason(
+ Lang::C,
+ &[],
+ "int f(int d) { dprintf(2, \"%d\\n\", d); return d; }"
+ )
+ .is_some()
+ );
+ assert!(
+ reason(
+ Lang::C,
+ &[],
+ "int f(struct ops *ops, int c) { ops->putc(c); return c; }"
+ )
+ .is_none()
+ );
}
#[test]