Blob Blame History Raw
From 657025565cbd4640fd7f85a2e55ce470e1da748a Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 9 Mar 2017 13:29:37 +0000
Subject: [PATCH 10/26] common/tmap.c: Safe comparison of two pointers.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Subtracting two pointers and assigning the result to a 32 bit integer
is never safe on 64 bit architectures -- ptrdiff_t should be used in
this situation.  Also subtracting void pointers is undefined in C.

Since we require an 'int' result, the easiest fix is to open code the
comparison we require.  GCC optimizes this function to six instructions.

The original warning was:

tmap.c: In function ‘ptrcmp’:
tmap.c:67:21: error: pointer of type ‘void *’ used in subtraction [-Werror=pointer-arith]
     return (int) (v0-v1);
                     ^

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 common/tmap.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/common/tmap.c b/common/tmap.c
index afa2203..bdbd0e3 100644
--- a/common/tmap.c
+++ b/common/tmap.c
@@ -64,7 +64,12 @@ static TMapNode nil = { NULL, NULL, false, &nil, &nil, &nil };
 static int
 ptrcmp(const void *v0, const void *v1)
 {
-    return (int) (v0-v1);
+    if (v0 == v1)
+        return 0;
+    else if (v0 < v1)
+        return -1;
+    else
+        return 1;
 }
 
 static void
-- 
2.10.2