Commit 7c365ec
authored
Prevent IndexOutOfRangeException in RegexInterpreter (#97916)
* Prevent IndexOutOfRangeException in RegexInterpreter
This update fixes the IndexOutOfRangeException in RegexInterpreter by enhancing the `TrackPush` and `TrackPush2` methods. The adjustment involves checking the runtrack position before decrementing it, ensuring that it doesn't become negative, which was the root cause of the exception. This prevents potential out-of-range errors when handling large numbers of repetitions in regular expressions.
Fix #62094
* Changed to call EnsureStorage() unconditionally.
If EnsureStorage() is called unconditionally, the array will be expanded, so the position will never become negative. When the conditions inside EnsureStorage() are true, it might be necessary to expand the array, regardless of the comparison between newpos and codepos.
https://github.com/dotnet/runtime/blob/6ebc8bd86dbc780b2a2a7daf3ab6020f9104f09e/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.MultipleMatches.Tests.cs#L461-L469
Before the change, in this test case, EnsureStorage() is not called because newpos == codepos == 6 from the first time until an exception occurs.
Fix #620491 parent 1e05afc commit 7c365ec
File tree
2 files changed
+9
-19
lines changed- src/libraries/System.Text.RegularExpressions
- src/System/Text/RegularExpressions
- tests/FunctionalTests
2 files changed
+9
-19
lines changedLines changed: 2 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 56 | + | |
60 | 57 | | |
61 | 58 | | |
62 | 59 | | |
| |||
144 | 141 | | |
145 | 142 | | |
146 | 143 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 144 | + | |
151 | 145 | | |
152 | 146 | | |
153 | 147 | | |
| |||
Lines changed: 7 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
457 | | - | |
458 | | - | |
| 457 | + | |
| 458 | + | |
459 | 459 | | |
460 | | - | |
461 | | - | |
| 460 | + | |
462 | 461 | | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
470 | 466 | | |
471 | 467 | | |
472 | 468 | | |
| |||
0 commit comments