All of lore.kernel.org
 help / color / mirror / Atom feed
From: Coly Li <colyli@suse.de>
To: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org,
	axboe@kernel.dk
Cc: bcache@lists.ewheeler.net, Michael Lyle <mlyle@lyle.org>,
	stable@vger.kernel.org
Subject: [PATCH 12/12] bcache: fix bch_hprint crash and improve output
Date: Wed,  6 Sep 2017 14:26:02 +0800	[thread overview]
Message-ID: <20170906062602.50497-13-colyli@suse.de> (raw)
In-Reply-To: <20170906062602.50497-1-colyli@suse.de>

From: Michael Lyle <mlyle@lyle.org>

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
---
 drivers/md/bcache/util.c | 50 +++++++++++++++++++++++++++++++++---------------
 1 file 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)
 STRTO_H(strtoll, long long)
 STRTO_H(strtoull, unsigned long long)
 
+/**
+ * bch_hprint() - formats @v to human readable string for sysfs.
+ *
+ * @v - signed 64 bit integer
+ * @buf - the (at least 8 byte) buffer to format the result into.
+ *
+ * Returns the number of bytes used by format.
+ */
 ssize_t bch_hprint(char *buf, int64_t v)
 {
 	static const char units[] = "?kMGTPEZY";
-	char dec[4] = "";
-	int u, t = 0;
-
-	for (u = 0; v >= 1024 || v <= -1024; u++) {
-		t = v & ~(~0 << 10);
-		v >>= 10;
-	}
-
-	if (!u)
-		return sprintf(buf, "%llu", v);
-
-	if (v < 100 && v > -100)
-		snprintf(dec, sizeof(dec), ".%i", t / 100);
-
-	return sprintf(buf, "%lli%s%c", v, dec, units[u]);
+	int u = 0, t;
+
+	uint64_t q;
+
+	if (v < 0)
+		q = -v;
+	else
+		q = v;
+
+	/* For as long as the number is more than 3 digits, but at least
+	 * once, shift right / divide by 1024.  Keep the remainder for
+	 * a digit after the decimal point.
+	 */
+	do {
+		u++;
+
+		t = q & ~(~0 << 10);
+		q >>= 10;
+	} while (q >= 1000);
+
+	if (v < 0)
+		/* '-', up to 3 digits, '.', 1 digit, 1 character, null;
+		 * yields 8 bytes.
+		 */
+		return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
+	else
+		return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
 }
 
 ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
-- 
2.13.5

  parent reply	other threads:[~2017-09-06  6:26 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-06  6:25 [PATCH 00/13] bcache: fixes and update for 4.14 Coly Li
2017-09-06  6:25 ` [PATCH 01/12] bcache: Fix leak of bdev reference Coly Li
2017-09-06  6:25 ` [PATCH 02/12] bcache: fix sequential large write IO bypass Coly Li
2017-09-06  6:25 ` [PATCH 03/12] bcache: do not subtract sectors_to_gc for bypassed IO Coly Li
2017-09-06  6:25 ` [PATCH 04/12] bcache: Don't reinvent the wheel but use existing llist API Coly Li
2017-09-26  4:38   ` Michael Lyle
2017-09-26  6:39     ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  6:39       ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  7:09       ` Coly Li
2017-09-26  7:15         ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  7:15           ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  7:22           ` Coly Li
2017-09-26  7:08     ` Coly Li
2017-09-26  7:16       ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  7:16         ` 박병철/선임연구원/SW Platform(연)AOT팀(byungchul.park@lge.com)
2017-09-26  7:24         ` Coly Li
2017-09-26  7:46     ` Coly Li
2017-09-26 19:55       ` Michael Lyle
2017-09-26 19:55         ` Michael Lyle
2017-09-06  6:25 ` [PATCH 05/12] bcache: gc does not work when triggering by manual command Coly Li
2017-09-06  6:25 ` [PATCH 06/12] bcache: correct cache_dirty_target in __update_writeback_rate() Coly Li
2017-09-06  6:25 ` [PATCH 07/12] bcache: Correct return value for sysfs attach errors Coly Li
2017-09-06  6:25 ` [PATCH 08/12] bcache: increase the number of open buckets Coly Li
2017-09-06  6:25   ` Coly Li
2017-09-06  6:25 ` [PATCH 09/12] bcache: fix for gc and write-back race Coly Li
2017-09-06  6:26 ` [PATCH 10/12] bcache: silence static checker warning Coly Li
2017-09-06  6:26 ` [PATCH 11/12] bcache: Update continue_at() documentation Coly Li
2017-09-06  6:26 ` Coly Li [this message]
2017-09-06 14:20 ` [PATCH 00/13] bcache: fixes and update for 4.14 Jens Axboe
2017-09-06 15:41   ` Coly Li
2017-09-06 15:46     ` Jens Axboe
2017-09-06 17:38       ` Coly Li
2017-09-07 18:51         ` Eddie Chapman
2017-09-07 19:31           ` Jens Axboe
2017-09-07 19:01         ` Eddie Chapman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170906062602.50497-13-colyli@suse.de \
    --to=colyli@suse.de \
    --cc=axboe@kernel.dk \
    --cc=bcache@lists.ewheeler.net \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=mlyle@lyle.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.