Changeset: 3611c685cc6d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3611c685cc6d
Modified Files:
        gdk/gdk_hash.c
        geom/monetdb5/geom.c
Branch: Sep2022
Log Message:

Fix geom hashes to not use undefined operations.
Such as left shift of negative value, or case NaN to int and then
multiplying while ignoring overflow.


diffs (85 lines):

diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -100,10 +100,11 @@ HASHclear(Hash *h)
        memset(h->Bckt, 0xFF, h->nbucket * h->width);
 }
 
-#define HASH_VERSION           4
-/* this is only for the change of hash function of the UUID type; if
- * HASH_VERSION is increased again from 4, the code associated with
- * HASH_VERSION_NOUUID must be deleted */
+#define HASH_VERSION           5
+/* this is only for the change of hash function of the UUID type and MBR
+ * type; if HASH_VERSION is increased again from 5, the code associated
+ * with HASH_VERSION_NOUUID and HASH_VERSION_NOMBR must be deleted */
+#define HASH_VERSION_NOMBR     4
 #define HASH_VERSION_NOUUID    3
 #define HASH_HEADER_SIZE       7       /* nr of size_t fields in header */
 
@@ -508,7 +509,17 @@ BATcheckhash(BAT *b)
                                                         ((size_t) 1 << 24) |
 #endif
                                                         HASH_VERSION_NOUUID) &&
-                                                strcmp(ATOMname(b->ttype), 
"uuid") != 0)
+                                                strcmp(ATOMname(b->ttype), 
"uuid") != 0 &&
+                                                strcmp(ATOMname(b->ttype), 
"mbr") != 0)
+#endif
+#ifdef HASH_VERSION_NOMBR
+                                            /* if not uuid, also allow 
previous version */
+                                            || (hdata[0] == (
+#ifdef PERSISTENTHASH
+                                                        ((size_t) 1 << 24) |
+#endif
+                                                        HASH_VERSION_NOMBR) &&
+                                                strcmp(ATOMname(b->ttype), 
"mbr") != 0)
 #endif
                                                    ) &&
                                            hdata[1] > 0 &&
diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -5169,7 +5169,15 @@ wkbHASH(const void *W)
        BUN h = 0;
 
        for (i = 0; i < (w->len - 1); i += 2) {
-               int a = *(w->data + i), b = *(w->data + i + 1);
+               BUN a = ((unsigned char *) w->data)[i];
+               BUN b = ((unsigned char *) w->data)[i + 1];
+#if '\377' < 0                                 /* char is signed? */
+               /* maybe sign extend */
+               if (a & 0x80)
+                       a |= ~(BUN)0x7f;
+               if (b & 0x80)
+                       b |= ~(BUN)0x7f;
+#endif
                h = (h << 3) ^ (h >> 11) ^ (h >> 17) ^ (b << 8) ^ a;
        }
        return h;
@@ -5396,7 +5404,8 @@ static BUN
 mbrHASH(const void *ATOM)
 {
        const mbr *atom = ATOM;
-       return (BUN) (((int) atom->xmin * (int)atom->ymin) *((int) atom->xmax * 
(int)atom->ymax));
+       return ATOMhash(TYPE_flt, &atom->xmin) ^ ATOMhash(TYPE_flt, 
&atom->ymin) ^
+               ATOMhash(TYPE_flt, &atom->xmax) ^ ATOMhash(TYPE_flt, 
&atom->ymax);
 }
 
 static const void *
@@ -5622,7 +5631,15 @@ wkbaHASH(const void *WARRAY)
        for (j = 0; j < wArray->itemsNum; j++) {
                wkb *w = wArray->data[j];
                for (i = 0; i < (w->len - 1); i += 2) {
-                       int a = *(w->data + i), b = *(w->data + i + 1);
+                       BUN a = ((unsigned char *) w->data)[i];
+                       BUN b = ((unsigned char *) w->data)[i + 1];
+#if '\377' < 0                                 /* char is signed? */
+                       /* maybe sign extend */
+                       if (a & 0x80)
+                               a |= ~(BUN)0x7f;
+                       if (b & 0x80)
+                               b |= ~(BUN)0x7f;
+#endif
                        h = (h << 3) ^ (h >> 11) ^ (h >> 17) ^ (b << 8) ^ a;
                }
        }
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to