From 37f85034422ce86438d52ab5d86f4d675689c8ff Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 15 Jan 2020 15:31:43 -0600 Subject: [PATCH] Issue #4383 - Minimal NPE prevention on MultiPart Signed-off-by: Joakim Erdfelt --- .../jetty/http/MultiPartFormInputStream.java | 7 +++++++ .../jetty/http/MultiPartFormInputStreamTest.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/MultiPartFormInputStream.java b/jetty-http/src/main/java/org/eclipse/jetty/http/MultiPartFormInputStream.java index 05c1dbf25e17..08f1e0b96e6e 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/MultiPartFormInputStream.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/MultiPartFormInputStream.java @@ -397,6 +397,13 @@ public Collection getParsedParts() */ public void deleteParts() { + if (_parts == null) + { + // If we call deleteParts at this point, we are considered CLOSED + _err = new IllegalStateException("CLOSED via call to deleteParts()"); + return; + } + MultiException err = null; for (List parts : _parts.values()) { diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/MultiPartFormInputStreamTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/MultiPartFormInputStreamTest.java index 2308314886cd..2e7062c4c631 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/MultiPartFormInputStreamTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/MultiPartFormInputStreamTest.java @@ -509,6 +509,20 @@ public void testPartTmpFileDeletion() throws Exception assertThat(stuff.exists(), is(false)); //tmp file was removed after cleanup } + @Test + public void testParseAfterCleanUp() throws Exception + { + final InputStream input = new ByteArrayInputStream(createMultipartRequestString("myFile").getBytes()); + MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 1024, 50); + MultiPartFormInputStream mpis = new MultiPartFormInputStream(input, _contentType, config, _tmpDir); + + mpis.deleteParts(); + + // The call to getParts should throw because we have already cleaned up the parts. + Throwable error = assertThrows(IllegalStateException.class, mpis::getParts); + assertThat(error.getMessage(), containsString("CLOSED")); + } + @Test public void testLFOnlyRequest() throws Exception