linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] strings: helper for maximum decimal encoding of an unsigned integer
@ 2012-08-21 21:29 J. Bruce Fields
  2012-08-21 21:22 ` Jim Rees
  2012-09-10  6:19 ` Jan Engelhardt
  0 siblings, 2 replies; 17+ messages in thread
From: J. Bruce Fields @ 2012-08-21 21:29 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jim Rees

From: "J. Bruce Fields" <bfields@redhat.com>

I've seen a couple examples recently where we've gotten this wrong.
Maybe something like this would help?  Is there some better way?

(Approximation due to Jim Rees).

Signed-off-by: J. Bruce Fields <bfields@redhat.com>
---
 include/linux/string.h |    6 ++++++
 net/sunrpc/cache.c     |    2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index ffe0442..d4809b7 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -126,6 +126,12 @@ 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 unsigned integer.  Just an
+ * approximation, but it's right for types of size 1 to 36 bytes:
+ */
+#define base10len(i) (sizeof(i) * 24 / 10 + 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..1dcd2b3 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[base10len(long) + 2];
 	unsigned long p = *ppos;
 	size_t len;
 
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2012-09-14 15:02 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).