From 748f3d43e9492b8f25355ec5fd907c63c8becf0d Mon Sep 17 00:00:00 2001 From: Ralph Date: Wed, 1 Jul 2026 13:47:24 -0700 Subject: [PATCH 1/2] =?UTF-8?q?fix(runtime):=20#5836=20=E2=80=94=20RegExp?= =?UTF-8?q?=20constructor=20edge=20cases=20(not-a-constructor,=20class-ran?= =?UTF-8?q?ge=20order)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `new` on a RegExp instance now throws TypeError (RegExp has no [[Construct]] internal method, ECMA-262 22.2.7) instead of silently producing an empty placeholder object. `[a--z]`-style character classes made out-of-order by a doubled hyphen now throw SyntaxError: JS parses "a--z" as ClassAtom `a`, a `-` range operator, then ClassAtom `-` (a literal hyphen is itself a valid ClassAtom) — i.e. the range a..'-'. Since 'a' > '-', CharacterRange requires this to be rejected, but the Rust `regex`/`fancy-regex` crates parse the doubled hyphen differently and silently accept the pattern. Fixes test262 built-ins/RegExp/S15.10.7_A2_T2.js and S15.10.4.1_A9_T3.js. Verified zero regressions on the full built-ins/RegExp test262 subset (1082 pass, parity unchanged at 92%). --- .../src/object/class_registry/construct.rs | 10 +++ crates/perry-runtime/src/regex.rs | 10 +++ .../src/regex/class_range_validate.rs | 68 +++++++++++++++++++ crates/perry-runtime/src/regex/compile.rs | 7 ++ 4 files changed, 95 insertions(+) create mode 100644 crates/perry-runtime/src/regex/class_range_validate.rs diff --git a/crates/perry-runtime/src/object/class_registry/construct.rs b/crates/perry-runtime/src/object/class_registry/construct.rs index 7d41c9aabb0..67d9de49a24 100644 --- a/crates/perry-runtime/src/object/class_registry/construct.rs +++ b/crates/perry-runtime/src/object/class_registry/construct.rs @@ -223,6 +223,16 @@ pub unsafe extern "C" fn js_new_function_construct( if crate::builtins::boxed_primitive_payload(func_value).is_some() { super::super::object_ops::throw_object_type_error(b"is not a constructor"); } + // `new (new RegExp())` — a RegExp instance has no [[Construct]] internal + // method (Test262 `S15.10.7_A2_T2`). Without this it fell through to the + // empty-object construction fallback and silently produced `{}` instead + // of throwing. + { + let jv = crate::value::JSValue::from_bits(func_value.to_bits()); + if jv.is_pointer() && crate::regex::is_registered_regex(jv.as_pointer::() as usize) { + super::super::object_ops::throw_object_type_error(b"is not a constructor"); + } + } // #3656: `new p()` where `p` is a Proxy dispatches through its `construct` // trap (or forwards to the target). Reached when the compiler can't prove // the callee is a proxy statically (e.g. `new record.proxy()`). newTarget diff --git a/crates/perry-runtime/src/regex.rs b/crates/perry-runtime/src/regex.rs index fa9938fc197..b35d91e231d 100644 --- a/crates/perry-runtime/src/regex.rs +++ b/crates/perry-runtime/src/regex.rs @@ -28,6 +28,8 @@ type CompiledRegex = regex::Regex; #[cfg(not(feature = "regex-engine"))] type CompiledRegex = (); +#[cfg(feature = "regex-engine")] +mod class_range_validate; #[cfg(feature = "regex-engine")] mod compile; mod escape; @@ -41,6 +43,8 @@ mod match_all; mod replace_expand; mod replace_fn; #[cfg(feature = "regex-engine")] +use class_range_validate::has_out_of_order_double_dash_class_range; +#[cfg(feature = "regex-engine")] pub use compile::js_regexp_compile_value; pub use escape::js_regexp_escape; #[cfg(feature = "regex-engine")] @@ -587,6 +591,12 @@ pub extern "C" fn js_regexp_new( pattern_str )); } + if has_out_of_order_double_dash_class_range(pattern_str) { + throw_regexp_syntax_error(&format!( + "Invalid regular expression: /{}/: invalid pattern", + pattern_str + )); + } // Annex B.1.4 legacy escapes (`\1` non-backref octal, `\0DD`, `\8`/`\9`, // `\c` without a control letter) are accepted in sloppy patterns but are // a hard SyntaxError under the `/u` (and `/v`) flag — `js_regex_to_rust` diff --git a/crates/perry-runtime/src/regex/class_range_validate.rs b/crates/perry-runtime/src/regex/class_range_validate.rs new file mode 100644 index 00000000000..9aa71bbceff --- /dev/null +++ b/crates/perry-runtime/src/regex/class_range_validate.rs @@ -0,0 +1,68 @@ +//! JS-spec character-class range validation the underlying `regex` / +//! `fancy-regex` crates don't perform themselves. Split out of `grammar.rs` +//! to keep that file under the 2000-line size gate. + +/// Detect a character-class range made out of order by a doubled hyphen +/// (`[a--z]`): JS parses the class contents "a--z" as ClassAtom `a`, a `-` +/// range operator, ClassAtom `-` (a literal hyphen is itself a valid +/// ClassAtom) — i.e. the range `a`..`-`. Since `a` (U+0061) is greater than +/// `-` (U+002D), `CharacterRange` (22.2.1.1) requires the low bound's code +/// point to be no greater than the high bound's, so this is a SyntaxError. +/// The Rust `regex` crate parses the doubled hyphen differently and +/// silently accepts patterns like this, so Perry must catch it before +/// delegating to the crate (test262 `S15.10.4.1_A9_T3`). +pub(super) fn has_out_of_order_double_dash_class_range(pattern: &str) -> bool { + let chars: Vec = pattern.chars().collect(); + let mut i = 0; + let mut in_class = false; + while i < chars.len() { + match chars[i] { + '\\' => i += 2, + '[' if !in_class => { + in_class = true; + i += 1; + } + ']' if in_class => { + in_class = false; + i += 1; + } + c if in_class + && c != '^' + && chars.get(i + 1) == Some(&'-') + && chars.get(i + 2) == Some(&'-') + && !matches!(chars.get(i + 3), None | Some(']')) => + { + if (c as u32) > ('-' as u32) { + return true; + } + i += 1; + } + _ => i += 1, + } + } + false +} + +#[cfg(test)] +mod tests { + use super::has_out_of_order_double_dash_class_range; + + #[test] + fn rejects_descending_double_dash_ranges() { + assert!(has_out_of_order_double_dash_class_range("[a--z]")); + assert!(has_out_of_order_double_dash_class_range("[a---z]")); + } + + #[test] + fn accepts_ascending_double_dash_ranges() { + assert!(!has_out_of_order_double_dash_class_range("[!--z]")); + assert!(!has_out_of_order_double_dash_class_range("[+--z]")); + } + + #[test] + fn ignores_unrelated_class_hyphens() { + assert!(!has_out_of_order_double_dash_class_range("[a-z-]")); + assert!(!has_out_of_order_double_dash_class_range("[a-z]")); + assert!(!has_out_of_order_double_dash_class_range("abc")); + } +} diff --git a/crates/perry-runtime/src/regex/compile.rs b/crates/perry-runtime/src/regex/compile.rs index 581e5af38c6..2af95352c99 100644 --- a/crates/perry-runtime/src/regex/compile.rs +++ b/crates/perry-runtime/src/regex/compile.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use regex::Regex; +use super::class_range_validate::has_out_of_order_double_dash_class_range; use super::grammar::{ has_invalid_repeated_quantifier, has_unicode_forbidden_legacy_escape, has_unicode_forbidden_pattern, js_regex_to_rust, @@ -92,6 +93,12 @@ pub extern "C" fn js_regexp_compile_value( pattern_str )); } + if has_out_of_order_double_dash_class_range(pattern_str) { + throw_regexp_syntax_error(&format!( + "Invalid regular expression: /{}/: invalid pattern", + pattern_str + )); + } // Annex B.1.4 leniencies are hard `SyntaxError`s under `/u` (mirror of // `js_regexp_new`): legacy escapes for `u`/`v`, plus the structural // restrictions for `u` specifically. From 918ac3f108d3cdad491376c34a31792999416e8e Mon Sep 17 00:00:00 2001 From: Ralph Date: Wed, 1 Jul 2026 14:35:23 -0700 Subject: [PATCH 2/2] fix(runtime): address CodeRabbit review on #5858 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Skip the doubled-hyphen class-range check under the `v` flag: `--` is the real ClassSetExpression subtraction operator there (UTS #51), so `[a--z]` is valid set-difference syntax, not a malformed range. Verified against Node: `/[a--z]/v` doesn't throw, `/[a--z]/u` and `/[a--z]/` do. - Fix `^` handling to be position-aware: it's only a negation marker as the class's first ClassAtom (`[^a]`); anywhere else (`[x^--z]`) it's an ordinary ClassAtom that must still be range-order-checked (`^` is U+005E, greater than `-`, so Node throws for `[x^--z]` too). The `]`-position claim in the same review comment doesn't hold up: verified against both Node (`new RegExp("[]a-z]")` matches nothing — consistent with `[]` closing as an empty, never-matching class, not a POSIX-style literal `]`) and the existing `js_regex_to_rust` translator, which already treats a `]` immediately after `[` as closing an empty class. Left unchanged. --- crates/perry-runtime/src/regex.rs | 6 +++- .../src/regex/class_range_validate.rs | 30 +++++++++++++++++-- crates/perry-runtime/src/regex/compile.rs | 4 ++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/perry-runtime/src/regex.rs b/crates/perry-runtime/src/regex.rs index b35d91e231d..5994a920deb 100644 --- a/crates/perry-runtime/src/regex.rs +++ b/crates/perry-runtime/src/regex.rs @@ -591,7 +591,11 @@ pub extern "C" fn js_regexp_new( pattern_str )); } - if has_out_of_order_double_dash_class_range(pattern_str) { + // `--` is the real ClassSetExpression subtraction operator under + // the `v` flag (UTS #51) — `[a--z]` there means "a minus z", not + // a malformed range — so only legacy/`u`-mode patterns are + // subject to the doubled-hyphen range-order check. + if !flags_str.contains('v') && has_out_of_order_double_dash_class_range(pattern_str) { throw_regexp_syntax_error(&format!( "Invalid regular expression: /{}/: invalid pattern", pattern_str diff --git a/crates/perry-runtime/src/regex/class_range_validate.rs b/crates/perry-runtime/src/regex/class_range_validate.rs index 9aa71bbceff..2ad7abde6a0 100644 --- a/crates/perry-runtime/src/regex/class_range_validate.rs +++ b/crates/perry-runtime/src/regex/class_range_validate.rs @@ -15,19 +15,30 @@ pub(super) fn has_out_of_order_double_dash_class_range(pattern: &str) -> bool { let chars: Vec = pattern.chars().collect(); let mut i = 0; let mut in_class = false; + // True for the first ClassAtom position of the current class — `^` is + // only a negation marker there (`[^a]`); a `^` anywhere else (`[x^--z]`) + // is an ordinary ClassAtom and must still be range-order-checked. + let mut at_class_start = false; while i < chars.len() { match chars[i] { - '\\' => i += 2, + '\\' => { + i += 2; + at_class_start = false; + } '[' if !in_class => { in_class = true; + at_class_start = true; i += 1; } ']' if in_class => { in_class = false; i += 1; } + '^' if in_class && at_class_start => { + at_class_start = false; + i += 1; + } c if in_class - && c != '^' && chars.get(i + 1) == Some(&'-') && chars.get(i + 2) == Some(&'-') && !matches!(chars.get(i + 3), None | Some(']')) => @@ -35,9 +46,13 @@ pub(super) fn has_out_of_order_double_dash_class_range(pattern: &str) -> bool { if (c as u32) > ('-' as u32) { return true; } + at_class_start = false; + i += 1; + } + _ => { + at_class_start = false; i += 1; } - _ => i += 1, } } false @@ -65,4 +80,13 @@ mod tests { assert!(!has_out_of_order_double_dash_class_range("[a-z]")); assert!(!has_out_of_order_double_dash_class_range("abc")); } + + #[test] + fn caret_is_only_a_negation_marker_at_class_start() { + // A leading `^` negates the class and is not itself a range atom. + assert!(!has_out_of_order_double_dash_class_range("[^--z]")); + // A non-leading `^` is an ordinary ClassAtom (U+005E > U+002D `-`), + // so `^--z` is the same out-of-order range as `a--z`. + assert!(has_out_of_order_double_dash_class_range("[x^--z]")); + } } diff --git a/crates/perry-runtime/src/regex/compile.rs b/crates/perry-runtime/src/regex/compile.rs index 2af95352c99..ee35d7dc6e5 100644 --- a/crates/perry-runtime/src/regex/compile.rs +++ b/crates/perry-runtime/src/regex/compile.rs @@ -93,7 +93,9 @@ pub extern "C" fn js_regexp_compile_value( pattern_str )); } - if has_out_of_order_double_dash_class_range(pattern_str) { + // `--` is the real ClassSetExpression subtraction operator under the `v` + // flag (UTS #51) — see the matching comment in `js_regexp_new`. + if !flags_str.contains('v') && has_out_of_order_double_dash_class_range(pattern_str) { throw_regexp_syntax_error(&format!( "Invalid regular expression: /{}/: invalid pattern", pattern_str