Skip to content
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

NPE when handling corrupt jar files #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/main/java/com/redhat/victims/fingerprint/JarFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,25 @@ public Artifact getRecord() {
*/
protected Content getNextFile() throws IOException {
JarEntry entry;
if ((entry = jis.getNextJarEntry()) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER];
int size;
while ((size = jis.read(data, 0, data.length)) != -1) {
bos.write(data, 0, size);
}
Content file = new Content(entry.getName(), bos.toByteArray());
bos.close();
return file;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
if ((entry = jis.getNextJarEntry()) != null) {

byte[] data = new byte[BUFFER];
int size;
while ((size = jis.read(data, 0, data.length)) != -1) {
bos.write(data, 0, size);
}
Content file = new Content(entry.getName(), bos.toByteArray());
bos.close();
return file;
}
}
catch (IOException io) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle all IOExceptions here or just NPEs?

io.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stacktrace should only be print if we are in debug mode etc. The message here should be more a user-friendly error. Additionally, note that the exception must be re-thrown in order for it to be handled by caller and a 'VictimsException' be thrown correctly up the call stack.

bos.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a finally block. Something like

ByteArrayOutputStream bos = new ByteArrayOutputStream();
Content file = null;
try {
            // stuff
} catch (IOException io) {
            // handle message handling and re-throw
} finally {
            bos.close();
            return file;
}

System.out.println(new String(bos.toByteArray()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we doing this?

}
return null;
}

Expand Down