]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix more alignment and ubsan issues
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 12 Aug 2019 20:37:19 +0000 (21:37 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 12 Aug 2019 20:37:19 +0000 (21:37 +0100)
src/libcryptobox/base64/ref.c
src/libserver/rspamd_symcache.c
src/libserver/url.c
src/libutil/str_util.h

index 541e4e929c7fd09d0978cdf9d08165d077f584ee..c7cefd7612c2fe4e36eddacbf6563c04d1964d8e 100644 (file)
@@ -28,13 +28,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include "config.h"
+#include "libutil/util.h"
 
 extern const uint8_t base64_table_dec[256];
 
 #define INNER_LOOP_64 do { \
+       uint64_t str, res, dec; \
+       bool aligned = rspamd_is_aligned_as(c, str); \
+       bool oaligned = rspamd_is_aligned_as(o, res); \
        while (inlen >= 13) { \
-               uint64_t str, res, dec; \
-               memcpy(&str, c, sizeof(str)); \
+               if (aligned) { str = *(uint64_t *)c; } else {memcpy(&str, c, sizeof(str)); } \
                str = GUINT64_TO_BE(str); \
                if ((dec = base64_table_dec[str >> 56]) > 63) { \
                        break; \
@@ -69,7 +72,7 @@ extern const uint8_t base64_table_dec[256];
                } \
                res |= dec << 16; \
                res = GUINT64_FROM_BE(res); \
-               *(uint64_t *)o = res; \
+               if (oaligned) {*(uint64_t *)o = res;} else {memcpy(o, &res, sizeof(res));} \
                c += 8; \
                o += 6; \
                outl += 6; \
@@ -78,9 +81,11 @@ extern const uint8_t base64_table_dec[256];
 } while (0)
 
 #define INNER_LOOP_32 do { \
+       uint32_t str, res, dec; \
+       bool aligned = rspamd_is_aligned_as(c, str); \
+       bool oaligned = rspamd_is_aligned_as(o, res); \
        while (inlen >= 8) { \
-               uint32_t str, res, dec; \
-               memcpy(&str, c, sizeof(str)); \
+               if (aligned) { str = *(uint32_t *)c; } else {memcpy(&str, c, sizeof(str)); } \
                str = GUINT32_TO_BE(str); \
                if ((dec = base64_table_dec[str >> 24]) > 63) { \
                        break; \
@@ -99,7 +104,7 @@ extern const uint8_t base64_table_dec[256];
                } \
                res |= dec << 8; \
                res = GUINT32_FROM_BE(res); \
-               *(uint32_t *)o = res; \
+               if (oaligned) {*(uint32_t *)o = res;} else {memcpy(o, &res, sizeof(res));} \
                c += 4; \
                o += 3; \
                outl += 3; \
@@ -150,7 +155,7 @@ repeat:
                                break;
                        }
                        *o++ = carry | (q >> 4);
-                       carry = q << 4;
+                       carry = (uint8_t)(q << 4);
                        leftover++;
                        outl++;
 
index ad5bd4e1c663fd91f205f7483844f44cc79d9776..f91aa9a22834282bcda2a6b59195ad4bfb2a4233 100644 (file)
@@ -1232,7 +1232,7 @@ rspamd_symcache_new (struct rspamd_config *cfg)
        cache->cfg = cfg;
        cache->cksum = 0xdeadbabe;
        cache->peak_cb = -1;
-       cache->id = rspamd_random_uint64_fast ();
+       cache->id = (guint)rspamd_random_uint64_fast ();
 
        return cache;
 }
index 240af9d0395cc1acf70ac8c3e36d64f72a689f45..ef59b6da0591d67550a458cf1f1eca34118f88ec 100644 (file)
@@ -1188,7 +1188,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len,
                                                (*flags) |= RSPAMD_URL_FLAG_IDN;
                                                guint i = 0;
 
-                                               U8_NEXT (p, i, last - p, uc);
+                                               U8_NEXT (((const guchar *)p), i, last - p, uc);
 
                                                if (uc < 0) {
                                                        /* Bad utf8 */
index c820bd10cb928f8d87a723785902be62596f619c..a1f9805266a144ee48b5276af3efff566121f400 100644 (file)
@@ -20,6 +20,8 @@
 #include "ucl.h"
 #include "fstring.h"
 
+#include <stdalign.h>
+
 #ifdef  __cplusplus
 extern "C" {
 #endif
@@ -422,23 +424,35 @@ gsize rspamd_memspn (const gchar *s, const gchar *e, gsize len);
 
 /* https://graphics.stanford.edu/~seander/bithacks.html#HasMoreInWord */
 #define rspamd_str_hasmore(x, n) ((((x)+~0UL/255*(127-(n)))|(x))&~0UL/255*128)
+/*
+ * Check if a pointer is aligned; n must be power of two
+ */
+#define rspamd_is_aligned(p, n) (((uintptr_t)(p) & ((uintptr_t)(n) - 1)) == 0)
+#define rspamd_is_aligned_as(p, v) rspamd_is_aligned(p, _Alignof(__typeof((v))))
 
 static inline gboolean
-rspamd_str_has_8bit (const guchar *beg, gsize len) {
+rspamd_str_has_8bit (const guchar *beg, gsize len)
+{
        unsigned long *w;
-       gsize i, leftover = len % sizeof (*w);
+       gsize i, leftover;
 
-       w = (unsigned long *) beg;
+       if (rspamd_is_aligned_as (beg, *w)) {
+               leftover = len % sizeof (*w);
+               w = (unsigned long *) beg;
 
-       for (i = 0; i < len / sizeof (*w); i++) {
-               if (rspamd_str_hasmore (*w, 127)) {
-                       return TRUE;
+               for (i = 0; i < len / sizeof (*w); i++) {
+                       if (rspamd_str_hasmore (*w, 127)) {
+                               return TRUE;
+                       }
+
+                       w++;
                }
 
-               w++;
+               beg = (const guchar *) w;
+       }
+       else {
+               leftover = len;
        }
-
-       beg = (const guchar *) w;
 
        for (i = 0; i < leftover; i++) {
                if (beg[i] > 127) {