소스 검색

Handle empty files correctly when looking for FileMagic to avoid NegativeArraySizeException

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1873232 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_1_2
Dominik Stadler 4 년 전
부모
커밋
366c80c480
2개의 변경된 파일13개의 추가작업 그리고 3개의 파일을 삭제
  1. 5
    2
      src/java/org/apache/poi/poifs/filesystem/FileMagic.java
  2. 8
    1
      src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java

+ 5
- 2
src/java/org/apache/poi/poifs/filesystem/FileMagic.java 파일 보기

@@ -113,7 +113,7 @@ public enum FileMagic {
final static int MAX_PATTERN_LENGTH = 44;

final byte[][] magic;
FileMagic(long magic) {
this.magic = new byte[1][8];
LittleEndian.putLong(this.magic[0], 0, magic);
@@ -122,7 +122,7 @@ public enum FileMagic {
FileMagic(byte[]... magic) {
this.magic = magic;
}
FileMagic(String... magic) {
this.magic = new byte[magic.length][];
int i=0;
@@ -172,6 +172,9 @@ public enum FileMagic {
// read as many bytes as possible, up to the required number of bytes
byte[] data = new byte[MAX_PATTERN_LENGTH];
int read = IOUtils.readFully(fis, data, 0, MAX_PATTERN_LENGTH);
if(read == -1) {
return FileMagic.UNKNOWN;
}

// only use the bytes that could be read
data = Arrays.copyOf(data, read);

+ 8
- 1
src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java 파일 보기

@@ -107,8 +107,15 @@ public class TestFileMagic {
@Test
public void testShortFile() throws IOException {
// having a file shorter than 8 bytes previously caused an exception
byte[] data = new byte[] { -1, -40, -1, -32, 0 };
fetchMagicFromData(new byte[] { -1, -40, -1, -32, 0 });
fetchMagicFromData(new byte[] { -1, -40, -1, -32 });
fetchMagicFromData(new byte[] { -1, -40, -1 });
fetchMagicFromData(new byte[] { -1, -40 });
fetchMagicFromData(new byte[] { -1 });
fetchMagicFromData(new byte[0]);
}

private void fetchMagicFromData(byte[] data) throws IOException {
File file = TempFile.createTempFile("TestFileMagic", ".bin");
try {
try (FileOutputStream fos = new FileOutputStream(file)) {

Loading…
취소
저장