-
Notifications
You must be signed in to change notification settings - Fork 495
Fix NullPointerException issues in HeifReader and PngMetadataReader #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
|
|
||
| import com.drew.lang.SequentialReader; | ||
| import com.drew.lang.StreamReader; | ||
| import com.drew.lang.annotations.NotNull; | ||
| import com.drew.metadata.heif.HeifBoxTypes; | ||
| import com.drew.metadata.heif.HeifContainerTypes; | ||
| import com.drew.metadata.heif.HeifDirectory; | ||
|
|
@@ -38,8 +39,15 @@ public class HeifReader | |
| private static final Set<String> ACCEPTABLE_PRE_META_BOX_TYPES = | ||
| new HashSet<String>(Arrays.asList(HeifBoxTypes.BOX_FILE_TYPE, HeifContainerTypes.BOX_METADATA)); | ||
|
|
||
| public void extract(InputStream inputStream, HeifHandler<?> handler) | ||
| public void extract(@NotNull InputStream inputStream, @NotNull HeifHandler<?> handler) | ||
| { | ||
| if (inputStream == null) { | ||
| throw new IllegalArgumentException("inputStream cannot be null"); | ||
| } | ||
| if (handler == null) { | ||
| throw new IllegalArgumentException("handler cannot be null"); | ||
| } | ||
|
Comment on lines
+44
to
+49
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are probably thousands of such places where we don't validate parameter values as non-null. I'm not sure what value this adds in practice. |
||
|
|
||
| // We need to read through the input stream to find the meta box which will tell us what handler to use | ||
|
|
||
| // The meta box is not necessarily the first box, so we need to mark the input stream (if we can) | ||
|
|
@@ -49,7 +57,10 @@ public void extract(InputStream inputStream, HeifHandler<?> handler) | |
| boolean markSupported = false; | ||
| if (inputStream.markSupported()) { | ||
| markSupported = true; | ||
| inputStream.mark(inputStream.available() + 1); // +1 since we're going to read past the end of the stream by 1 byte | ||
| int available = inputStream.available(); | ||
| // Some InputStreams return -1 for available(), so use a reasonable default | ||
| int markReadAheadLimit = (available > 0) ? available + 1 : 8192; | ||
| inputStream.mark(markReadAheadLimit); | ||
|
Comment on lines
+60
to
+63
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some additional context on this change? In what case were you seeing this? I'd like to think through the consequences of this a bit more. |
||
| } | ||
|
|
||
| StreamReader reader = new StreamReader(inputStream); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2002-2019 Drew Noakes and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * More information about this project is available at: | ||
| * | ||
| * https://drewnoakes.com/code/exif/ | ||
| * https://github.com/drewnoakes/metadata-extractor | ||
| */ | ||
| package com.drew.imaging.heif; | ||
|
|
||
| import com.drew.metadata.Metadata; | ||
| import com.drew.metadata.heif.HeifBoxHandler; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
|
|
||
| /** | ||
| * @author Drew Noakes https://drewnoakes.com | ||
| */ | ||
| public class HeifReaderTest | ||
| { | ||
| @Test(expected = IllegalArgumentException.class) | ||
| public void testExtractWithNullInputStream() | ||
| { | ||
| HeifReader reader = new HeifReader(); | ||
| Metadata metadata = new Metadata(); | ||
| HeifBoxHandler handler = new HeifBoxHandler(metadata); | ||
|
|
||
| reader.extract(null, handler); | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public void testExtractWithNullHandler() | ||
| { | ||
| HeifReader reader = new HeifReader(); | ||
| ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); | ||
|
|
||
| reader.extract(inputStream, null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExtractWithValidParameters() | ||
| { | ||
| // Should not throw any exceptions | ||
| HeifReader reader = new HeifReader(); | ||
| ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); | ||
| Metadata metadata = new Metadata(); | ||
| HeifBoxHandler handler = new HeifBoxHandler(metadata); | ||
|
|
||
| reader.extract(inputStream, handler); | ||
| // This should complete without throwing any exceptions | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotations are good, thanks!