{"id":"0d869ce06e3ee26e","repo":"google/gson","slug":"invalid-nesting-limit-limit","errorCode":null,"errorMessage":"Invalid nesting limit: \" + limit","messagePattern":"Invalid nesting limit: \" \\+ limit","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonReader.java","lineNumber":437,"sourceCode":"   *\n   * <p>The nesting limit defines how many JSON arrays or objects may be open at the same time. For\n   * example a nesting limit of 0 means no arrays or objects may be opened at all, a nesting limit\n   * of 1 means one array or object may be open at the same time, and so on. So a nesting limit of 3\n   * allows reading the JSON data <code>[{\"a\":[true]}]</code>, but for a nesting limit of 2 it would\n   * fail at the inner {@code [true]}.\n   *\n   * <p>The nesting limit can help to protect against a {@link StackOverflowError} when recursive\n   * {@link com.google.gson.TypeAdapter} implementations process deeply nested JSON data.\n   *\n   * <p>The default nesting limit is {@value #DEFAULT_NESTING_LIMIT}.\n   *\n   * @throws IllegalArgumentException if the nesting limit is negative.\n   * @since 2.12.0\n   * @see #getNestingLimit()\n   */\n  public final void setNestingLimit(int limit) {\n    if (limit < 0) {\n      throw new IllegalArgumentException(\"Invalid nesting limit: \" + limit);\n    }\n    this.nestingLimit = limit;\n  }\n\n  /**\n   * Returns the nesting limit of this reader.\n   *\n   * @since 2.12.0\n   * @see #setNestingLimit(int)\n   */\n  public final int getNestingLimit() {\n    return nestingLimit;\n  }\n\n  /**\n   * Consumes the next token from the JSON stream and asserts that it is the beginning of a new\n   * array.\n   *","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonReader.java#L419-L455","documentation":"Thrown by JsonReader.setNestingLimit(int) when the supplied limit is negative. The nesting limit defines how many arrays/objects may be open concurrently; a negative value is meaningless, so it is rejected immediately with IllegalArgumentException. Zero and positive values are accepted (0 means no structures may be opened at all).","triggerScenarios":"Calling reader.setNestingLimit(-1) or any value < 0. Common when the limit is computed from configuration or arithmetic that can go negative, or when -1 is used as a sentinel meaning 'unlimited' (which Gson does not support).","commonSituations":"Reading a nesting limit from a config file/property that defaults to -1 for 'unlimited'; computing limit = actualDepth - someOffset and underflowing; porting code from a library that treats -1 as infinity.","solutions":["Pass a non-negative value; to disable the limit use a large number (default is 255).","If reading from config, map -1/unlimited to Integer.MAX_VALUE or to the DEFAULT_NESTING_LIMIT before calling setNestingLimit.","Validate the value with Math.max(0, configuredLimit) before passing it in.","Treat 0 as 'no nesting allowed' intentionally if you want to reject all structured JSON."],"exampleFix":"// before\nint configured = config.getInt(\"nesting\", -1); // -1 means unlimited\nreader.setNestingLimit(configured); // throws when -1\n\n// after\nint configured = config.getInt(\"nesting\", JsonReader.DEFAULT_NESTING_LIMIT);\nint safe = (configured < 0) ? Integer.MAX_VALUE : configured;\nreader.setNestingLimit(safe);","handlingStrategy":"validation","validationCode":"int safeNestingLimit(int configured) {\n  if (configured < 0) {\n    // treat negative as 'unlimited' sentinel -> use a large positive value\n    return Integer.MAX_VALUE;\n  }\n  return configured;\n}\nreader.setNestingLimit(safeNestingLimit(configured));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate that configured nesting limits are non-negative before passing them in.","Map '-1 means unlimited' sentinels to Integer.MAX_VALUE explicitly.","Default to JsonReader.DEFAULT_NESTING_LIMIT (255) when config is absent.","Document that 0 means 'no arrays/objects allowed' so callers don't use it by accident."],"tags":["gson","jsonreader","validation","configuration"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}