All of lore.kernel.org
 help / color / mirror / Atom feed
From: "J. Bruce Fields" <bfields@fieldses.org>
To: Jim Rees <rees@umich.edu>
Cc: Bernd Petrovitsch <bernd@petrovitsch.priv.at>,
	Jan Engelhardt <jengelh@inai.de>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer
Date: Fri, 14 Sep 2012 09:14:57 -0400	[thread overview]
Message-ID: <20120914131457.GA19666@fieldses.org> (raw)
In-Reply-To: <20120914123014.GB29160@umich.edu>

On Fri, Sep 14, 2012 at 08:30:14AM -0400, Jim Rees wrote:
> But I still like my way better.

Yeah.  It looks less mysterious once you realize it's just a
straightforward application of the usual coincidence

	2^10 = 1024 ~ 1000 = 10^3

How about this?  Also with some more defines because I like typing

	char buf[ULONG_STR_MAX + 1];

better than

	char bug[base10len(unsigned long) + 1];

--b.

commit 59a620640cd05d3d29e678ff893cfe266091fba7
Author: J. Bruce Fields <bfields@redhat.com>
Date:   Wed Aug 15 17:41:47 2012 -0400

    strings: helper for maximum decimal encoding of an unsigned integer
    
    I've seen a couple examples recently where we've gotten this wrong.
    Maybe something like this would help?
    
    Suggested-by: Jim Rees <rees@umich.edu>
    Signed-off-by: J. Bruce Fields <bfields@redhat.com>

diff --git a/include/linux/string.h b/include/linux/string.h
index ffe0442..38da7a0 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -126,6 +126,27 @@ extern void argv_free(char **argv);
 extern bool sysfs_streq(const char *s1, const char *s2);
 extern int strtobool(const char *s, bool *res);
 
+/*
+ * length of the decimal representation of an integer, not including any
+ * sign or null termination.  Just an approximation, but it's right for
+ * unsigned types of size 1 to 26 bytes:
+ */
+#define base10len(i) (sizeof(i) * 8 * 3 / 10 + 1)
+
+/* Actually a slight overestimate in the signed 64-bit case: */
+
+#define UCHAR_STR_MAX	base10len(char)
+#define CHAR_STR_MAX	base10len(char) + 1
+#define UINT_STR_MAX	base10len(int)
+#define INT_STR_MAX	base10len(int) + 1
+#define ULONG_STR_MAX	base10len(long)
+#define LONG_STR_MAX	base10len(long) + 1
+
+#define U32_STR_MAX	base10len(u32)
+#define S32_STR_MAX	base10len(u32) + 1
+#define U64_STR_MAX	base10len(u64)
+#define S64_STR_MAX	base10len(u64) + 1
+
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c
index 2afd2a8..d80d482 100644
--- a/net/sunrpc/cache.c
+++ b/net/sunrpc/cache.c
@@ -1409,7 +1409,7 @@ static ssize_t read_flush(struct file *file, char __user *buf,
 			  size_t count, loff_t *ppos,
 			  struct cache_detail *cd)
 {
-	char tbuf[20];
+	char tbuf[ULONG_STR_MAX + 1];
 	unsigned long p = *ppos;
 	size_t len;
 

  reply	other threads:[~2012-09-14 13:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-21 21:29 [PATCH] strings: helper for maximum decimal encoding of an unsigned integer J. Bruce Fields
2012-08-21 21:22 ` Jim Rees
2012-08-21 22:06   ` Al Viro
2012-08-21 22:19     ` J. Bruce Fields
2012-08-22  0:03     ` Jim Rees
2012-09-10  6:19 ` Jan Engelhardt
2012-09-14  9:17   ` Bernd Petrovitsch
2012-09-14 12:30     ` Jim Rees
2012-09-14 13:14       ` J. Bruce Fields [this message]
2012-09-14 13:18       ` Bernd Petrovitsch
2012-09-14 13:51         ` Jim Rees
2012-09-14 13:37       ` Jan Engelhardt
2012-09-14 13:54         ` Jim Rees
2012-09-14 12:59     ` Jan Engelhardt
2012-09-14 13:46       ` Jim Rees
2012-09-14 14:25         ` Jan Engelhardt
2012-09-14 15:00           ` Bernd Petrovitsch

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=20120914131457.GA19666@fieldses.org \
    --to=bfields@fieldses.org \
    --cc=bernd@petrovitsch.priv.at \
    --cc=jengelh@inai.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rees@umich.edu \
    /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.