From ad46fd69a65739a72f70e82ac048fb1b1562b70e Mon Sep 17 00:00:00 2001
From: Jason Dellaluce <jasondellaluce@gmail.com>
Date: Thu, 11 Jul 2024 09:19:18 +0000
Subject: [PATCH] fix(userspace/libsinsp): solve issues with negate comparisons
 on ip and ipnet checks

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
---
 userspace/libsinsp/filter/ast.h       |  2 +-
 userspace/libsinsp/filter_compare.cpp | 16 ++++++++--------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/userspace/libsinsp/filter/ast.h b/userspace/libsinsp/filter/ast.h
index 8022503cc1..4518d3056a 100644
--- a/userspace/libsinsp/filter/ast.h
+++ b/userspace/libsinsp/filter/ast.h
@@ -493,7 +493,7 @@ struct SINSP_PUBLIC unary_check_expr: expr
     bool is_equal(const expr* other) const override
     {
         auto o = dynamic_cast<const unary_check_expr*>(other);
-        return o != nullptr && left->is_equal(o->left.get());
+        return o != nullptr && left->is_equal(o->left.get()) && op == o->op;
     }
 
     std::unique_ptr<expr> left;
diff --git a/userspace/libsinsp/filter_compare.cpp b/userspace/libsinsp/filter_compare.cpp
index d409db0617..775974b7dc 100644
--- a/userspace/libsinsp/filter_compare.cpp
+++ b/userspace/libsinsp/filter_compare.cpp
@@ -650,25 +650,25 @@ bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void
 	case PT_IPV4ADDR:
 		if (op2_len != sizeof(struct in_addr))
 		{
-			return false;
+			return op == CO_NE;
 		}
 		return flt_compare_ipv4addr(op, flt_cast<uint32_t, uint64_t>(operand1), flt_cast<uint32_t, uint64_t>(operand2));
 	case PT_IPV4NET:
 		if (op2_len != sizeof(ipv4net))
 		{
-			return false;
+			return op == CO_NE;
 		}
 		return flt_compare_ipv4net(op, (uint64_t)*(uint32_t*)operand1, (ipv4net*)operand2);
 	case PT_IPV6ADDR:
 		if (op2_len != sizeof(ipv6addr))
 		{
-			return false;
+			return op == CO_NE;
 		}
 		return flt_compare_ipv6addr(op, (ipv6addr *)operand1, (ipv6addr *)operand2);
 	case PT_IPV6NET:
 		if (op2_len != sizeof(ipv6net))
 		{
-			return false;
+			return op == CO_NE;
 		}
 		return flt_compare_ipv6net(op, (ipv6addr *)operand1, (ipv6net*)operand2);
 	case PT_IPADDR:
@@ -676,7 +676,7 @@ bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void
 		{
 			if (op2_len != sizeof(struct in_addr))
 			{
-				return false;
+				return op == CO_NE;
 			}
 			return flt_compare(op, PT_IPV4ADDR, operand1, operand2, op1_len, op2_len);
 		}
@@ -684,7 +684,7 @@ bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void
 		{
 			if (op2_len != sizeof(ipv6addr))
 			{
-				return false;
+				return op == CO_NE;
 			}
 			return flt_compare(op, PT_IPV6ADDR, operand1, operand2, op1_len, op2_len);
 		}
@@ -697,7 +697,7 @@ bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void
 		{
 			if (op2_len != sizeof(ipv4net))
 			{
-				return false;
+				return op == CO_NE;
 			}
 			return flt_compare(op, PT_IPV4NET, operand1, operand2, op1_len, op2_len);
 		}
@@ -705,7 +705,7 @@ bool flt_compare(cmpop op, ppm_param_type type, const void* operand1, const void
 		{
 			if (op2_len != sizeof(ipv6net))
 			{
-				return false;
+				return op == CO_NE;
 			}
 			return flt_compare(op, PT_IPV6NET, operand1, operand2, op1_len, op2_len);
 		}