X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=396474b250e0c04be0b8a581a2fddc1bb553c7a4;hb=4a9245a21818f26efa403d952bc740d584aac292;hp=b89b0600ac301d0f5d088cb5a31176c841e1c8c3;hpb=60b47bf5e0878f014eb1e33e7784c92e43e2fcb6;p=ccan diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index b89b0600..396474b2 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -1,4 +1,3 @@ -#include "config.h" #include #include #include @@ -7,6 +6,7 @@ #include #include #include +#include #define SIZET_BITS (sizeof(size_t)*CHAR_BIT) @@ -27,8 +27,8 @@ 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)); if (tally) { @@ -322,7 +322,7 @@ ssize_t tally_total(const struct tally *tally, ssize_t *overflow) } /* If result is negative, make sure we can represent it. */ - if (tally->total[1] & (1 << (SIZET_BITS-1))) { + if (tally->total[1] & ((size_t)1 << (SIZET_BITS-1))) { /* Must have only underflowed once, and must be able to * represent result at ssize_t. */ if ((~tally->total[1])+1 != 0