diff --git a/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java b/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java index 3410dc0769..b49e4416d8 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java +++ b/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java @@ -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 @@ -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) diff --git a/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java b/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java index 1cbb219942..70219b3864 100644 --- a/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java +++ b/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java @@ -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) { @@ -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); } @@ -160,4 +179,4 @@ private ContentReference _sourceRef(File rawSrc) { private ContentReference _rawSourceRef(boolean textual, Object rawSrc) { return ContentReference.rawReference(textual, rawSrc); } -} +} \ No newline at end of file