Merge pull request #4479 from eclipse/jetty-9.4.x-4383-npe-multipart

Issue #4383 - Minimal NPE prevention on MultiPart
This commit is contained in:
Joakim Erdfelt 2020-01-15 16:40:03 -06:00 committed by GitHub
commit b75cf1c6a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -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;
@ -341,16 +341,19 @@ public class MultiPartFormInputStream
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;
}
/**
@ -432,6 +435,9 @@ public class MultiPartFormInputStream
parse();
throwIfError();
if (_parts.isEmpty())
return Collections.emptyList();
Collection<List<Part>> values = _parts.values();
List<Part> parts = new ArrayList<>();
for (List<Part> o : values)
@ -492,9 +498,6 @@ public class MultiPartFormInputStream
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;

View File

@ -509,6 +509,16 @@ public class MultiPartFormInputStreamTest
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