This is an automated email from the ASF dual-hosted git repository. jdaugherty pushed a commit to branch 7.0.x in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit e77e3cd9cca965b868a8c95592440b06b52c73d6 Author: James Daugherty <[email protected]> AuthorDate: Fri Oct 16 12:56:25 2026 -0400 Report a misplaced endObject as such and let JSONTokener.next(n) read to the end JSONWriter.end() chose its message by comparing the expected mode with OBJECT, but endObject() passes KEY, so "Misplaced endObject." was unreachable and every misplaced end was reported as endArray. The message now follows the closing character being written. PrettyPrintJSONWriter no longer writes a newline and indentation ahead of that check, so a misplaced end leaves the output untouched. JSONTokener.next(n) rejected a request for exactly the remaining characters because its bound used >= instead of >, contrary to its javadoc. --- .../main/groovy/org/grails/web/json/JSONTokener.java | 2 +- .../main/groovy/org/grails/web/json/JSONWriter.java | 3 +-- .../org/grails/web/json/PrettyPrintJSONWriter.java | 6 ++++-- .../org/grails/web/json/JSONTokenerApiSpec.groovy | 10 ++++++++++ .../org/grails/web/json/JSONWriterApiSpec.groovy | 5 ++--- .../web/json/PrettyPrintJSONWriterApiSpec.groovy | 19 +++++++++++++++++-- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java b/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java index 2b0018e8b9..2e72759cb5 100644 --- a/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java +++ b/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java @@ -122,7 +122,7 @@ public class JSONTokener { public String next(int n) throws JSONException { int i = this.myIndex; int j = i + n; - if (j >= this.mySource.length()) { + if (j > this.mySource.length()) { throw syntaxError("Substring bounds error"); } this.myIndex += n; diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java b/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java index b40a42d874..98d829f044 100644 --- a/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java +++ b/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java @@ -161,8 +161,7 @@ public class JSONWriter { */ protected JSONWriter end(Mode m, char c) { if (this.mode != m) { - throw new JSONException(m == OBJECT ? "Misplaced endObject." : - "Misplaced endArray."); + throw new JSONException(c == '}' ? "Misplaced endObject." : "Misplaced endArray."); } this.pop(m); try { diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java b/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java index 864d0daff3..19829b282d 100644 --- a/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java +++ b/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java @@ -104,8 +104,10 @@ public class PrettyPrintJSONWriter extends JSONWriter { @Override protected JSONWriter end(Mode m, char c) { - newline(); - indent(); + if (this.mode == m) { + newline(); + indent(); + } return super.end(m, c); } diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy index 52dc8db523..902ae07a14 100644 --- a/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy +++ b/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy @@ -48,6 +48,16 @@ class JSONTokenerApiSpec extends Specification { tokener.toString() == ' at character 4 of abcd' } + void "next(n) returns the next n characters up to exactly the end of the source"() { + given: + def tokener = new JSONTokener('abc') + + expect: + tokener.next(2) == 'ab' + tokener.next(1) == 'c' + new JSONTokener('abc').next(3) == 'abc' + } + void "character and substring mismatches include the source position"() { when: new JSONTokener('abc').next((char) 'z') diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy index 7c0c4217cb..f2574c31f0 100644 --- a/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy +++ b/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy @@ -105,9 +105,8 @@ class JSONWriterApiSpec extends Specification { ({ JSONWriter w -> w.object() }) | ({ w -> w.array() }) | 'Misplaced array' ({ JSONWriter w -> w.object() }) | ({ w -> w.object() }) | 'Misplaced object' ({ JSONWriter w -> }) | ({ w -> w.endArray() }) | 'Misplaced endArray' - // endObject misuse is matched on the shared prefix only: the writer currently reports it as endArray - ({ JSONWriter w -> }) | ({ w -> w.endObject() }) | 'Misplaced end' - ({ JSONWriter w -> w.object().key('key') }) | ({ w -> w.endObject() }) | 'Misplaced end' + ({ JSONWriter w -> }) | ({ w -> w.endObject() }) | 'Misplaced endObject' + ({ JSONWriter w -> w.object().key('key') }) | ({ w -> w.endObject() }) | 'Misplaced endObject' ({ JSONWriter w -> w.array().endArray() }) | ({ w -> w.array() }) | 'Misplaced array' ({ JSONWriter w -> w.object().endObject() }) | ({ w -> w.object() }) | 'Misplaced object' ({ JSONWriter w -> w.array().endArray() }) | ({ w -> w.value(1L) }) | 'Value out of sequence' diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy index 1651f989df..84230a889c 100644 --- a/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy +++ b/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy @@ -73,11 +73,26 @@ class PrettyPrintJSONWriterApiSpec extends Specification { ({ w -> w.key('key') }) | 'Misplaced key' ({ w -> w.value(true) }) | 'Value out of sequence' ({ w -> w.object().valueNull() }) | 'Value out of sequence' - // endObject misuse is matched on the shared prefix only: the writer currently reports it as endArray - ({ w -> w.array().endObject() }) | 'Misplaced end' + ({ w -> w.array().endObject() }) | 'Misplaced endObject' ({ w -> w.object().endArray() }) | 'Misplaced endArray' } + void "pretty writer writes nothing before rejecting a misplaced end"() { + given: + def output = new StringWriter() + def writer = new PrettyPrintJSONWriter(output) + writer.array() + def written = output.toString() + + when: + writer.endObject() + + then: + def error = thrown(JSONException) + error.message == 'Misplaced endObject.' + output.toString() == written + } + void "pretty writer wraps failures in newlines indentation keys and values"() { given: def output = Stub(Writer) {
