Browse Source

uthash.h: Swap multiplicands to put the widest ones first.

It was reported in #195 that Visual Studio 2019, on a 32-bit platform,
produces this warning:

    Warning C26451
    Arithmetic overflow: Using operator '*' on a 4 byte value
    and then casting the result to a 8 byte value. Cast the
    value to the wider type before calling operator '*' to
    avoid overflow (io.2).

I'm not 100% sure that this tweak will silence the warning,
but I think it might, and at least it's harmless.

Fixes #195.
Arthur O'Dwyer 4 years ago
parent
commit
973bd67220
1 changed files with 2 additions and 2 deletions
  1. 2 2
      src/uthash.h

+ 2 - 2
src/uthash.h

@@ -843,12 +843,12 @@ do {
   struct UT_hash_handle *_he_thh, *_he_hh_nxt;                                   \
   UT_hash_bucket *_he_new_buckets, *_he_newbkt;                                  \
   _he_new_buckets = (UT_hash_bucket*)uthash_malloc(                              \
-           2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket));            \
+           sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U);             \
   if (!_he_new_buckets) {                                                        \
     HASH_RECORD_OOM(oomed);                                                      \
   } else {                                                                       \
     uthash_bzero(_he_new_buckets,                                                \
-        2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket));               \
+        sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U);                \
     (tbl)->ideal_chain_maxlen =                                                  \
        ((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) +                      \
        ((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U);    \