Merge remote-tracking branch 'origin/topic/awelzel/4084-vector-of-pattern-compare'

* origin/topic/awelzel/4084-vector-of-pattern-compare:
  ZAM/relexpr-op NE for patterns
  Expr: Fix folding of pattern values to support == and !=

(cherry picked from commit 33eaa5ccda)
This commit is contained in:
Tim Wojtulewicz 2024-12-16 12:37:25 -07:00
parent 4d6031cbb0
commit 05f8d043a7
7 changed files with 38 additions and 7 deletions

13
CHANGES
View file

@ -1,3 +1,16 @@
7.1.0-rc1.1 | 2024-12-16 13:02:12 -0700
* ZAM/relexpr-op NE for patterns (Vern Paxson, Corelight)
(cherry picked from commit 33eaa5ccda4157a34feb5ffc466f7371dc6d8eff)
* Expr: Fix folding of pattern values to support == and != (Arne Welzel, Corelight)
The fatal error is actually triggered at runtime, so it's a bit
dangerous for users, but not sure there's many use-cases to
compare vectors of patterns.
(cherry picked from commit 33eaa5ccda4157a34feb5ffc466f7371dc6d8eff)
7.1.0-rc1 | 2024-12-16 11:01:50 -0700 7.1.0-rc1 | 2024-12-16 11:01:50 -0700
* Update docs submodule [nomail] [skip ci] (Tim Wojtulewicz) * Update docs submodule [nomail] [skip ci] (Tim Wojtulewicz)

View file

@ -1 +1 @@
7.1.0-rc1 7.1.0-rc1.1

View file

@ -925,12 +925,20 @@ ValPtr BinaryExpr::PatternFold(Val* v1, Val* v2) const {
const RE_Matcher* re1 = v1->AsPattern(); const RE_Matcher* re1 = v1->AsPattern();
const RE_Matcher* re2 = v2->AsPattern(); const RE_Matcher* re2 = v2->AsPattern();
if ( tag != EXPR_AND && tag != EXPR_OR ) ValPtr res;
if ( tag == EXPR_AND || tag == EXPR_OR ) {
RE_Matcher* matcher = tag == EXPR_AND ? RE_Matcher_conjunction(re1, re2) : RE_Matcher_disjunction(re1, re2);
res = make_intrusive<PatternVal>(matcher);
}
else if ( tag == EXPR_EQ || tag == EXPR_NE ) {
bool cmp = strcmp(re1->PatternText(), re2->PatternText());
res = val_mgr->Bool(tag == EXPR_EQ ? cmp == 0 : cmp != 0);
}
else {
BadTag("BinaryExpr::PatternFold"); BadTag("BinaryExpr::PatternFold");
}
RE_Matcher* res = tag == EXPR_AND ? RE_Matcher_conjunction(re1, re2) : RE_Matcher_disjunction(re1, re2); return res;
return make_intrusive<PatternVal>(res);
} }
ValPtr BinaryExpr::SetFold(Val* v1, Val* v2) const { ValPtr BinaryExpr::SetFold(Val* v1, Val* v2) const {

View file

@ -29,7 +29,7 @@ eval-type P strcmp($1->Get()->PatternText(), $2->Get()->PatternText()) == 0
eval-mixed P S $1->MatchExactly($2->AsString()) eval-mixed P S $1->MatchExactly($2->AsString())
rel-expr-op NE rel-expr-op NE
op-type I U D S T A N F op-type I U D S T A N F P
vector vector
eval $1 != $2 eval $1 != $2
eval-type S Bstr_cmp($1->AsString(), $2->AsString()) != 0 eval-type S Bstr_cmp($1->AsString(), $2->AsString()) != 0
@ -37,6 +37,7 @@ eval-type T ! $1->EqualTo(*$2)
eval-type A $1->AsAddr() != $2->AsAddr() eval-type A $1->AsAddr() != $2->AsAddr()
eval-type N $1->AsSubNet() != $2->AsSubNet() eval-type N $1->AsSubNet() != $2->AsSubNet()
eval-type F $1->GetName() != $2->GetName() eval-type F $1->GetName() != $2->GetName()
eval-type P strcmp($1->Get()->PatternText(), $2->Get()->PatternText()) != 0
eval-mixed P S ! $1->MatchExactly($2->AsString()) eval-mixed P S ! $1->MatchExactly($2->AsString())
# Note, canonicalization means that GE and GT shouldn't occur # Note, canonicalization means that GE and GT shouldn't occur

View file

@ -84,3 +84,5 @@ negative index (PASS)
negative index (PASS) negative index (PASS)
+= of empty vector (PASS) += of empty vector (PASS)
+= of empty vector (PASS) += of empty vector (PASS)
pv1 == pv2 -> [T, F] (PASS)
pv1 != pv2 -> [F, T] (PASS)

View file

@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
1248 valid, 1890 tested, 437 skipped 1255 valid, 1908 tested, 440 skipped

View file

@ -241,4 +241,11 @@ event zeek_init()
v26 += vector(); v26 += vector();
test_case( "+= of empty vector", |v26| == 0 ); test_case( "+= of empty vector", |v26| == 0 );
# Pattern vectors
local pv1 = vector(/a/, /b/);
local pv2 = vector(/a/, /c/);
local pv_eq = pv1 == pv2;
local pv_ne = pv1 != pv2;
test_case( fmt("pv1 == pv2 -> %s", pv_eq), (pv_eq[0] == T) && (pv_eq[1] == F) );
test_case( fmt("pv1 != pv2 -> %s", pv_ne), (pv_ne[0] == F) && (pv_ne[1] == T) );
} }