From: Jeff Layton Subject: Re: [PATCH] procfs: don't need a PATH_MAX allocation to hold a string representation of an int Date: Mon, 10 Sep 2012 09:23:28 -0400 Message-ID: <20120910092328.0251144c@corrin.poochiereds.net> References: <1347021293-19052-1-git-send-email-jlayton@redhat.com> <20120907084814.07a59c98@corrin.poochiereds.net> <5049EF5B.3020607@draigBrady.com> <20120907194416.GB4633@fieldses.org> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: =?ISO-8859-1?B?UOFkcmFpZw==?= Brady , viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org To: "J. Bruce Fields" Return-path: Received: from mx1.redhat.com ([209.132.183.28]:50484 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752642Ab2IJNXg convert rfc822-to-8bit (ORCPT ); Mon, 10 Sep 2012 09:23:36 -0400 In-Reply-To: <20120907194416.GB4633@fieldses.org> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On Fri, 7 Sep 2012 15:44:16 -0400 "J. Bruce Fields" wrote: > On Fri, Sep 07, 2012 at 01:58:03PM +0100, P=E1draig Brady wrote: > > On 09/07/2012 01:48 PM, Jeff Layton wrote: > > >On Fri, 7 Sep 2012 08:34:53 -0400 > > >Jeff Layton wrote: > > > > > >>Signed-off-by: Jeff Layton > > >>--- > > >> fs/proc/base.c | 5 +++-- > > >> 1 file changed, 3 insertions(+), 2 deletions(-) > > >> > > >>diff --git a/fs/proc/base.c b/fs/proc/base.c > > >>index 1b6c84c..58e801b 100644 > > >>--- a/fs/proc/base.c > > >>+++ b/fs/proc/base.c > > >>@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct d= entry *dentry, struct nameidata *nd) > > >> pid_t tgid =3D task_tgid_nr_ns(current, ns); > > >> char *name =3D ERR_PTR(-ENOENT); > > >> if (tgid) { > > >>- name =3D __getname(); > > >>+ /* 10 for max length of an int in decimal + NULL terminator */ > > >>+ name =3D kmalloc(11, GFP_KERNEL); > > > > > > ^^^^^ > > >Bah...my mistake. This should be "12", since it's possible (though > > >unlikely) that this value could be negative. Is there a better way= to > > >express "strlen of max representation of an int in decimal" ? >=20 > It'd be nice to have something--I've run across similar mistakes > recently. >=20 > >=20 > > See INT_BUFSIZE_BOUND() in: > > http://git.sv.gnu.org/gitweb/?p=3Dgnulib.git;a=3Dblob;f=3Dlib/intpr= ops.h;hb=3DHEAD >=20 > My lame attempt follows. It's simpler than P=E1draig's but possibly = also > stupider. >=20 > --b. >=20 > commit 15e8c46104e0c1dd6a76d09b55563b6f83b61667 > Author: J. Bruce Fields > Date: Wed Aug 15 17:41:47 2012 -0400 >=20 > strings: helper for maximum decimal encoding of an unsigned integ= er > =20 > I've seen a couple examples recently where we've gotten this wron= g. > Maybe something like this would help? > =20 > Suggested-by: Jim Rees > Signed-off-by: J. Bruce Fields >=20 > diff --git a/include/linux/string.h b/include/linux/string.h > index ffe0442..3674cf5 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); > =20 > +/* > + * length of the decimal representation of an unsigned integer. Jus= t an > + * approximation, but it's right for types of size 1 to 26 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, ch= ar __user *buf, > size_t count, loff_t *ppos, > struct cache_detail *cd) > { > - char tbuf[20]; > + char tbuf[base10len(unsigned long) + 2]; > unsigned long p =3D *ppos; > size_t len; > =20 Thanks Bruce and P=E1draig... Bruce, are you planning to push that patch for 3.7? If so, I'll base mine on top of yours... --=20 Jeff Layton -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel= " in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html