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 !=
This commit is contained in:
Tim Wojtulewicz 2024-12-16 12:37:25 -07:00
commit 33eaa5ccda
7 changed files with 35 additions and 7 deletions

10
CHANGES
View file

@ -1,3 +1,13 @@
7.2.0-dev.11 | 2024-12-16 12:37:25 -0700
* ZAM/relexpr-op NE for patterns (Vern Paxson, Corelight)
* 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.
7.2.0-dev.8 | 2024-12-16 10:19:18 -0700
* QUIC/decrypt_crypto: Actually check if decryption was successful (Arne Welzel, Corelight)

View file

@ -1 +1 @@
7.2.0-dev.8
7.2.0-dev.11

View file

@ -925,12 +925,20 @@ ValPtr BinaryExpr::PatternFold(Val* v1, Val* v2) const {
const RE_Matcher* re1 = v1->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");
}
RE_Matcher* res = tag == EXPR_AND ? RE_Matcher_conjunction(re1, re2) : RE_Matcher_disjunction(re1, re2);
return make_intrusive<PatternVal>(res);
return res;
}
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())
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
eval $1 != $2
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 N $1->AsSubNet() != $2->AsSubNet()
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())
# Note, canonicalization means that GE and GT shouldn't occur

View file

@ -84,3 +84,5 @@ negative index (PASS)
negative index (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.
1248 valid, 1890 tested, 437 skipped
1255 valid, 1908 tested, 440 skipped

View file

@ -241,4 +241,11 @@ event zeek_init()
v26 += vector();
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) );
}