[PATCH v2 1/2] inetmib: Avoid overflows in IPv4 address comparisons.

Francois Gouget fgouget at codeweavers.com
Tue Feb 15 08:32:54 CST 2022


The difference between two ULONGs may not fit in an int, causing
comparison errors.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
v2: Introduce DWORD_cmp() to simplify the comparisons.

compareIpAddrRow() and oidToIpForwardRow() should also do byte-swapping
which will be done in the next patch.
---
 dlls/inetmib1/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/dlls/inetmib1/main.c b/dlls/inetmib1/main.c
index 90da277375e..bd39abb6240 100644
--- a/dlls/inetmib1/main.c
+++ b/dlls/inetmib1/main.c
@@ -770,11 +770,15 @@ static void oidToIpAddrRow(AsnObjectIdentifier *oid, void *dst)
     row->dwAddr = oidToIpAddr(oid);
 }
 
-static int __cdecl compareIpAddrRow(const void *a, const void *b)
+static int __cdecl DWORD_cmp(DWORD a, DWORD b)
 {
-    const MIB_IPADDRROW *key = a, *value = b;
+    return a < b ? -1 : a > b ? 1 : 0; /* a substraction would overflow */
+}
 
-    return key->dwAddr - value->dwAddr;
+static int __cdecl compareIpAddrRow(const void *a, const void *b)
+{
+    const MIB_IPADDRROW *rowA = a, *rowB = b;
+    return DWORD_cmp(rowA->dwAddr, rowB->dwAddr);
 }
 
 static BOOL mib2IpAddrQuery(BYTE bPduType, SnmpVarBind *pVarBind,
@@ -865,9 +869,8 @@ static void oidToIpForwardRow(AsnObjectIdentifier *oid, void *dst)
 
 static int __cdecl compareIpForwardRow(const void *a, const void *b)
 {
-    const MIB_IPFORWARDROW *key = a, *value = b;
-
-    return key->dwForwardDest - value->dwForwardDest;
+    const MIB_IPFORWARDROW *rowA = a, *rowB = b;
+    return DWORD_cmp(rowA->dwForwardDest, rowB->dwForwardDest);
 }
 
 static BOOL mib2IpRouteQuery(BYTE bPduType, SnmpVarBind *pVarBind,
@@ -1223,13 +1226,9 @@ static void oidToUdpRow(AsnObjectIdentifier *oid, void *dst)
 
 static int __cdecl compareUdpRow(const void *a, const void *b)
 {
-    const MIB_UDPROW *key = a, *value = b;
-    int ret;
-
-    ret = ntohl(key->dwLocalAddr) - ntohl(value->dwLocalAddr);
-    if (ret == 0)
-        ret = ntohs(key->dwLocalPort) - ntohs(value->dwLocalPort);
-    return ret;
+    const MIB_UDPROW *rowA = a, *rowB = b;
+    return DWORD_cmp(ntohl(rowA->dwLocalAddr), ntohl(rowB->dwLocalAddr)) ||
+           ntohs(rowA->dwLocalPort) - ntohs(rowB->dwLocalPort);
 }
 
 static BOOL mib2UdpEntryQuery(BYTE bPduType, SnmpVarBind *pVarBind,
-- 
2.30.2




More information about the wine-devel mailing list