Skip to content

Commit

Permalink
Bit more checking wrt JsonLocation (and #739)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 17, 2022
1 parent c5aa1e1 commit 456b20f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Expand Up @@ -360,6 +360,12 @@ public boolean equals(Object other)
if (!(other instanceof ContentReference)) return false;
ContentReference otherSrc = (ContentReference) other;

// 16-Jan-2022, tatu: First ensure offset/length the same
if ((_offset != otherSrc._offset)
|| (_length != otherSrc._length)) {
return false;
}

// 16-Jan-2022, tatu: As per [core#739] we'll want to consider some
// but not all content cases with real equality: the concern here is
// to avoid expensive comparisons and/or possible security issues
Expand All @@ -370,7 +376,7 @@ public boolean equals(Object other)
} else if (otherRaw == null) {
return false;
}

if ((_rawContent instanceof File)
|| (_rawContent instanceof URL)
|| (_rawContent instanceof URI)
Expand Down
21 changes: 20 additions & 1 deletion src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java
Expand Up @@ -135,6 +135,21 @@ public void testLocationEquality() throws Exception
JsonLocation loc2 = new JsonLocation(_sourceRef(src2),
10L, 10L, 1, 2);
assertEquals(loc1, loc2);

// Also make sure to consider offset/length
final byte[] bogus = "BOGUS".getBytes();

// If same, equals:
assertEquals(new JsonLocation(_sourceRef(bogus, 0, 5), 5L, 0L, 1, 2),
new JsonLocation(_sourceRef(bogus, 0, 5), 5L, 0L, 1, 2));

// If different, not equals
loc1 = new JsonLocation(_sourceRef(bogus, 0, 5),
5L, 0L, 1, 2);
loc2 = new JsonLocation(_sourceRef(bogus, 1, 4),
5L, 0L, 1, 2);
assertFalse(loc1.equals(loc2));
assertFalse(loc2.equals(loc1));
}

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

private ContentReference _sourceRef(byte[] rawSrc, int offset, int length) {
return ContentReference.construct(true, rawSrc, offset, length);
}

private ContentReference _sourceRef(InputStream rawSrc) {
return ContentReference.construct(true, rawSrc, -1, -1);
}
Expand All @@ -160,4 +179,4 @@ private ContentReference _sourceRef(File rawSrc) {
private ContentReference _rawSourceRef(boolean textual, Object rawSrc) {
return ContentReference.rawReference(textual, rawSrc);
}
}
}

0 comments on commit 456b20f

Please sign in to comment.