Skip to content

Commit

Permalink
Make SnakeYAML's code point limit configurable
Browse files Browse the repository at this point in the history
Resolves #517.
  • Loading branch information
sschuberth committed Apr 11, 2024
1 parent a31a3ae commit 66953b5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Yaml(
parseToYamlNode(string.bufferedSource())

internal fun parseToYamlNode(source: Source): YamlNode {
val parser = YamlParser(source)
val parser = YamlParser(source, configuration.codePointLimit)
val reader =
YamlNodeReader(parser, configuration.extensionDefinitionPrefix, configuration.allowAnchorsAndAliases)
val node = reader.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package com.charleskorn.kaml
* * [ambiguousQuoteStyle]: how strings should be escaped when [singleLineStringStyle] is [SingleLineStringStyle.PlainExceptAmbiguous] and the value is ambiguous
* * [sequenceBlockIndent]: number of spaces to use as indentation for sequences, if [sequenceStyle] set to [SequenceStyle.Block]
* * [allowAnchorsAndAliases]: set to true to allow anchors and aliases when decoding YAML (defaults to `false`)
* * [codePointLimit]: the maximum amount of code points allowed in the input YAML document (defaults to 3 MB)
*/
public data class YamlConfiguration(
internal val encodeDefaults: Boolean = true,
Expand All @@ -50,6 +51,7 @@ public data class YamlConfiguration(
internal val sequenceBlockIndent: Int = 0,
internal val allowAnchorsAndAliases: Boolean = false,
internal val yamlNamingStrategy: YamlNamingStrategy? = null,
internal val codePointLimit: Int? = null,
)

public enum class PolymorphismStyle {
Expand Down
7 changes: 5 additions & 2 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import org.snakeyaml.engine.v2.exceptions.MarkedYamlEngineException
import org.snakeyaml.engine.v2.parser.ParserImpl
import org.snakeyaml.engine.v2.scanner.StreamReader

internal class YamlParser(reader: Source) {
internal class YamlParser(reader: Source, codePointLimit: Int? = null) {
internal constructor(source: String) : this(source.bufferedSource())

private val dummyFileName = "DUMMY_FILE_NAME"
private val loadSettings = LoadSettings.builder().setLabel(dummyFileName).build()
private val loadSettings = LoadSettings.builder().apply {
if (codePointLimit != null) setCodePointLimit(codePointLimit)
setLabel(dummyFileName)
}.build()
private val streamReader = StreamReader(loadSettings, reader)
private val events = ParserImpl(loadSettings, streamReader)

Expand Down

0 comments on commit 66953b5

Please sign in to comment.