Skip to content

Commit 67d910d

Browse files
UOS-liangadam900710
authored andcommitted
btrfs-progs: fix size formatting for large values
pretty_size_snprintf() converts the size to a float for printing. A float has a 24 bit mantissa, so values above 16 MiB are rounded to a multiple of the unit: a 12 TB device is printed as 11718850560.00KiB instead of 11718850304KiB, and -m rounds the value up to a whole MiB. Print all units with integer arithmetic, including the auto detected unit used by the human readable output. Bytes and KiB/kB are exact, so they are printed as plain integers (KiB is divided by 1024). MiB and larger keep the two decimal digits, computed from the integer remainder. This removes the float conversion from pretty_size_snprintf() completely. Fixes: #1212 Signed-off-by: mahailiang <mahailiang@uniontech.com>
1 parent 5e64472 commit 67d910d

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

‎common/units.c‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ static const char* unit_suffix_decimal[] =
4545
int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mode)
4646
{
4747
int num_divs;
48-
float fraction;
4948
u64 base = 0;
5049
int mult = 0;
5150
const char** suffix = NULL;
5251
u64 last_size;
52+
u64 value;
53+
u64 whole;
54+
u64 frac;
55+
s64 svalue;
5356
int negative;
57+
int neg;
5458

5559
if (str_size == 0)
5660
return 0;
@@ -139,13 +143,41 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod
139143
return -1;
140144
}
141145

142-
if (negative) {
143-
fraction = (float)(s64)last_size / base;
144-
} else {
145-
fraction = (float)last_size / base;
146+
/*
147+
* Do not use floating point for any unit, explicit or auto detected.
148+
* Bytes and KiB/kB are exact, so print the plain integer. For MiB
149+
* and larger print the whole part plus two decimal digits computed
150+
* from the integer remainder: a float only has 24 mantissa bits, so
151+
* values above 16 MiB would be rounded to a multiple of the unit.
152+
*
153+
* In auto detection mode the value has already been scaled down and
154+
* last_size holds the value before the final division.
155+
*/
156+
if ((unit_mode & UNITS_MODE_MASK) == UNITS_RESERVED)
157+
value = last_size;
158+
else
159+
value = size;
160+
161+
svalue = (s64)value;
162+
if (num_divs <= 1) {
163+
if (negative)
164+
return snprintf(str, str_size, "%lld%s",
165+
svalue / (s64)base, suffix[num_divs]);
166+
return snprintf(str, str_size, "%llu%s", value / base,
167+
suffix[num_divs]);
168+
}
169+
170+
neg = negative && svalue < 0;
171+
whole = neg ? (u64)(-(svalue + 1)) + 1 : value;
172+
frac = ((whole % base) * 100 + base / 2) / base;
173+
whole /= base;
174+
if (frac >= 100) {
175+
whole++;
176+
frac -= 100;
146177
}
147178

148-
return snprintf(str, str_size, "%.2f%s", fraction, suffix[num_divs]);
179+
return snprintf(str, str_size, "%s%llu.%02llu%s",
180+
neg ? "-" : "", whole, frac, suffix[num_divs]);
149181
}
150182

151183
void units_set_mode(unsigned *units, unsigned mode)

0 commit comments

Comments
 (0)