[parsing/mfm] Further limit recursion depth to 20

This commit is contained in:
Laura Hausmann 2024-11-27 02:50:02 +01:00
parent d6f4f5bd51
commit 59ab914f0a
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 6 additions and 6 deletions

View file

@ -116,7 +116,7 @@ module MfmNodeTypes =
type internal UserState = type internal UserState =
{ ParenthesisStack: char list { ParenthesisStack: char list
LastLine: int64 LastLine: int64
Depth: int64 Depth: int
TimeoutAt: int64 } TimeoutAt: int64 }
member this.TimeoutReached = Stopwatch.GetTimestamp() > this.TimeoutAt member this.TimeoutReached = Stopwatch.GetTimestamp() > this.TimeoutAt
@ -561,7 +561,7 @@ module private MfmParser =
let prefixedNode (m: ParseMode) : Parser<MfmNode, UserState> = let prefixedNode (m: ParseMode) : Parser<MfmNode, UserState> =
fun (stream: CharStream<_>) -> fun (stream: CharStream<_>) ->
match stream.UserState.Depth with match stream.UserState.Depth with
| GreaterEqualThan 100L -> stream |> charNode | GreaterEqualThan 20 -> stream |> charNode
| _ -> | _ ->
match (stream.Peek(), m) with match (stream.Peek(), m) with
// Block nodes, ordered by expected frequency // Block nodes, ordered by expected frequency
@ -593,8 +593,8 @@ module private MfmParser =
failIfTimeout >>. (attempt <| prefixedNode m <|> charNode) failIfTimeout >>. (attempt <| prefixedNode m <|> charNode)
// Populate references // Populate references
let pushDepth = updateUserState (fun u -> { u with Depth = (u.Depth + 1L) }) let pushDepth = updateUserState (fun u -> { u with Depth = (u.Depth + 1) })
let popDepth = updateUserState (fun u -> { u with Depth = (u.Depth - 1L) }) let popDepth = updateUserState (fun u -> { u with Depth = (u.Depth - 1) })
do inlineNodeRef.Value <- pushDepth >>. (parseNode Inline |>> fun v -> v :?> MfmInlineNode) .>> popDepth do inlineNodeRef.Value <- pushDepth >>. (parseNode Inline |>> fun v -> v :?> MfmInlineNode) .>> popDepth
// Parser abstractions // Parser abstractions

View file

@ -526,8 +526,8 @@ public class MfmTests
[TestMethod] [TestMethod]
public void TestFnRecursionLimit() public void TestFnRecursionLimit()
{ {
const int iterations = 150; const int iterations = 50;
const int limit = 100; const int limit = 20;
var input = GetMfm(iterations); var input = GetMfm(iterations);
var result = Mfm.parse(input); var result = Mfm.parse(input);