From 01b572bc623cec3f7ff4f6ffb2dbf47d28ee7559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20Sp=C3=A6ren?= Date: Sat, 16 Oct 2021 00:55:34 +0200 Subject: [PATCH] zdb: fix overflow of time estimation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The calculation of estimated time remaining in zdb -cc could overflow, as reported in #10666. This patch fixes this, by using uint64_t instead of ints in the calculations. Reviewed-by: Brian Behlendorf Signed-off-by: Teodor Spæren Closes #10666 Closes #12610 --- cmd/zdb/zdb.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/zdb/zdb.c b/cmd/zdb/zdb.c index 797cac0874..81b9604f37 100644 --- a/cmd/zdb/zdb.c +++ b/cmd/zdb/zdb.c @@ -5469,9 +5469,9 @@ zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, uint64_t now = gethrtime(); char buf[10]; uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; - int kb_per_sec = + uint64_t kb_per_sec = 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); - int sec_remaining = + uint64_t sec_remaining = (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; /* make sure nicenum has enough space */ @@ -5479,8 +5479,9 @@ zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, zfs_nicebytes(bytes, buf, sizeof (buf)); (void) fprintf(stderr, - "\r%5s completed (%4dMB/s) " - "estimated time remaining: %uhr %02umin %02usec ", + "\r%5s completed (%4"PRIu64"MB/s) " + "estimated time remaining: " + "%"PRIu64"hr %02"PRIu64"min %02"PRIu64"sec ", buf, kb_per_sec / 1024, sec_remaining / 60 / 60, sec_remaining / 60 % 60,