Hi, I've discovered a bug in the new lsblk util, which incorrectly reports the size of a nearly 2TB partition: NAME MAJ:MIN RM SIZE RO MOUNTPOINT sda 8:0 0 83.9G 0 ââsda1 8:1 0 100M 0 /boot ââsda2 8:2 0 83.8G 0 / sdb 8:16 0 1.171798692T 0 ââsdb1 8:17 0 20G 0 /home ââsdb2 8:18 0 1.171798692T 0 /mnt/Gluttony sdc 8:32 0 931.5G 0 ââsdc1 8:33 0 931.5G 0 /mnt/Haven sr0 11:0 1 2K 0 Looking at size_to_human_string (strutils.c:245), it looks like it's a simple integer overflow. With the attached patch applied, the output is now accurate: NAME MAJ:MIN RM SIZE RO MOUNTPOINT sda 8:0 0 83.9G 0 ââsda1 8:1 0 100M 0 /boot ââsda2 8:2 0 83.8G 0 / sdb 8:16 0 1.8T 0 ââsdb1 8:17 0 20G 0 /home ââsdb2 8:18 0 1.8T 0 /mnt/Gluttony sdc 8:32 0 931.5G 0 ââsdc1 8:33 0 931.5G 0 /mnt/Haven sr0 11:0 1 2K 0 thanks, dave reisner
>From 78e7c415db74723e8ac5ec696217afe26a05e3d8 Mon Sep 17 00:00:00 2001 From: Dave Reisner <d@xxxxxxxxxxxxxx> Date: Mon, 14 Feb 2011 12:12:31 -0500 Subject: [PATCH] strutils: avoid integer overflow on large values This is visible on a 2TB disk via lsblk, where a large partition incorrectly displays as 1.171798692T instead of 1.8T. This is corrected by using a uint64_t type instead of a simple int -- consistant with the type used in lsblk.c to represent the raw size in bytes. Signed-off-by: Dave Reisner <d@xxxxxxxxxxxxxx> --- lib/strutils.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/strutils.c b/lib/strutils.c index 94635b1..303d692 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -245,7 +245,8 @@ static int get_exp(uint64_t n) char *size_to_human_string(uint64_t bytes) { char buf[32]; - int dec, frac, exp; + int dec, exp; + uint64_t frac; const char *letters = "BKMGTP"; char c; @@ -267,7 +268,7 @@ char *size_to_human_string(uint64_t bytes) if (!dp || !*dp) dp = "."; - snprintf(buf, sizeof(buf), "%d%s%d%c", dec, dp, frac, c); + snprintf(buf, sizeof(buf), "%d%s%jd%c", dec, dp, frac, c); } else snprintf(buf, sizeof(buf), "%d%c", dec, c); -- 1.7.4.1