Skip to content

Commit 456b20f

Browse files
committed
Bit more checking wrt JsonLocation (and #739)
1 parent c5aa1e1 commit 456b20f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/main/java/com/fasterxml/jackson/core/io/ContentReference.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ public boolean equals(Object other)
360360
if (!(other instanceof ContentReference)) return false;
361361
ContentReference otherSrc = (ContentReference) other;
362362

363+
// 16-Jan-2022, tatu: First ensure offset/length the same
364+
if ((_offset != otherSrc._offset)
365+
|| (_length != otherSrc._length)) {
366+
return false;
367+
}
368+
363369
// 16-Jan-2022, tatu: As per [core#739] we'll want to consider some
364370
// but not all content cases with real equality: the concern here is
365371
// to avoid expensive comparisons and/or possible security issues
@@ -370,7 +376,7 @@ public boolean equals(Object other)
370376
} else if (otherRaw == null) {
371377
return false;
372378
}
373-
379+
374380
if ((_rawContent instanceof File)
375381
|| (_rawContent instanceof URL)
376382
|| (_rawContent instanceof URI)

src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ public void testLocationEquality() throws Exception
135135
JsonLocation loc2 = new JsonLocation(_sourceRef(src2),
136136
10L, 10L, 1, 2);
137137
assertEquals(loc1, loc2);
138+
139+
// Also make sure to consider offset/length
140+
final byte[] bogus = "BOGUS".getBytes();
141+
142+
// If same, equals:
143+
assertEquals(new JsonLocation(_sourceRef(bogus, 0, 5), 5L, 0L, 1, 2),
144+
new JsonLocation(_sourceRef(bogus, 0, 5), 5L, 0L, 1, 2));
145+
146+
// If different, not equals
147+
loc1 = new JsonLocation(_sourceRef(bogus, 0, 5),
148+
5L, 0L, 1, 2);
149+
loc2 = new JsonLocation(_sourceRef(bogus, 1, 4),
150+
5L, 0L, 1, 2);
151+
assertFalse(loc1.equals(loc2));
152+
assertFalse(loc2.equals(loc1));
138153
}
139154

140155
private ContentReference _sourceRef(String rawSrc) {
@@ -149,6 +164,10 @@ private ContentReference _sourceRef(byte[] rawSrc) {
149164
return ContentReference.construct(true, rawSrc, 0, rawSrc.length);
150165
}
151166

167+
private ContentReference _sourceRef(byte[] rawSrc, int offset, int length) {
168+
return ContentReference.construct(true, rawSrc, offset, length);
169+
}
170+
152171
private ContentReference _sourceRef(InputStream rawSrc) {
153172
return ContentReference.construct(true, rawSrc, -1, -1);
154173
}
@@ -160,4 +179,4 @@ private ContentReference _sourceRef(File rawSrc) {
160179
private ContentReference _rawSourceRef(boolean textual, Object rawSrc) {
161180
return ContentReference.rawReference(textual, rawSrc);
162181
}
163-
}
182+
}

0 commit comments

Comments
 (0)