From 742c1a7442763964546f79d76b615bfcb476d604 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 1 Nov 2008 10:45:39 +0000 Subject: [PATCH] Protect against division by zero when handling directories with total size of zero. git-svn-id: svn://svn.tartarus.org/sgt/agedu@8237 cda61777-01e9-0310-a592-d414129be87e --- html.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/html.c b/html.c index 3ce87c0..76ecd83 100644 --- a/html.c +++ b/html.c @@ -316,11 +316,18 @@ static void print_heading(struct html *ctx, const char *title) #define PIXEL_SIZE 600 /* FIXME: configurability? */ static void write_report_line(struct html *ctx, struct vector *vec) { - unsigned long long size, asize; + unsigned long long size, asize, divisor; int pix, newpix; int i; /* + * Prevent divisions by zero. + */ + divisor = ctx->totalsize; + if (!divisor) + divisor = 1; + + /* * Find the total size of this subdirectory. */ size = vec->sizes[MAXCOLOUR]; @@ -336,7 +343,7 @@ static void write_report_line(struct html *ctx, struct vector *vec) pix = 0; for (i = 0; i <= MAXCOLOUR; i++) { asize = vec->sizes[i]; - newpix = asize * PIXEL_SIZE / ctx->totalsize; + newpix = asize * PIXEL_SIZE / divisor; add_to_colour_bar(ctx, i, newpix - pix); pix = newpix; } @@ -348,7 +355,7 @@ static void write_report_line(struct html *ctx, struct vector *vec) * Output size as a percentage of totalsize. */ htprintf(ctx, "" - "%.2f%%\n", (double)size / ctx->totalsize * 100.0); + "%.2f%%\n", (double)size / divisor * 100.0); /* * Output a subdirectory marker. -- 2.11.0