findbugs: DocumentInputStream.skip(long) result not checked; close DocumentInputStream even if exception is thrown

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751020 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-07-02 04:39:53 +00:00
parent 6645d8a80f
commit d7900beec3

View File

@ -198,71 +198,79 @@ public class VBAMacroReader implements Closeable {
String name = entry.getName(); String name = entry.getName();
DocumentNode document = (DocumentNode)entry; DocumentNode document = (DocumentNode)entry;
DocumentInputStream dis = new DocumentInputStream(document); DocumentInputStream dis = new DocumentInputStream(document);
if ("dir".equalsIgnoreCase(name)) { try {
// process DIR if ("dir".equalsIgnoreCase(name)) {
RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis); // process DIR
String streamName = null; RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
while (true) { String streamName = null;
int id = in.readShort(); while (true) {
if (id == -1 || id == 0x0010) { int id = in.readShort();
break; // EOF or TERMINATOR if (id == -1 || id == 0x0010) {
} break; // EOF or TERMINATOR
int len = in.readInt(); }
switch (id) { int len = in.readInt();
case 0x0009: // PROJECTVERSION switch (id) {
trySkip(in, 6); case 0x0009: // PROJECTVERSION
break; trySkip(in, 6);
case 0x0003: // PROJECTCODEPAGE break;
int codepage = in.readShort(); case 0x0003: // PROJECTCODEPAGE
modules.charset = Charset.forName("Cp" + codepage); int codepage = in.readShort();
break; modules.charset = Charset.forName("Cp" + codepage);
case 0x001A: // STREAMNAME break;
streamName = readString(in, len, modules.charset); case 0x001A: // STREAMNAME
break; streamName = readString(in, len, modules.charset);
case 0x0031: // MODULEOFFSET break;
int moduleOffset = in.readInt(); case 0x0031: // MODULEOFFSET
Module module = modules.get(streamName); int moduleOffset = in.readInt();
if (module != null) { Module module = modules.get(streamName);
ByteArrayOutputStream out = new ByteArrayOutputStream(); if (module != null) {
RLEDecompressingInputStream stream = new RLEDecompressingInputStream(new ByteArrayInputStream( ByteArrayOutputStream out = new ByteArrayOutputStream();
module.buf, moduleOffset, module.buf.length - moduleOffset)); RLEDecompressingInputStream stream = new RLEDecompressingInputStream(new ByteArrayInputStream(
IOUtils.copy(stream, out); module.buf, moduleOffset, module.buf.length - moduleOffset));
stream.close(); IOUtils.copy(stream, out);
out.close(); stream.close();
module.buf = out.toByteArray(); out.close();
} else { module.buf = out.toByteArray();
module = new Module(); } else {
module.offset = moduleOffset; module = new Module();
modules.put(streamName, module); module.offset = moduleOffset;
modules.put(streamName, module);
}
break;
default:
trySkip(in, len);
break;
} }
break;
default:
trySkip(in, len);
break;
} }
in.close();
} else if (!startsWithIgnoreCase(name, "__SRP")
&& !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
// process module, skip __SRP and _VBA_PROJECT since these do not contain macros
Module module = modules.get(name);
final InputStream in;
// TODO Refactor this to fetch dir then do the rest
if (module == null) {
// no DIR stream with offsets yet, so store the compressed bytes for later
module = new Module();
modules.put(name, module);
in = dis;
} else {
// we know the offset already, so decompress immediately on-the-fly
long skippedBytes = dis.skip(module.offset);
if (skippedBytes != module.offset) {
throw new IOException("tried to skip " + module.offset + " bytes, but actually skipped " + skippedBytes + " bytes");
}
in = new RLEDecompressingInputStream(dis);
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
in.close();
out.close();
module.buf = out.toByteArray();
} }
in.close(); }
} else if (!startsWithIgnoreCase(name, "__SRP") finally {
&& !startsWithIgnoreCase(name, "_VBA_PROJECT")) { dis.close();
// process module, skip __SRP and _VBA_PROJECT since these do not contain macros
Module module = modules.get(name);
final InputStream in;
// TODO Refactor this to fetch dir then do the rest
if (module == null) {
// no DIR stream with offsets yet, so store the compressed bytes for later
module = new Module();
modules.put(name, module);
in = dis;
} else {
// we know the offset already, so decompress immediately on-the-fly
dis.skip(module.offset);
in = new RLEDecompressingInputStream(dis);
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
in.close();
out.close();
module.buf = out.toByteArray();
} }
} }
} }