2012-08-21 21:29:21

by J. Bruce Fields

[permalink] [raw]
Subject: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer

From: "J. Bruce Fields" <[email protected]>

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 <[email protected]>
---
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


2012-08-21 21:36:41

by Jim Rees

[permalink] [raw]
Subject: Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer

J. Bruce Fields wrote:

From: "J. Bruce Fields" <[email protected]>

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).

Please add Suggested-by: Jim Rees <[email protected]>. I'm thinking of
patenting the algorithm.

2012-08-21 22:06:19

by Al Viro

[permalink] [raw]
Subject: Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer

On Tue, Aug 21, 2012 at 05:22:27PM -0400, Jim Rees wrote:
> J. Bruce Fields wrote:
>
> From: "J. Bruce Fields" <[email protected]>
>
> 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).
>
> Please add Suggested-by: Jim Rees <[email protected]>. I'm thinking of
> patenting the algorithm.

Is that a joke? Patenting the fact that log10(256) is 2.408..., which
is about 2.4, which is 24/10? I really hope we are Poe'd... BTW, NAK
the comment - s/36/26/ in there; check it yourself -
$ echo '2^(8*27)' | bc
105312291668557186697918027683670432318895095400549111254310977536
which is 66-digit, not 65 as the estimate would be.

2012-08-21 22:19:14

by J. Bruce Fields

[permalink] [raw]
Subject: Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer

On Tue, Aug 21, 2012 at 11:06:13PM +0100, Al Viro wrote:
> On Tue, Aug 21, 2012 at 05:22:27PM -0400, Jim Rees wrote:
> > J. Bruce Fields wrote:
> >
> > From: "J. Bruce Fields" <[email protected]>
> >
> > 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).
> >
> > Please add Suggested-by: Jim Rees <[email protected]>. I'm thinking of
> > patenting the algorithm.
>
> Is that a joke? Patenting the fact that log10(256) is 2.408..., which
> is about 2.4, which is 24/10? I really hope we are Poe'd... BTW, NAK
> the comment - s/36/26/ in there; check it yourself -
> $ echo '2^(8*27)' | bc
> 105312291668557186697918027683670432318895095400549111254310977536
> which is 66-digit, not 65 as the estimate would be.

Erp, you're right.

Anyway, does something like base10len(type) seem reasonable? Or define
macros that enumerate the sizes? (ULONG_STR_MAX or something?)

--b.

2012-08-22 00:04:08

by Jim Rees

[permalink] [raw]
Subject: Re: [PATCH] strings: helper for maximum decimal encoding of an unsigned integer

Al Viro wrote:

On Tue, Aug 21, 2012 at 05:22:27PM -0400, Jim Rees wrote:
> J. Bruce Fields wrote:
>
> From: "J. Bruce Fields" <[email protected]>
>
> 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).
>
> Please add Suggested-by: Jim Rees <[email protected]>. I'm thinking of
> patenting the algorithm.

Is that a joke?

Yes, that's a joke. Sorry if it wasn't obvious.

I am, however offering up licenses at a cost of one pint of beer per
thousand digits converted.