diff options
author | Michael Lyle <mlyle@lyle.org> | 2017-09-06 02:26:02 -0400 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2017-09-06 10:17:33 -0400 |
commit | 9276717b9e297a62d1151a43d1cd286213f68eb7 (patch) | |
tree | 4b2a34a8ee2f8f8c01138558b064b655e3d1424a | |
parent | 7b6a8570e02a20d0b6cadb9689e4b2e6217c390c (diff) |
bcache: fix bch_hprint crash and improve output
Most importantly, solve a crash where %llu was used to format signed
numbers. This would cause a buffer overflow when reading sysfs
writeback_rate_debug, as only 20 bytes were allocated for this and
%llu writes 20 characters plus a null.
Always use the units mechanism rather than having different output
paths for simplicity.
Also, correct problems with display output where 1.10 was a larger
number than 1.09, by multiplying by 10 and then dividing by 1024 instead
of dividing by 100. (Remainders of >= 1000 would print as .10).
Minor changes: Always display the decimal point instead of trying to
omit it based on number of digits shown. Decide what units to use
based on 1000 as a threshold, not 1024 (in other words, always print
at most 3 digits before the decimal point).
Signed-off-by: Michael Lyle <mlyle@lyle.org>
Reported-by: Dmitry Yu Okunev <dyokunev@ut.mephi.ru>
Acked-by: Kent Overstreet <kent.overstreet@gmail.com>
Reviewed-by: Coly Li <colyli@suse.de>
Cc: stable@vger.kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r-- | drivers/md/bcache/util.c | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/drivers/md/bcache/util.c b/drivers/md/bcache/util.c index 8c3a938f4bf0..176d3c2ef5f5 100644 --- a/drivers/md/bcache/util.c +++ b/drivers/md/bcache/util.c | |||
@@ -74,24 +74,44 @@ STRTO_H(strtouint, unsigned int) | |||
74 | STRTO_H(strtoll, long long) | 74 | STRTO_H(strtoll, long long) |
75 | STRTO_H(strtoull, unsigned long long) | 75 | STRTO_H(strtoull, unsigned long long) |
76 | 76 | ||
77 | /** | ||
78 | * bch_hprint() - formats @v to human readable string for sysfs. | ||
79 | * | ||
80 | * @v - signed 64 bit integer | ||
81 | * @buf - the (at least 8 byte) buffer to format the result into. | ||
82 | * | ||
83 | * Returns the number of bytes used by format. | ||
84 | */ | ||
77 | ssize_t bch_hprint(char *buf, int64_t v) | 85 | ssize_t bch_hprint(char *buf, int64_t v) |
78 | { | 86 | { |
79 | static const char units[] = "?kMGTPEZY"; | 87 | static const char units[] = "?kMGTPEZY"; |
80 | char dec[4] = ""; | 88 | int u = 0, t; |
81 | int u, t = 0; | 89 | |
82 | 90 | uint64_t q; | |
83 | for (u = 0; v >= 1024 || v <= -1024; u++) { | 91 | |
84 | t = v & ~(~0 << 10); | 92 | if (v < 0) |
85 | v >>= 10; | 93 | q = -v; |
86 | } | 94 | else |
87 | 95 | q = v; | |
88 | if (!u) | 96 | |
89 | return sprintf(buf, "%llu", v); | 97 | /* For as long as the number is more than 3 digits, but at least |
90 | 98 | * once, shift right / divide by 1024. Keep the remainder for | |
91 | if (v < 100 && v > -100) | 99 | * a digit after the decimal point. |
92 | snprintf(dec, sizeof(dec), ".%i", t / 100); | 100 | */ |
93 | 101 | do { | |
94 | return sprintf(buf, "%lli%s%c", v, dec, units[u]); | 102 | u++; |
103 | |||
104 | t = q & ~(~0 << 10); | ||
105 | q >>= 10; | ||
106 | } while (q >= 1000); | ||
107 | |||
108 | if (v < 0) | ||
109 | /* '-', up to 3 digits, '.', 1 digit, 1 character, null; | ||
110 | * yields 8 bytes. | ||
111 | */ | ||
112 | return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]); | ||
113 | else | ||
114 | return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]); | ||
95 | } | 115 | } |
96 | 116 | ||
97 | ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[], | 117 | ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[], |