mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复FileMagicNumber长度判断问题导致的越界异常
This commit is contained in:
parent
a3f48ccb29
commit
c6ce5fe215
@ -13,6 +13,7 @@
|
|||||||
* 【crypto】 修复NoSuchMethodError未捕获问题(issue#2966@Github)
|
* 【crypto】 修复NoSuchMethodError未捕获问题(issue#2966@Github)
|
||||||
* 【poi 】 修复SXSSFWorkbook调用setComment时错位的问题(issue#I6MBS5@Gitee)
|
* 【poi 】 修复SXSSFWorkbook调用setComment时错位的问题(issue#I6MBS5@Gitee)
|
||||||
* 【core 】 修复BeanUtil.hasGetter没有跳过getClass方法的问题(issue#I6MBS5@Gitee)
|
* 【core 】 修复BeanUtil.hasGetter没有跳过getClass方法的问题(issue#I6MBS5@Gitee)
|
||||||
|
* 【core 】 修复FileMagicNumber长度判断问题导致的越界异常(issue#I6MACI@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.15 (2023-03-09)
|
# 5.8.15 (2023-03-09)
|
||||||
|
@ -325,6 +325,9 @@ public enum FileMagicNumber {
|
|||||||
WOFF("font/woff", "woff") {
|
WOFF("font/woff", "woff") {
|
||||||
@Override
|
@Override
|
||||||
public boolean match(final byte[] bytes) {
|
public boolean match(final byte[] bytes) {
|
||||||
|
if (bytes.length < 8) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
|
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
|
||||||
&& Objects.equals(bytes[1], (byte) 0x4f)
|
&& Objects.equals(bytes[1], (byte) 0x4f)
|
||||||
&& Objects.equals(bytes[2], (byte) 0x46)
|
&& Objects.equals(bytes[2], (byte) 0x46)
|
||||||
@ -341,13 +344,15 @@ public enum FileMagicNumber {
|
|||||||
&& Objects.equals(bytes[5], (byte) 0x72)
|
&& Objects.equals(bytes[5], (byte) 0x72)
|
||||||
&& Objects.equals(bytes[6], (byte) 0x75)
|
&& Objects.equals(bytes[6], (byte) 0x75)
|
||||||
&& Objects.equals(bytes[7], (byte) 0x65);
|
&& Objects.equals(bytes[7], (byte) 0x65);
|
||||||
return bytes.length > 7
|
return flag1 && (flag2 || flag3 || flag4);
|
||||||
&& (flag1 && (flag2 || flag3 || flag4));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
WOFF2("font/woff2", "woff2") {
|
WOFF2("font/woff2", "woff2") {
|
||||||
@Override
|
@Override
|
||||||
public boolean match(final byte[] bytes) {
|
public boolean match(final byte[] bytes) {
|
||||||
|
if (bytes.length < 8) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
|
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
|
||||||
&& Objects.equals(bytes[1], (byte) 0x4f)
|
&& Objects.equals(bytes[1], (byte) 0x4f)
|
||||||
&& Objects.equals(bytes[2], (byte) 0x46)
|
&& Objects.equals(bytes[2], (byte) 0x46)
|
||||||
@ -364,8 +369,7 @@ public enum FileMagicNumber {
|
|||||||
&& Objects.equals(bytes[5], (byte) 0x72)
|
&& Objects.equals(bytes[5], (byte) 0x72)
|
||||||
&& Objects.equals(bytes[6], (byte) 0x75)
|
&& Objects.equals(bytes[6], (byte) 0x75)
|
||||||
&& Objects.equals(bytes[7], (byte) 0x65);
|
&& Objects.equals(bytes[7], (byte) 0x65);
|
||||||
return bytes.length > 7
|
return flag1 && (flag2 || flag3 || flag4);
|
||||||
&& (flag1 && (flag2 || flag3 || flag4));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
TTF("font/ttf", "ttf") {
|
TTF("font/ttf", "ttf") {
|
||||||
|
@ -19,38 +19,38 @@ public class FileTypeUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void fileTypeUtilTest() {
|
public void fileTypeUtilTest() {
|
||||||
File file = FileUtil.file("hutool.jpg");
|
final File file = FileUtil.file("hutool.jpg");
|
||||||
String type = FileTypeUtil.getType(file);
|
final String type = FileTypeUtil.getType(file);
|
||||||
Assert.assertEquals("jpg", type);
|
Assert.assertEquals("jpg", type);
|
||||||
|
|
||||||
FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
|
FileTypeUtil.putFileType("ffd8ffe000104a464946", "new_jpg");
|
||||||
String newType = FileTypeUtil.getType(file);
|
final String newType = FileTypeUtil.getType(file);
|
||||||
Assert.assertEquals("new_jpg", newType);
|
Assert.assertEquals("new_jpg", newType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void emptyTest() {
|
public void emptyTest() {
|
||||||
File file = FileUtil.file("d:/empty.txt");
|
final File file = FileUtil.file("d:/empty.txt");
|
||||||
String type = FileTypeUtil.getType(file);
|
final String type = FileTypeUtil.getType(file);
|
||||||
Console.log(type);
|
Console.log(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void docTest() {
|
public void docTest() {
|
||||||
File file = FileUtil.file("f:/test/test.doc");
|
final File file = FileUtil.file("f:/test/test.doc");
|
||||||
String type = FileTypeUtil.getType(file);
|
final String type = FileTypeUtil.getType(file);
|
||||||
Console.log(type);
|
Console.log(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void ofdTest() {
|
public void ofdTest() {
|
||||||
File file = FileUtil.file("e:/test.ofd");
|
final File file = FileUtil.file("e:/test.ofd");
|
||||||
String hex = IoUtil.readHex64Upper(FileUtil.getInputStream(file));
|
final String hex = IoUtil.readHex64Upper(FileUtil.getInputStream(file));
|
||||||
Console.log(hex);
|
Console.log(hex);
|
||||||
String type = FileTypeUtil.getType(file);
|
final String type = FileTypeUtil.getType(file);
|
||||||
Console.log(type);
|
Console.log(type);
|
||||||
Assert.assertEquals("ofd", type);
|
Assert.assertEquals("ofd", type);
|
||||||
}
|
}
|
||||||
@ -59,18 +59,18 @@ public class FileTypeUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void inputStreamAndFilenameTest() {
|
public void inputStreamAndFilenameTest() {
|
||||||
File file = FileUtil.file("e:/laboratory/test.xlsx");
|
final File file = FileUtil.file("e:/laboratory/test.xlsx");
|
||||||
String type = FileTypeUtil.getType(file);
|
final String type = FileTypeUtil.getType(file);
|
||||||
Assert.assertEquals("xlsx", type);
|
Assert.assertEquals("xlsx", type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void getTypeFromInputStream() throws IOException {
|
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);
|
final BufferedInputStream inputStream = FileUtil.getInputStream(file);
|
||||||
inputStream.mark(0);
|
inputStream.mark(0);
|
||||||
String type = FileTypeUtil.getType(inputStream);
|
final String type = FileTypeUtil.getType(inputStream);
|
||||||
|
|
||||||
inputStream.reset();
|
inputStream.reset();
|
||||||
}
|
}
|
||||||
@ -85,4 +85,11 @@ public class FileTypeUtilTest {
|
|||||||
Console.log(type);
|
Console.log(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issueI6MACITest() {
|
||||||
|
final File file = FileUtil.file("text.txt");
|
||||||
|
final String type = FileTypeUtil.getType(file);
|
||||||
|
Assert.assertEquals("txt", type);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
1
hutool-core/src/test/resources/text.txt
Normal file
1
hutool-core/src/test/resources/text.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
1
|
Loading…
x
Reference in New Issue
Block a user