From c4a5ac8bba611206e062c0955fb605bfc0f48b0f Mon Sep 17 00:00:00 2001
From: Mark Zhang <markzhang@nvidia.com>
Date: Fri, 26 Apr 2024 14:17:55 +0300
Subject: [PATCH] librdmacm: Fix an overflow bug in qsort comparison function

Reference: https://github.com/linux-rdma/rdma-core/commit/c4a5ac8bba611206e062c0955fb605bfc0f48b0f

The comparison function dev_cmp() doesn't work with 64b pointers in some
cases, as it casts the pointer to int. For example it's not able to sort
this list:
  {0xfffe0c2f0b00, 0xaaac741b4a90, 0xaaac741b4d70}

Fixes: e5d371cb0af0 ("librdmacm: Globally store and sort IB device list")
Signed-off-by: Mark Zhang <markzhang@nvidia.com>
Reviewed-by: Leon Romanovsky <leonro@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
---
 librdmacm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/librdmacm/cma.c b/librdmacm/cma.c
index 7b924bd..0a631bd 100644
--- a/librdmacm/cma.c
+++ b/librdmacm/cma.c
@@ -311,7 +311,7 @@ static void remove_cma_dev(struct cma_device *cma_dev)
 
 static int dev_cmp(const void *a, const void *b)
 {
-	return (int)(*(char *const *)a - *(char *const *)b);
+	return (*(uintptr_t *)a > *(uintptr_t *)b) - (*(uintptr_t *)a < *(uintptr_t *)b);
 }
 
 static int sync_devices_list(void)
-- 
2.27.0

