Skip to content

Commit c5aa1e1

Browse files
committed
Fix #739
1 parent 143eece commit c5aa1e1

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,7 @@ Andrey Somov (asomov@github)
279279
* Contributed #732: Update Maven wrapper
280280
(2.13.2)
281281

282+
Vlad Tatavu (vladt@github)
283+
* Reported #739: `JsonLocation` in 2.13 only uses identity comparison
284+
for "content reference"
285+
(2.13.2)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ JSON library.
1818

1919
#732: Update Maven wrapper
2020
(contributed by Andrey S)
21+
#739: `JsonLocation` in 2.13 only uses identity comparison for "content reference"
22+
(reported by Vlad T)
2123

2224
2.13.1 (19-Dec-2021)
2325

src/main/java/com/fasterxml/jackson/core/JsonLocation.java

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
* Object that encapsulates Location information used for reporting
1212
* parsing (or potentially generation) errors, as well as current location
1313
* within input streams.
14+
*<p>
15+
* NOTE: users should be careful if using {@link #equals} implementation as
16+
* it may or may not compare underlying "content reference" for equality.
17+
* Instead if would make sense to explicitly implementing equality checks
18+
* using specific criteria caller desires.
1419
*/
1520
public class JsonLocation
1621
implements java.io.Serializable

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

+20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.fasterxml.jackson.core.io;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.io.ObjectInputStream;
56
import java.io.ObjectOutputStream;
7+
import java.net.URI;
8+
import java.net.URL;
69
import java.nio.charset.Charset;
710
import java.util.Objects;
811

@@ -357,6 +360,23 @@ public boolean equals(Object other)
357360
if (!(other instanceof ContentReference)) return false;
358361
ContentReference otherSrc = (ContentReference) other;
359362

363+
// 16-Jan-2022, tatu: As per [core#739] we'll want to consider some
364+
// but not all content cases with real equality: the concern here is
365+
// to avoid expensive comparisons and/or possible security issues
366+
final Object otherRaw = otherSrc._rawContent;
367+
368+
if (_rawContent == null) {
369+
return (otherRaw == null);
370+
} else if (otherRaw == null) {
371+
return false;
372+
}
373+
374+
if ((_rawContent instanceof File)
375+
|| (_rawContent instanceof URL)
376+
|| (_rawContent instanceof URI)
377+
) {
378+
return _rawContent.equals(otherRaw);
379+
}
360380
return _rawContent == otherSrc._rawContent;
361381
}
362382

src/test/java/com/fasterxml/jackson/core/TestLocation.java renamed to src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.fasterxml.jackson.core;
22

33
import java.io.ByteArrayInputStream;
4+
import java.io.File;
45
import java.io.InputStream;
56

67
import com.fasterxml.jackson.core.io.ContentReference;
78

8-
public class TestLocation extends BaseTest
9+
public class JsonLocationTest extends BaseTest
910
{
1011
static class Foobar { }
1112

@@ -121,6 +122,21 @@ public void testDisableSourceInclusion() throws Exception
121122
p.close();
122123
}
123124

125+
// for [jackson-core#739]: try to support equality
126+
public void testLocationEquality() throws Exception
127+
{
128+
// important: create separate but equal instances
129+
File src1 = new File("/tmp/foo");
130+
File src2 = new File("/tmp/foo");
131+
assertEquals(src1, src2);
132+
133+
JsonLocation loc1 = new JsonLocation(_sourceRef(src1),
134+
10L, 10L, 1, 2);
135+
JsonLocation loc2 = new JsonLocation(_sourceRef(src2),
136+
10L, 10L, 1, 2);
137+
assertEquals(loc1, loc2);
138+
}
139+
124140
private ContentReference _sourceRef(String rawSrc) {
125141
return ContentReference.construct(true, rawSrc, 0, rawSrc.length());
126142
}
@@ -137,6 +153,10 @@ private ContentReference _sourceRef(InputStream rawSrc) {
137153
return ContentReference.construct(true, rawSrc, -1, -1);
138154
}
139155

156+
private ContentReference _sourceRef(File rawSrc) {
157+
return ContentReference.construct(true, rawSrc, -1, -1);
158+
}
159+
140160
private ContentReference _rawSourceRef(boolean textual, Object rawSrc) {
141161
return ContentReference.rawReference(textual, rawSrc);
142162
}

0 commit comments

Comments
 (0)