Skip to content

Commit

Permalink
Ensure input length is not overflown after a match
Browse files Browse the repository at this point in the history
  • Loading branch information
alllex committed Oct 7, 2023
1 parent d1e0b33 commit 4876834
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal class ParsingContext(
private val debugMode: Boolean = false
) : ParsingScope {

private val inputLength = tokenizer.input.length

private var backtrackCont: Continuation<ParseError>? = null
private var cont: Continuation<Any?>? = null
private var position: Int = 0
Expand Down Expand Up @@ -63,10 +65,10 @@ internal class ParsingContext(
val match = tokenizer.findMatchOf(fromIndex, token)
?: return UnmatchedToken(token, fromIndex, getParseErrorContextProviderOrNull())

// TODO: clean up, as this should not happen anymore
// This can only happen with EagerTokenizer
if (match.token != token) return MismatchedToken(token, match, getParseErrorContextProviderOrNull())

val newPosition = match.nextOffset
val newPosition = match.nextOffset.coerceAtMost(inputLength)
this.position = newPosition
this.lastTokenMatchContext.currentOffset = newPosition
this.lastTokenMatchContext.lastMatch = match
Expand Down
15 changes: 15 additions & 0 deletions src/commonTest/kotlin/me/alllex/parsus/TokenTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@ class TokenTests {
}
}

@Test
fun explicitEofMatchesDoNotOverflowInputLength() {
object : Grammar<List<TokenMatch>>() {
val ab by literalToken("ab")
val eof by EofToken
override val root by ab * eof * eof map { it.toList() }
}.run {
assertParsed("ab").isEqualTo(listOf(
TokenMatch(ab, 0, 2),
TokenMatch(EofToken, 2, 1),
TokenMatch(EofToken, 2, 1),
))
}
}

}

0 comments on commit 4876834

Please sign in to comment.