Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase max json nesting to 256 #3333

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.apollographql.apollo3.api.internal.json

import com.apollographql.apollo3.api.Throws
import com.apollographql.apollo3.api.Upload
import com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader.Companion.MAX_STACK_SIZE
import com.apollographql.apollo3.api.json.JsonWriter
import com.apollographql.apollo3.exception.JsonDataException
import okio.BufferedSink
Expand All @@ -31,12 +32,12 @@ import okio.IOException
* To writer to a [Map], see also [MapJsonWriter]
*/
class BufferedSinkJsonWriter(private val sink: BufferedSink) : JsonWriter {
// The nesting stack. Using a manual array rather than an ArrayList saves 20%. This stack permits up to 32 levels of nesting including
// The nesting stack. Using a manual array rather than an ArrayList saves 20%. This stack permits up to MAX_STACK_SIZE levels of nesting including
// the top-level document. Deeper nesting is prone to trigger StackOverflowErrors.
private var stackSize = 0
private val scopes = IntArray(32)
private val pathNames = arrayOfNulls<String>(32)
private val pathIndices = IntArray(32)
private val scopes = IntArray(MAX_STACK_SIZE)
private val pathNames = arrayOfNulls<String>(MAX_STACK_SIZE)
private val pathIndices = IntArray(MAX_STACK_SIZE)

/**
* A string containing a full set of spaces for a single level of indentation, or null for no pretty printing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader

/**
* The nesting stack. Using a manual array rather than an ArrayList saves 20%.
* This stack permits up to 32 levels of nesting including the top-level document.
* This stack permits up to MAX_STACK_SIZE levels of nesting including the top-level document.
* Deeper nesting is prone to trigger StackOverflowErrors.
*/
private val stack = IntArray(32).apply {
private val stack = IntArray(MAX_STACK_SIZE).apply {
this[0] = JsonScope.EMPTY_DOCUMENT
}
private var stackSize = 1
private val pathNames = arrayOfNulls<String>(32)
private val pathIndices = IntArray(32)
private val pathNames = arrayOfNulls<String>(MAX_STACK_SIZE)
private val pathIndices = IntArray(MAX_STACK_SIZE)

private var lenient = false

private var failOnUnknown = false

private val indexStack = IntArray(32).apply {
private val indexStack = IntArray(MAX_STACK_SIZE).apply {
this[0] = 0
}
private var indexStackSize = 1
Expand Down Expand Up @@ -967,5 +967,7 @@ class BufferedSourceJsonReader(private val source: BufferedSource) : JsonReader
private const val NUMBER_CHAR_EXP_E = 5
private const val NUMBER_CHAR_EXP_SIGN = 6
private const val NUMBER_CHAR_EXP_DIGIT = 7

const val MAX_STACK_SIZE = 256
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo3.api.internal.json

import com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader.Companion.MAX_STACK_SIZE
import com.apollographql.apollo3.api.internal.json.Utils.readRecursively
import com.apollographql.apollo3.api.json.JsonReader

Expand Down Expand Up @@ -42,10 +43,7 @@ class MapJsonReader(val root: Map<String, Any?>) : JsonReader {
// No need to stack this, when we pop, we know we have to read a new name
private var currentName: String? = "root"

/**
* See [com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader] for the 32 limitation
*/
private val nameIndexStack = IntArray(32).apply {
private val nameIndexStack = IntArray(MAX_STACK_SIZE).apply {
this[0] = 0
}

Expand Down