To compute the difference between two addresses, cast the pointers
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 6 Sep 2008 18:27:33 +0000 (18:27 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 6 Sep 2008 18:27:33 +0000 (18:27 +0000)
holding those addresses to "void *" and then to "char *" (so we don't
get warnings from casting directly to "char *" or errors from
subtracting two "void *"s), and subtract them, rather than casting the
pointers to an integral type possibly shorter than the pointers (to
avoid warnings and to avoid the admittedly-infinitesimal chance that the
two pointers don't differ in the bits that fit into the integral type).

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@26151 f5534014-38df-0310-8fa8-9805f1628bb7

plugins/mate/mate_util.c

index 5f450185ab27005ac771bd17c375ebd7a62ff9a3..a2ac794d79e2ab3b366ab6f6194d1dab35ebcc56 100644 (file)
 #include "mate_util.h"
 #include <wsutil/file_util.h>
 
+/***************************************************************************
+*  ADDRDIFF
+***************************************************************************
+* This is a macro that computes the difference between the raw address
+* values of two pointers (rather than the difference between the pointers)
+* as a ptrdiff_t.
+***************************************************************************/
+#define ADDRDIFF(p,q)  (((char *)(void *)(p)) - ((char *)(void *)(q)))
+
+
 /***************************************************************************
 *  dbg_print
 ***************************************************************************
@@ -815,7 +825,7 @@ extern gchar* avpl_to_dotstr(AVPL* avpl) {
 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
        AVPN* cd = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t c;
        AVP* copy;
 
 #ifdef _AVP_DEBUGGING
@@ -828,7 +838,7 @@ extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
        while(cs->avp) {
 
                if(cd->avp) {
-                       c = (guint) cd->avp->n - (guint) cs->avp->n;
+                       c = ADDRDIFF(cd->avp->n,cs->avp->n);
                } else {
                        c = -1;
                }
@@ -1094,7 +1104,7 @@ extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboole
        AVPL* newavpl;
        AVPN* co = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t c;
        AVP* m;
        AVP* copy;
        gboolean matches;
@@ -1123,7 +1133,7 @@ extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboole
                        break;
                }
 
-               c = (guint) co->avp->n - (guint) cs->avp->n;
+               c = ADDRDIFF(co->avp->n,cs->avp->n);
 
                if ( c > 0 ) {
                        delete_avpl(newavpl,TRUE);
@@ -1185,9 +1195,9 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
        AVPL* newavpl = new_avpl(name);
        AVPN* co = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t c;
        AVP* m;
-    AVP* copy;
+       AVP* copy;
 
 #ifdef _AVP_DEBUGGING
        dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
@@ -1205,7 +1215,7 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
        co = op->null.next;
        while(1) {
 
-               c = (guint) co->avp->n - (guint) cs->avp->n;
+               c = ADDRDIFF(co->avp->n,cs->avp->n);
 
                if ( c > 0 ) {
                        delete_avpl(newavpl,TRUE);