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.

C#: find clock and environment calls written with System in full (#1391)

jakalijey435948cf2073
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 19c5310c..cba0e9c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -223,6 +223,10 @@
(#1337 by @jakalijey).
from one JVM start to the next. `List.of`, `Collectors.toList`, and a `toMap` or `groupingBy`
given a `TreeMap` or `LinkedHashMap` supplier are not (#1343 by @jakalijey).
+- C#: a clock or environment call written with its `System` namespace in full
+ (`System.DateTime.Now`, `System.Environment.GetEnvironmentVariable`,
+ `System.IO.Directory.GetCurrentDirectory()`, `global::System.IO.Path.GetTempPath()`) is found
+ like the short spelling, in `analyze` and `bridge` (#1373 by @jakalijey).
- TS/JS: `process.hrtime()`, `process.hrtime.bigint()` and `crypto.randomInt()` count as reading
the clock or randomness in `analyze` and `bridge`, and `analyze` now counts `crypto.randomBytes()`
too, which only `bridge` did (#1376 by @jakalijey).
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 82198e0a..8a569f51 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -635,7 +635,8 @@ fn find_all<'a>(hay: &'a str, needle: &'a str) -> impl Iterator<Item = usize> +
let before = hay[..i].chars().next_back();
let first = needle.chars().next().unwrap_or(' ');
// `pprint(` is not `print(`, `x.print(` is not a global print.
- let continues = before.is_some_and(|c| (is_ident(first) && is_ident(c)) || c == '.');
+ let continues = before.is_some_and(|c| (is_ident(first) && is_ident(c)) || c == '.')
+ && !after_system_namespace(&hay[..i]);
let rest = &hay[i + needle.len()..];
let longer = (method || now) && rest.starts_with(is_ident);
let fixed = now && passes_arguments(rest);
@@ -643,6 +644,21 @@ fn find_all<'a>(hay: &'a str, needle: &'a str) -> impl Iterator<Item = usize> +
})
}
+/// Whether `before` ends with a .NET `System` namespace written in full (`System.`,
+/// `System.IO.`, `global::System.`): `System.DateTime.Now` is `DateTime.Now`, while `x.Now` and
+/// `obj.Random` after any other name are members of something else.
+fn after_system_namespace(before: &str) -> bool {
+ ["System.IO.", "System."].iter().any(|ns| {
+ before.strip_suffix(ns).is_some_and(|head| {
+ head.strip_suffix("global::").is_some()
+ || !head
+ .chars()
+ .next_back()
+ .is_some_and(|c| is_ident(c) || c == '.' || c == ':')
+ })
+ })
+}
+
fn assigns(after: &str, lang: Lang) -> bool {
let a = after.trim_start_matches([' ', '\t']);
let mut chars = a.chars();
@@ -1457,6 +1473,21 @@ mod tests {
}
// The clock matters when nothing is passed: one recorded case is one moment.
assert!(reason(Lang::Python, &[], "def f():\n return time.time()").is_some());
+ assert!(
+ reason(
+ Lang::Csharp,
+ &[],
+ "public static long Ticks() => global::System.DateTime.UtcNow.Ticks;"
+ )
+ .is_some()
+ );
+ // A member of something else that is not the `System` namespace is not the clock.
+ for code in [
+ "public static long Ticks() => clock.DateTime.Now.Ticks;",
+ "public static long Ticks() => MySystem.DateTime.Now.Ticks;",
+ ] {
+ assert_eq!(reason(Lang::Csharp, &[], code), None, "{code}");
+ }
for body in ["process.hrtime.bigint()", "crypto.randomInt(6)"] {
let code = format!("export function f() {{\n return {body}\n}}\n");
assert!(reason(Lang::Ts, &[], &code).is_some(), "{body}");
@@ -1500,6 +1531,30 @@ mod tests {
fn scans_list_environment_clock_and_hash_order_in_every_spelling() {
use crate::risks::Risk;
let cases: &[(Lang, &str, &str, Risk)] = &[
+ (
+ Lang::Csharp,
+ "Stamp.cs",
+ "namespace A;\npublic static class S {\n public static long Full() => System.DateTime.Now.Ticks;\n}\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Csharp,
+ "Stamp.cs",
+ "namespace A;\npublic static class S {\n public static string Home(string n) => System.Environment.GetEnvironmentVariable(\"HOME\") + n;\n}\n",
+ Risk::SharedState,
+ ),
+ (
+ Lang::Csharp,
+ "Stamp.cs",
+ "namespace A;\npublic static class S {\n public static string Here(string n) => System.IO.Directory.GetCurrentDirectory() + n;\n}\n",
+ Risk::SharedState,
+ ),
+ (
+ Lang::Csharp,
+ "Stamp.cs",
+ "namespace A;\npublic static class S {\n public static string Tmp(string n) => global::System.IO.Path.GetTempPath() + n;\n}\n",
+ Risk::SharedState,
+ ),
(
Lang::C,
"dice.c",