]> git.ozlabs.org Git - ccan/blobdiff - ccan/tally/tally.c
tally: Fix a c++ warning
[ccan] / ccan / tally / tally.c
index 0d0190795557b60ccfe8d73138dfda2baf99477d..d3d320eb1b0657d0e53bf2d042d49f913f207fdd 100644 (file)
@@ -1,4 +1,4 @@
-#include "config.h"
+/* Licensed under LGPLv3+ - see LICENSE file for details */
 #include <ccan/tally/tally.h>
 #include <ccan/build_assert/build_assert.h>
 #include <ccan/likely/likely.h>
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
+#include <stdlib.h>
 
 #define SIZET_BITS (sizeof(size_t)*CHAR_BIT)
 
@@ -27,10 +28,11 @@ struct tally *tally_new(unsigned buckets)
        if (buckets == 0)
                buckets = 1;
 
-       /* Check for overflow. */
-       if (buckets && SIZE_MAX / buckets < sizeof(tally->counts[0]))
+       /* Overly cautious check for overflow. */
+       if (sizeof(*tally) * buckets / sizeof(*tally) != buckets)
                return NULL;
-       tally = malloc(sizeof(*tally) + sizeof(tally->counts[0])*(buckets-1));
+       tally = (struct tally *)malloc(
+               sizeof(*tally) + sizeof(tally->counts[0])*(buckets-1));
        if (tally) {
                tally->max = ((size_t)1 << (SIZET_BITS - 1));
                tally->min = ~tally->max;