mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 04:01:11 +08:00
fix: 移除 repeat 万级静默截断上限
This commit is contained in:
@@ -614,7 +614,7 @@ pub fn string_trim_end<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
NativeResult::Ok(vm.new_string_units_owned(s[..end].to_vec()))
|
NativeResult::Ok(vm.new_string_units_owned(s[..end].to_vec()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `String.prototype.repeat(count)`:重复字符串 count 次(当前上限 10000 防滥用)。
|
/// `String.prototype.repeat(count)`:重复字符串 count 次。
|
||||||
pub fn string_repeat<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
pub fn string_repeat<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
||||||
builtins_debug!("String.prototype.repeat called with {} args", args.len());
|
builtins_debug!("String.prototype.repeat called with {} args", args.len());
|
||||||
// count 转换先行(&mut 路径):ToIntegerOrInfinity 传播式,负值与 +Inf
|
// count 转换先行(&mut 路径):ToIntegerOrInfinity 传播式,负值与 +Inf
|
||||||
@@ -627,11 +627,20 @@ pub fn string_repeat<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
if p < 0.0 || p.is_infinite() {
|
if p < 0.0 || p.is_infinite() {
|
||||||
return NativeResult::Err(crate::error::create_range_error(vm, "Invalid count argument"));
|
return NativeResult::Err(crate::error::create_range_error(vm, "Invalid count argument"));
|
||||||
}
|
}
|
||||||
(p as usize).min(10000)
|
p
|
||||||
} else {
|
} else {
|
||||||
1
|
1.0
|
||||||
};
|
};
|
||||||
let s = try_string!(this_units(vm, args));
|
let s = try_string!(this_units(vm, args));
|
||||||
|
// 空串或 0 次直接返回空串(规范步 7),免大 count 下空循环。
|
||||||
|
if s.is_empty() || n == 0.0 {
|
||||||
|
return NativeResult::Ok(vm.new_string_units_owned(Vec::new()));
|
||||||
|
}
|
||||||
|
// 重复后总长超 2^53-1 规范上限抛 RangeError(规范步 9)。
|
||||||
|
if (s.len() as f64) * n > 9007199254740991.0 {
|
||||||
|
return NativeResult::Err(crate::error::create_range_error(vm, "Invalid string length"));
|
||||||
|
}
|
||||||
|
let n = n as usize;
|
||||||
let mut out = Vec::with_capacity(s.len() * n);
|
let mut out = Vec::with_capacity(s.len() * n);
|
||||||
for _ in 0..n {
|
for _ in 0..n {
|
||||||
out.extend_from_slice(&s);
|
out.extend_from_slice(&s);
|
||||||
|
|||||||
Reference in New Issue
Block a user