Skip to content

Commit f7a9ebf

Browse files
committed
Don't require file length greater than region_len
Fixes #15.
1 parent a18e395 commit f7a9ebf

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/fdo_magic/check.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use petgraph::prelude::*;
2+
use std::cmp::min;
23
use std::iter::zip;
34

45
fn from_u8_singlerule(bytes: &[u8], rule: &super::MagicRule) -> bool {
56
// Check if we're even in bounds
67
let bound_min = rule.start_off as usize;
7-
let bound_max = rule.start_off as usize + rule.val.len() + rule.region_len as usize;
8-
9-
if bound_max > bytes.len() {
8+
if bound_min > bytes.len() {
109
return false;
1110
}
11+
let bound_max = rule.start_off as usize + rule.val.len() + rule.region_len as usize;
12+
let bound_max = min(bound_max, bytes.len());
13+
1214
let testarea = &bytes[bound_min..bound_max];
1315

1416
testarea.windows(rule.val.len()).any(|window| {

tests/from_u8.rs

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ mod from_u8 {
8383
}
8484

8585
/// Text tests
86+
#[test]
87+
fn text_html() {
88+
assert_eq!(
89+
tree_magic::from_u8(include_bytes!("text/html")),
90+
convmime!("text/html")
91+
);
92+
}
93+
8694
#[test]
8795
fn text_plain() {
8896
assert_eq!(

tests/text/html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body></body>
7+
</html>

0 commit comments

Comments
 (0)