Skip to content

Commit

Permalink
Merge pull request #4479 from eclipse/jetty-9.4.x-4383-npe-multipart
Browse files Browse the repository at this point in the history
Issue #4383 - Minimal NPE prevention on MultiPart
  • Loading branch information
joakime authored Jan 15, 2020
2 parents 33a5a25 + eba24b6 commit b75cf1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public class MultiPartFormInputStream
{
private static final Logger LOG = Log.getLogger(MultiPartFormInputStream.class);
private static final MultiMap<Part> EMPTY_MAP = new MultiMap<>(Collections.emptyMap());
private final MultiMap<Part> _parts;
private InputStream _in;
private MultipartConfigElement _config;
private String _contentType;
private MultiMap<Part> _parts;
private Throwable _err;
private File _tmpDir;
private File _contextTmpDir;
Expand Down Expand Up @@ -341,16 +341,19 @@ public MultiPartFormInputStream(InputStream in, String contentType, MultipartCon
if (_config == null)
_config = new MultipartConfigElement(_contextTmpDir.getAbsolutePath());

MultiMap parts = new MultiMap();

if (in instanceof ServletInputStream)
{
if (((ServletInputStream)in).isFinished())
{
_parts = EMPTY_MAP;
parts = EMPTY_MAP;
_parsed = true;
return;
}
}
_in = new BufferedInputStream(in);
if (!_parsed)
_in = new BufferedInputStream(in);
_parts = parts;
}

/**
Expand Down Expand Up @@ -432,6 +435,9 @@ public Collection<Part> getParts() throws IOException
parse();
throwIfError();

if (_parts.isEmpty())
return Collections.emptyList();

Collection<List<Part>> values = _parts.values();
List<Part> parts = new ArrayList<>();
for (List<Part> o : values)
Expand Down Expand Up @@ -492,9 +498,6 @@ protected void parse()
Handler handler = new Handler();
try
{
// initialize
_parts = new MultiMap<>();

// if its not a multipart request, don't parse it
if (_contentType == null || !_contentType.startsWith("multipart/form-data"))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,16 @@ public void testPartTmpFileDeletion() throws Exception
assertThat(stuff.exists(), is(false)); //tmp file was removed after cleanup
}

@Test
public void testDeleteNPE()
{
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(); // this should not be an NPE
}

@Test
public void testLFOnlyRequest()
throws Exception
Expand Down

0 comments on commit b75cf1c

Please sign in to comment.