Skip to content

Commit 1cd2c1c

Browse files
committed
Merge branch '2.11'
2 parents 7fe4494 + 1000b22 commit 1cd2c1c

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ David Nault (dnault@github)
196196
(2.10.0)
197197

198198
Fabien Renaud (fabienrenaud@github)
199-
* Reported, contributed fix fir #533: UTF-8 BOM not accounted for in
199+
* Reported, contributed fix for #533: UTF-8 BOM not accounted for in
200200
`JsonLocation.getByteOffset()`
201201
(2.10.0)
202+
* Reported, contributed fix for #603: 'JsonParser.getCurrentLocation()`
203+
byte/char offset update incorrectly for big payloads
204+
(2.10.3)
202205

203206
Todd O'Bryan (toddobryan@github)
204207
* Contributed fix fox #455: Jackson reports wrong locations for JsonEOFException

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ JSON library.
2727

2828
#592: DataFormatMatcher#getMatchedFormatName throws NPE when no match exists
2929
(reported by Scott L)
30+
#603: 'JsonParser.getCurrentLocation()` byte/char offset update incorrectly for big payloads
31+
(reported, fix contributed by Fabien R)
3032

3133
2.10.2 (05-Jan-2020)
3234

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,10 @@ protected void _loadMoreGuaranteed() throws IOException {
216216

217217
protected boolean _loadMore() throws IOException
218218
{
219-
final int bufSize = _inputEnd;
220-
221219
if (_reader != null) {
222220
int count = _reader.read(_inputBuffer, 0, _inputBuffer.length);
223221
if (count > 0) {
224-
_inputPtr = 0;
225-
_inputEnd = count;
226-
222+
final int bufSize = _inputEnd;
227223
_currInputProcessed += bufSize;
228224
_currInputRowStart -= bufSize;
229225

@@ -232,6 +228,9 @@ protected boolean _loadMore() throws IOException
232228
// in negative value, which is fine as combine value remains unchanged.
233229
_nameStartOffset -= bufSize;
234230

231+
_inputPtr = 0;
232+
_inputEnd = count;
233+
235234
return true;
236235
}
237236
// End of input

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public Object getInputSource() {
184184

185185
protected final boolean _loadMore() throws IOException
186186
{
187-
final int bufSize = _inputEnd;
188187
if (_inputStream != null) {
189188
int space = _inputBuffer.length;
190189
if (space == 0) { // only occurs when we've been closed
@@ -193,17 +192,19 @@ protected final boolean _loadMore() throws IOException
193192

194193
int count = _inputStream.read(_inputBuffer, 0, space);
195194
if (count > 0) {
196-
_inputPtr = 0;
197-
_inputEnd = count;
195+
final int bufSize = _inputEnd;
198196

199-
_currInputProcessed += _inputEnd;
200-
_currInputRowStart -= _inputEnd;
197+
_currInputProcessed += bufSize;
198+
_currInputRowStart -= bufSize;
201199

202200
// 26-Nov-2015, tatu: Since name-offset requires it too, must offset
203201
// this increase to avoid "moving" name-offset, resulting most likely
204202
// in negative value, which is fine as combine value remains unchanged.
205203
_nameStartOffset -= bufSize;
206204

205+
_inputPtr = 0;
206+
_inputEnd = count;
207+
207208
return true;
208209
}
209210
// End of input

src/test/java/com/fasterxml/jackson/core/read/LocationOffsetsTest.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.fasterxml.jackson.core.*;
44
import com.fasterxml.jackson.core.json.JsonFactory;
55

6+
import java.io.IOException;
7+
import java.util.Random;
8+
69
public class LocationOffsetsTest extends com.fasterxml.jackson.core.BaseTest
710
{
811
final JsonFactory JSON_F = new JsonFactory();
@@ -143,7 +146,7 @@ private void _testWithLazyStringRead(int readMode) throws Exception
143146
assertEquals(8, p.getCurrentLocation().getColumnNr());
144147
p.close();
145148
}
146-
149+
147150
// for [core#533]
148151
public void testUtf8Bom() throws Exception
149152
{
@@ -232,4 +235,75 @@ private byte[] withUtf8Bom(byte[] bytes) {
232235
System.arraycopy(bytes, 0, arr, 3, bytes.length);
233236
return arr;
234237
}
238+
239+
// [core#603]
240+
public void testBigPayload() throws IOException {
241+
JsonLocation loc;
242+
JsonParser p;
243+
244+
String doc = "{\"key\":\"" + generateRandomAlpha(50000) + "\"}";
245+
246+
p = createParserUsingStream(JSON_F, doc, "UTF-8");
247+
248+
assertToken(JsonToken.START_OBJECT, p.nextToken());
249+
loc = p.getTokenLocation();
250+
assertEquals(0, loc.getByteOffset());
251+
assertEquals(-1L, loc.getCharOffset());
252+
assertEquals(1, loc.getLineNr());
253+
assertEquals(1, loc.getColumnNr());
254+
loc = p.getCurrentLocation();
255+
assertEquals(1, loc.getByteOffset());
256+
assertEquals(-1L, loc.getCharOffset());
257+
assertEquals(1, loc.getLineNr());
258+
assertEquals(2, loc.getColumnNr());
259+
260+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
261+
loc = p.getTokenLocation();
262+
assertEquals(1, loc.getByteOffset());
263+
assertEquals(-1L, loc.getCharOffset());
264+
assertEquals(1, loc.getLineNr());
265+
assertEquals(2, loc.getColumnNr());
266+
loc = p.getCurrentLocation();
267+
assertEquals(8, loc.getByteOffset());
268+
assertEquals(-1L, loc.getCharOffset());
269+
assertEquals(1, loc.getLineNr());
270+
assertEquals(9, loc.getColumnNr());
271+
272+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
273+
loc = p.getTokenLocation();
274+
assertEquals(7, loc.getByteOffset());
275+
assertEquals(-1L, loc.getCharOffset());
276+
assertEquals(1, loc.getLineNr());
277+
assertEquals(8, loc.getColumnNr());
278+
loc = p.getCurrentLocation();
279+
assertEquals(8, loc.getByteOffset());
280+
assertEquals(-1L, loc.getCharOffset());
281+
assertEquals(1, loc.getLineNr());
282+
assertEquals(9, loc.getColumnNr());
283+
284+
p.getTextCharacters();
285+
loc = p.getTokenLocation();
286+
assertEquals(7, loc.getByteOffset());
287+
assertEquals(-1L, loc.getCharOffset());
288+
assertEquals(1, loc.getLineNr());
289+
assertEquals(8, loc.getColumnNr());
290+
loc = p.getCurrentLocation();
291+
assertEquals(doc.length() - 1, loc.getByteOffset());
292+
assertEquals(-1L, loc.getCharOffset());
293+
assertEquals(1, loc.getLineNr());
294+
assertEquals(doc.length(), loc.getColumnNr());
295+
296+
p.close();
297+
}
298+
299+
private String generateRandomAlpha(int length) {
300+
StringBuilder sb = new StringBuilder(length);
301+
Random rnd = new Random(length);
302+
for (int i = 0; i < length; ++i) {
303+
// let's limit it not to include surrogate pairs:
304+
char ch = (char) ('A' + rnd.nextInt(26));
305+
sb.append(ch);
306+
}
307+
return sb.toString();
308+
}
235309
}

0 commit comments

Comments
 (0)