X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=0d49319608d2ff6dc53edf9d62429e055c50f152;hb=05b39faed2c73b317f6a7f6f59ec0e8c69e5d5bf;hp=b3ac863ba68119aedc59973b72c1d3716bb35867;hpb=097857e6c09bf9696797fe08cd4241b8315f9c9a;p=ccan diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index b3ac863b..0d493196 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -1,4 +1,4 @@ -#include "config.h" +/* Licensed under LGPLv3+ - see LICENSE file for details */ #include #include #include @@ -7,6 +7,7 @@ #include #include #include +#include #define SIZET_BITS (sizeof(size_t)*CHAR_BIT) @@ -27,8 +28,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 +323,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 @@ -455,14 +456,17 @@ char *tally_histogram(const struct tally *tally, } for (i = 0; i < height; i++) { - unsigned covered = 1; - count = (double)tally->counts[i] / largest_bucket * (width-1)+1; + unsigned covered = 1, row; - if (i == 0) + /* People expect minimum at the bottom. */ + row = height - i - 1; + count = (double)tally->counts[row] / largest_bucket * (width-1)+1; + + if (row == 0) covered = snprintf(p, width, "%zi", tally->min); - else if (i == height - 1) + else if (row == height - 1) covered = snprintf(p, width, "%zi", tally->max); - else if (i == bucket_of(tally->min, tally->step_bits, 0)) + else if (row == bucket_of(tally->min, tally->step_bits, 0)) *p = '+'; else *p = '|';