Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 634f92d

Browse files
committed
在模式解析器中添加对“或”模式的数量限制,防止无限循环,并增强了元组和数组模式的解析能力,支持嵌套“或”模式的解析。
1 parent fd32578 commit 634f92d

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/parser/pattern_parser.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,17 @@ impl<'a> PatternParser for ParserBase<'a> {
176176
/// 解析或模式 (pattern1 | pattern2 | pattern3)
177177
fn parse_pattern_or(&mut self) -> Result<Pattern, String> {
178178
let mut patterns = vec![self.parse_pattern_primary()?];
179-
179+
let mut or_count = 0;
180+
const MAX_OR_PATTERNS: usize = 100; // 限制最大或模式数量
181+
180182
while self.consume_symbol("|") {
183+
or_count += 1;
184+
if or_count > MAX_OR_PATTERNS {
185+
return Err("或模式数量过多,可能存在无限循环".to_string());
186+
}
181187
patterns.push(self.parse_pattern_primary()?);
182188
}
183-
189+
184190
if patterns.len() == 1 {
185191
Ok(patterns.into_iter().next().unwrap())
186192
} else {
@@ -202,45 +208,45 @@ impl<'a> PatternParser for ParserBase<'a> {
202208
"(" => {
203209
self.advance();
204210
let mut patterns = Vec::new();
205-
211+
206212
if !self.check_symbol(")") {
207-
patterns.push(self.parse_pattern()?);
208-
213+
patterns.push(self.parse_pattern_or()?);
214+
209215
while self.consume_symbol(",") {
210216
if self.check_symbol(")") {
211217
break;
212218
}
213-
patterns.push(self.parse_pattern()?);
219+
patterns.push(self.parse_pattern_or()?);
214220
}
215221
}
216-
222+
217223
if !self.consume_symbol(")") {
218224
return Err("期望 ')' 结束元组模式".to_string());
219225
}
220-
226+
221227
Ok(Pattern::Tuple(patterns))
222228
},
223229

224230
// 数组模式
225231
"[" => {
226232
self.advance();
227233
let mut patterns = Vec::new();
228-
234+
229235
if !self.check_symbol("]") {
230-
patterns.push(self.parse_pattern()?);
231-
236+
patterns.push(self.parse_pattern_or()?);
237+
232238
while self.consume_symbol(",") {
233239
if self.check_symbol("]") {
234240
break;
235241
}
236-
patterns.push(self.parse_pattern()?);
242+
patterns.push(self.parse_pattern_or()?);
237243
}
238244
}
239-
245+
240246
if !self.consume_symbol("]") {
241247
return Err("期望 ']' 结束数组模式".to_string());
242248
}
243-
249+
244250
Ok(Pattern::Array(patterns))
245251
},
246252

@@ -291,22 +297,22 @@ impl<'a> PatternParser for ParserBase<'a> {
291297
// 检查是否有参数
292298
let params = if self.consume_symbol("(") {
293299
let mut patterns = Vec::new();
294-
300+
295301
if !self.check_symbol(")") {
296-
patterns.push(self.parse_pattern()?);
297-
302+
patterns.push(self.parse_pattern_or()?);
303+
298304
while self.consume_symbol(",") {
299305
if self.check_symbol(")") {
300306
break;
301307
}
302-
patterns.push(self.parse_pattern()?);
308+
patterns.push(self.parse_pattern_or()?);
303309
}
304310
}
305-
311+
306312
if !self.consume_symbol(")") {
307313
return Err("期望 ')' 结束枚举变体参数".to_string());
308314
}
309-
315+
310316
patterns
311317
} else {
312318
Vec::new()

0 commit comments

Comments
 (0)