修复FileMagicNumber长度判断问题导致的越界异常

This commit is contained in:
Looly 2023-03-12 15:58:45 +08:00
parent a3f48ccb29
commit c6ce5fe215
4 changed files with 31 additions and 18 deletions

View File

@ -13,6 +13,7 @@
* 【crypto】 修复NoSuchMethodError未捕获问题issue#2966@Github
* 【poi 】 修复SXSSFWorkbook调用setComment时错位的问题issue#I6MBS5@Gitee
* 【core 】 修复BeanUtil.hasGetter没有跳过getClass方法的问题issue#I6MBS5@Gitee
* 【core 】 修复FileMagicNumber长度判断问题导致的越界异常issue#I6MACI@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.15 (2023-03-09)

View File

@ -325,6 +325,9 @@ public enum FileMagicNumber {
WOFF("font/woff", "woff") {
@Override
public boolean match(final byte[] bytes) {
if (bytes.length < 8) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
&& Objects.equals(bytes[1], (byte) 0x4f)
&& Objects.equals(bytes[2], (byte) 0x46)
@ -341,13 +344,15 @@ public enum FileMagicNumber {
&& Objects.equals(bytes[5], (byte) 0x72)
&& Objects.equals(bytes[6], (byte) 0x75)
&& Objects.equals(bytes[7], (byte) 0x65);
return bytes.length > 7
&& (flag1 && (flag2 || flag3 || flag4));
return flag1 && (flag2 || flag3 || flag4);
}
},
WOFF2("font/woff2", "woff2") {
@Override
public boolean match(final byte[] bytes) {
if (bytes.length < 8) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
&& Objects.equals(bytes[1], (byte) 0x4f)
&& Objects.equals(bytes[2], (byte) 0x46)
@ -364,8 +369,7 @@ public enum FileMagicNumber {
&& Objects.equals(bytes[5], (byte) 0x72)
&& Objects.equals(bytes[6], (byte) 0x75)
&& Objects.equals(bytes[7], (byte) 0x65);
return bytes.length > 7
&& (flag1 && (flag2 || flag3 || flag4));
return flag1 && (flag2 || flag3 || flag4);
}
},
TTF("font/ttf", "ttf") {

View File

@ -19,38 +19,38 @@ public class FileTypeUtilTest {
@Test
@Ignore
public void fileTypeUtilTest() {
File file = FileUtil.file("hutool.jpg");
String type = FileTypeUtil.getType(file);
final File file = FileUtil.file("hutool.jpg");
final String type = FileTypeUtil.getType(file);
Assert.assertEquals("jpg", type);
FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
String newType = FileTypeUtil.getType(file);
final String newType = FileTypeUtil.getType(file);
Assert.assertEquals("new_jpg", newType);
}
@Test
@Ignore
public void emptyTest() {
File file = FileUtil.file("d:/empty.txt");
String type = FileTypeUtil.getType(file);
final File file = FileUtil.file("d:/empty.txt");
final String type = FileTypeUtil.getType(file);
Console.log(type);
}
@Test
@Ignore
public void docTest() {
File file = FileUtil.file("f:/test/test.doc");
String type = FileTypeUtil.getType(file);
final File file = FileUtil.file("f:/test/test.doc");
final String type = FileTypeUtil.getType(file);
Console.log(type);
}
@Test
@Ignore
public void ofdTest() {
File file = FileUtil.file("e:/test.ofd");
String hex = IoUtil.readHex64Upper(FileUtil.getInputStream(file));
final File file = FileUtil.file("e:/test.ofd");
final String hex = IoUtil.readHex64Upper(FileUtil.getInputStream(file));
Console.log(hex);
String type = FileTypeUtil.getType(file);
final String type = FileTypeUtil.getType(file);
Console.log(type);
Assert.assertEquals("ofd", type);
}
@ -59,18 +59,18 @@ public class FileTypeUtilTest {
@Test
@Ignore
public void inputStreamAndFilenameTest() {
File file = FileUtil.file("e:/laboratory/test.xlsx");
String type = FileTypeUtil.getType(file);
final File file = FileUtil.file("e:/laboratory/test.xlsx");
final String type = FileTypeUtil.getType(file);
Assert.assertEquals("xlsx", type);
}
@Test
@Ignore
public void getTypeFromInputStream() throws IOException {
File file = FileUtil.file("d:/test/pic.jpg");
final File file = FileUtil.file("d:/test/pic.jpg");
final BufferedInputStream inputStream = FileUtil.getInputStream(file);
inputStream.mark(0);
String type = FileTypeUtil.getType(inputStream);
final String type = FileTypeUtil.getType(inputStream);
inputStream.reset();
}
@ -85,4 +85,11 @@ public class FileTypeUtilTest {
Console.log(type);
}
@Test
public void issueI6MACITest() {
final File file = FileUtil.file("text.txt");
final String type = FileTypeUtil.getType(file);
Assert.assertEquals("txt", type);
}
}

View File

@ -0,0 +1 @@
1