Skip to content

Commit

Permalink
Fix #763
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 11, 2022
1 parent 2dee63a commit 83c5694
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ JSON library.
(contributed by @pjfanning)
#759: JsonGenerator to provide current value to the context before starting objects
(reported by Illia O)
#763: `JsonFactory.createParser()` with `File` may leak `InputStream`s

2.13.3 (14-May-2022)

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1651,9 +1651,20 @@ public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
* @since 2.1
*/
protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
// As per [JACKSON-259], may want to fully disable canonicalization:
return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
_objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
try {
return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
_objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
} catch (IOException | RuntimeException e) {
// 10-Jun-2022, tatu: For [core#763] may need to close InputStream here
if (ctxt.isResourceManaged()) {
try {
in.close();
} catch (Exception e2) {
e.addSuppressed(e2);
}
}
throw e;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.fasterxml.jackson.core.json;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import com.fasterxml.jackson.core.*;

// [core#763] (and [databind#3455]
public class InputStreamInitTest
extends com.fasterxml.jackson.core.BaseTest
{
static class FailingInputStream extends InputStream {
public boolean closed = false;

@Override
public void close() {
closed = true;
}

@Override
public int read() throws IOException {
throw new IOException("Will not read, ever!");
}
}

static class FailingJsonFactory extends JsonFactory {
private static final long serialVersionUID = 1L;

public FailingInputStream lastStream;

@Override
protected InputStream _fileInputStream(File f) throws IOException {
return (lastStream = new FailingInputStream());
}

@Override
protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
return (lastStream = new FailingInputStream());
}
}

public void testForFile() throws Exception
{
final FailingJsonFactory jsonF = new FailingJsonFactory();
try {
/*JsonParser p =*/ jsonF.createParser(new File("/tmp/test.json"));
fail("Should not pass");
} catch (IOException e) {
verifyException(e, "Will not read");
}
assertNotNull(jsonF.lastStream);
assertTrue(jsonF.lastStream.closed);
}

public void testForURL() throws Exception
{
final FailingJsonFactory jsonF = new FailingJsonFactory();
try {
/*JsonParser p =*/ jsonF.createParser(new URL("http://localhost:80/"));
fail("Should not pass");
} catch (IOException e) {
verifyException(e, "Will not read");
}
assertNotNull(jsonF.lastStream);
assertTrue(jsonF.lastStream.closed);
}
}

0 comments on commit 83c5694

Please sign in to comment.