Skip to content

Commit 630516b

Browse files
committed
fix: prune header constraints on snapshot restore
Ensures that `header_constraints` are correctly cleared and updated when the compiler's state is reverted to a snapshot. This prevents stale constraints from persisting after compilation errors or partial rule additions, maintaining an accurate representation of the active patterns.
1 parent c9ba85f commit 630516b

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

lib/src/compiler/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,9 @@ impl Compiler<'_> {
12361236

12371237
self.filesize_bounds
12381238
.retain(|pattern_id, _| *pattern_id < snapshot.next_pattern_id);
1239+
1240+
self.header_constraints
1241+
.retain(|pattern_id, _| *pattern_id < snapshot.next_pattern_id);
12391242
}
12401243

12411244
/// Returns true if the bytes in the slice are all 0x00, 0x90, or 0xff.

lib/src/tests/mod.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,6 +3424,80 @@ fn filesize_bounds() {
34243424
.expect_err("should fail");
34253425
}
34263426

3427+
#[test]
3428+
fn header_constraints() {
3429+
let rules = crate::compile(
3430+
r#"
3431+
rule test_1 {
3432+
strings:
3433+
$a = /foo.*bar/
3434+
condition:
3435+
uint16(0) == 0x5a4d and $a
3436+
}
3437+
rule test_2 {
3438+
strings:
3439+
$a = /foo.*bar/
3440+
condition:
3441+
$a
3442+
}
3443+
"#,
3444+
)
3445+
.unwrap();
3446+
3447+
let mut scanner = crate::scanner::Scanner::new(&rules);
3448+
3449+
assert_eq!(
3450+
scanner
3451+
.scan(b"foobar")
3452+
.expect("scan should not fail")
3453+
.matching_rules()
3454+
.len(),
3455+
1 // test_2 matches, but test_1 do not.
3456+
);
3457+
3458+
let rules = crate::compile(
3459+
r#"
3460+
rule test {
3461+
strings:
3462+
$a = /foo.*bar/
3463+
condition:
3464+
$a and filesize == 6
3465+
}
3466+
"#,
3467+
)
3468+
.unwrap();
3469+
3470+
let mut scanner = crate::scanner::Scanner::new(&rules);
3471+
3472+
assert_eq!(
3473+
scanner
3474+
.scan(b"foobar")
3475+
.expect("scan should not fail")
3476+
.matching_rules()
3477+
.len(),
3478+
1
3479+
);
3480+
3481+
crate::compile(
3482+
r#"
3483+
rule test_1 {
3484+
strings:
3485+
$a = "foo"
3486+
$b = /a*/
3487+
condition:
3488+
uint16(0) == 0x5a4d and $a and $b
3489+
}
3490+
rule test_2 {
3491+
strings:
3492+
$c = "bar"
3493+
condition:
3494+
uint16(0) == 0x5a4d and $c
3495+
}
3496+
"#,
3497+
)
3498+
.expect_err("should fail");
3499+
}
3500+
34273501
#[test]
34283502
fn for_of() {
34293503
rule_true!(

0 commit comments

Comments
 (0)