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.

Ruby: count Time.new as a clock read only with no arguments (#1366)

jakalijeyd43449505d42
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 078f008f..ae8558a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -188,6 +188,9 @@
fails. Before, a Python unit built on `BeautifulSoup(html, "html.parser")`, which no crate
reproduces, could only meet the cap by dropping behavior. The skill and TASK.md tell the agent
to ask the user instead of writing one (#1189 by @sungkilhan221).
+- Ruby: `Time.new` counts as reading the clock only with no arguments (`Time.new`, `Time.new()`,
+ `Time.new.year`). `Time.new(2000, 1, 1)` builds a fixed date, so a function that calls it is
+ bridged, and `analyze` no longer counts it as a clock risk (#1337 by @jakalijey).
- Java and Kotlin: `ThreadLocalRandom.`, `SecureRandom(` and a full `new java.util.Random(` count
as randomness, so `bridge` keeps a function that rolls with one of them and `analyze` lists it as
a clock and random risk. Before, only `new Random(` and `Random.` were seen (#1339 by @jakalijey).
@@ -204,6 +207,10 @@
full `new java.util.Random(` count as randomness, so `bridge` keeps a function that rolls with one
of them and `analyze` lists it as a clock and random risk. Before, only `new Random(` and
`Random.` were seen (#1339 by @jakalijey).
+- Ruby: `Time.new` counts as reading the clock only with no arguments or a zone alone (`Time.new`,
+ `Time.new()`, `Time.new.year`, `Time.new(in: "+09:00")`). `Time.new(2000, 1, 1)` builds a fixed
+ date, so a function that calls it is bridged, and `analyze` no longer counts it as a clock risk
+ (#1337 by @jakalijey).
## 5.1.1 (2026-12-02)
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index fb2efee6..2f1eddaa 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -562,17 +562,50 @@ fn is_ident(c: char) -> bool {
c.is_alphanumeric() || c == '_' || c == '$'
}
+/// Calls that read the clock only with no arguments: Ruby's `Time.new` is now, `Time.new(2000, 1, 1)`
+/// is a fixed date.
+const NOW_WITHOUT_ARGUMENTS: &[&str] = &["Time.new"];
+
+/// Whether `rest`, the text right after such a call, passes it an argument: `(2000, 1, 1)`, or
+/// `2000, 1, 1` with no parentheses. `()`, `.year`, the end of the line and a keyword such as
+/// `if` pass none.
+fn passes_arguments(rest: &str) -> bool {
+ let spaced = rest.trim_start_matches([' ', '\t']);
+ if let Some(inside) = spaced.strip_prefix('(') {
+ let inside = inside.trim_start();
+ return !inside.starts_with(')') && !inside.starts_with("in:");
+ }
+ if spaced.len() == rest.len() || spaced.starts_with("in:") {
+ return false;
+ }
+ if let Some(after) = spaced.strip_prefix('-') {
+ return after.starts_with(|c: char| !c.is_whitespace());
+ }
+ let word: String = spaced.chars().take_while(|&c| is_ident(c)).collect();
+ if matches!(
+ word.as_str(),
+ "if" | "unless" | "and" | "or" | "do" | "then" | "while" | "until" | "rescue" | "end"
+ ) {
+ return false;
+ }
+ spaced.starts_with(|c: char| is_ident(c) || matches!(c, '"' | '\'' | ':' | '-' | '['))
+}
+
/// Positions of `needle` in `hay` that do not continue a longer name before them, nor, for a
-/// method name such as `.sample`, after them (`.sample_rate`, `.samples`).
+/// method name such as `.sample`, after them (`.sample_rate`, `.samples`). A call of
+/// `NOW_WITHOUT_ARGUMENTS` that passes an argument is not one.
fn find_all<'a>(hay: &'a str, needle: &'a str) -> impl Iterator<Item = usize> + 'a {
let method = needle.starts_with('.') && needle.chars().next_back().is_some_and(is_ident);
+ let now = NOW_WITHOUT_ARGUMENTS.contains(&needle);
hay.match_indices(needle).filter_map(move |(i, _)| {
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 longer = method && hay[i + needle.len()..].starts_with(is_ident);
- ((!continues || needle.starts_with('$')) && !longer).then_some(i)
+ let rest = &hay[i + needle.len()..];
+ let longer = (method || now) && rest.starts_with(is_ident);
+ let fixed = now && passes_arguments(rest);
+ ((!continues || needle.starts_with('$')) && !longer && !fixed).then_some(i)
})
}
@@ -1553,6 +1586,36 @@ mod tests {
"module M\n def self.f\n Time.now.year\n end\nend\n",
Risk::ClockAndRandom,
),
+ (
+ Lang::Ruby,
+ "x.rb",
+ "module M\n def self.f\n Time.new.year\n end\nend\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Ruby,
+ "x.rb",
+ "module M\n def self.f\n Time.new().year\n end\nend\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Ruby,
+ "x.rb",
+ "module M\n def self.f\n Time.new\n end\nend\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Ruby,
+ "x.rb",
+ "module M\n def self.f\n Time.new - 60\n end\nend\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Ruby,
+ "x.rb",
+ "module M\n def self.f\n Time.new(in: '+09:00').hour\n end\nend\n",
+ Risk::ClockAndRandom,
+ ),
(
Lang::Php,
"x.php",
@@ -1664,6 +1727,10 @@ mod tests {
for code in [
"module A\n def self.rate\n Audio.sample_rate\n end\nend\n",
"module A\n def self.n\n DATA.samples.size\n end\nend\n",
+ "module A\n def self.y2k_weekday\n Time.new(2000, 1, 1).wday\n end\nend\n",
+ "module A\n def self.weekday(y, m, d)\n Time.new(y, m, d).wday\n end\nend\n",
+ "module A\n def self.y2k\n Time.new 2000, 1, 1\n end\nend\n",
+ "module A\n def self.y2k\n Time.new (2000)\n end\nend\n",
] {
let facts = crate::lang::scan(Lang::Ruby, "x.rb", code);
assert!(