Francois Gouget : inetmib1: Avoid overflows in IPv4 address comparisons.

Alexandre Julliard julliard at winehq.org
Wed Feb 16 15:30:25 CST 2022


Module: wine
Branch: master
Commit: 85c6caf65b43c289a84b062e761005b8d03d1e48
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=85c6caf65b43c289a84b062e761005b8d03d1e48

Author: Francois Gouget <fgouget at codeweavers.com>
Date:   Wed Feb 16 13:05:48 2022 +0100

inetmib1: Avoid overflows in IPv4 address comparisons.

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

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
Signed-off-by: Huw Davies <huw at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 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..e66dbe0a811 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 subtraction 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,




More information about the wine-cvs mailing list