commit 80cb199: [Minor] Fix some ubsan related issues

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Aug 12 17:42:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-08-12 18:35:59 +0100
URL: https://github.com/rspamd/rspamd/commit/80cb19900c3ab36faf92060ea639bccdcda5eaa9 (HEAD -> master)

[Minor] Fix some ubsan related issues

---
 contrib/aho-corasick/acism_create.c | 2 +-
 contrib/libucl/ucl_hash.c           | 4 ++--
 src/libcryptobox/base64/ref.c       | 4 ++--
 src/libserver/url.c                 | 5 ++++-
 src/libutil/str_util.c              | 6 +++---
 src/libutil/uthash_strcase.h        | 4 ++--
 6 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/contrib/aho-corasick/acism_create.c b/contrib/aho-corasick/acism_create.c
index 6b842cf3b..2d4439ffe 100644
--- a/contrib/aho-corasick/acism_create.c
+++ b/contrib/aho-corasick/acism_create.c
@@ -158,7 +158,7 @@ fill_symv(ACISM *psp, MEMREF const *strv, int nstrs)
 
 #if ACISM_SIZE < 8
     psp->sym_bits = bitwid(psp->nsyms);
-    psp->sym_mask = ~(-1 << psp->sym_bits);
+    psp->sym_mask = ~((~0u) << psp->sym_bits);
 #endif
 }
 
diff --git a/contrib/libucl/ucl_hash.c b/contrib/libucl/ucl_hash.c
index a8e735d13..dbd19c928 100644
--- a/contrib/libucl/ucl_hash.c
+++ b/contrib/libucl/ucl_hash.c
@@ -66,7 +66,7 @@ extern const guchar lc_map[256];
 static inline uint32_t
 ucl_hash_func (const ucl_object_t *o)
 {
-	return rspamd_cryptobox_fast_hash (o->key, o->keylen, 0xb9a1ef83c4561c95ULL);
+	return (uint32_t)rspamd_cryptobox_fast_hash (o->key, o->keylen, 0xb9a1ef83c4561c95ULL);
 }
 
 static inline int
@@ -124,7 +124,7 @@ ucl_hash_caseless_func (const ucl_object_t *o)
 		break;
 	}
 
-	return rspamd_cryptobox_fast_hash_final (&hst);
+	return (uint32_t)rspamd_cryptobox_fast_hash_final (&hst);
 }
 
 
diff --git a/src/libcryptobox/base64/ref.c b/src/libcryptobox/base64/ref.c
index 6d3c295f1..541e4e929 100644
--- a/src/libcryptobox/base64/ref.c
+++ b/src/libcryptobox/base64/ref.c
@@ -34,7 +34,7 @@ extern const uint8_t base64_table_dec[256];
 #define INNER_LOOP_64 do { \
 	while (inlen >= 13) { \
 		uint64_t str, res, dec; \
-		str = *(uint64_t *)c; \
+		memcpy(&str, c, sizeof(str)); \
 		str = GUINT64_TO_BE(str); \
 		if ((dec = base64_table_dec[str >> 56]) > 63) { \
 			break; \
@@ -80,7 +80,7 @@ extern const uint8_t base64_table_dec[256];
 #define INNER_LOOP_32 do { \
 	while (inlen >= 8) { \
 		uint32_t str, res, dec; \
-		str = *(uint32_t *)c; \
+		memcpy(&str, c, sizeof(str)); \
 		str = GUINT32_TO_BE(str); \
 		if ((dec = base64_table_dec[str >> 24]) > 63) { \
 			break; \
diff --git a/src/libserver/url.c b/src/libserver/url.c
index 26e328a6d..240af9d03 100644
--- a/src/libserver/url.c
+++ b/src/libserver/url.c
@@ -1684,7 +1684,10 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool)
 		 * 192.168 -> 192.0.0.168
 		 */
 		shift = 8 * (4 - i);
-		n |= t << shift;
+
+		if (shift < 32) {
+			n |= t << shift;
+		}
 
 		if (check_num && dots <= 4) {
 			memcpy (&in4, &n, sizeof (in4));
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c
index 962ed1871..1f2c4629f 100644
--- a/src/libutil/str_util.c
+++ b/src/libutil/str_util.c
@@ -255,7 +255,7 @@ rspamd_strcase_hash (gconstpointer key)
 
 	len = strlen (p);
 
-	return rspamd_icase_hash (p, len, rspamd_hash_seed ());
+	return (guint)rspamd_icase_hash (p, len, rspamd_hash_seed ());
 }
 
 guint
@@ -293,7 +293,7 @@ rspamd_ftok_icase_hash (gconstpointer key)
 {
 	const rspamd_ftok_t *f = key;
 
-	return rspamd_icase_hash (f->begin, f->len, rspamd_hash_seed ());
+	return (guint)rspamd_icase_hash (f->begin, f->len, rspamd_hash_seed ());
 }
 
 gboolean
@@ -791,7 +791,7 @@ while (0)
 	cols = 0;
 
 	while (inlen > 6) {
-		n = *(guint64 *)in;
+		memcpy (&n, in, sizeof (n));
 		n = GUINT64_TO_BE (n);
 
 		if (str_len <= 0 || cols <= str_len - 8) {
diff --git a/src/libutil/uthash_strcase.h b/src/libutil/uthash_strcase.h
index 5d6f2773c..a4418b442 100644
--- a/src/libutil/uthash_strcase.h
+++ b/src/libutil/uthash_strcase.h
@@ -26,7 +26,7 @@
 /* Utils for uthash tuning */
 #ifndef HASH_CASELESS
 #define HASH_FUNCTION(key,keylen,num_bkts,hashv,bkt) do {\
-	hashv = rspamd_cryptobox_fast_hash(key, keylen, rspamd_hash_seed ()); \
+	hashv = (__typeof (hashv))rspamd_cryptobox_fast_hash(key, keylen, rspamd_hash_seed ()); \
 	bkt = (hashv) & (num_bkts-1); \
 } while (0)
 
@@ -78,7 +78,7 @@
 		rspamd_cryptobox_fast_hash_update (&_hst, &_u, sizeof (_u)); \
 		break; \
 	} \
-	hashv = rspamd_cryptobox_fast_hash_final (&_hst); \
+	hashv = (__typeof (hashv))rspamd_cryptobox_fast_hash_final (&_hst); \
 	bkt = (hashv) & (num_bkts-1); \
 } while (0)
 #define HASH_KEYCMP(a,b,len) rspamd_lc_cmp(a,b,len)


More information about the Commits mailing list