Skip to contentSkip to conversation

procfs: don't need a PATH_MAX allocation to hold a string representation of an int

PATCH Superseded Jeff Layton 4 participants 7 messages Sep 7, 2012 – Sep 10, 2012

Subsystems (6)
Networking VFS NFS client NFS server +2 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 procfs: don't need a PATH_MAX allocation to hold a string representation of an int

---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/base.c +3 -2
index 1b6c84c..58e801b 100644
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = kmalloc(11, GFP_KERNEL);
 		if (!name)
 			name = ERR_PTR(-ENOMEM);
 		else
@@ -2773,7 +2774,7 @@ static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
 {
 	char *s = nd_get_link(nd);
 	if (!IS_ERR(s))
-		__putname(s);
+		kfree(s);
 }
 
 static const struct inode_operations proc_self_inode_operations = {
Signature · 1 line
1.7.11.4
#1 lore raw
Quoted 10 lines · On Fri, 7 Sep 2012 08:34:53 -0400 Jeff Layton <[email protected]> wrote:
---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/base.c +0 -0
index 1b6c84c..58e801b 100644

fs/proc/base.c static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) · line 2762

Quoted 6 lines
fs/proc/base.c +2 -1
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = 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" ?

Quoted 12 lines
fs/proc/base.c +1 -1
 		if (!name)
 			name = ERR_PTR(-ENOMEM);
 		else
@@ -2773,7 +2774,7 @@ static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
 {
 	char *s = nd_get_link(nd);
 	if (!IS_ERR(s))
-		__putname(s);
+		kfree(s);
 }
 
 static const struct inode_operations proc_self_inode_operations = {
Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw
Quoted 21 lines · Jeff Layton
Quoted 10 lines · On Fri, 7 Sep 2012 08:34:53 -0400 Jeff Layton<[email protected]> wrote:
---
  fs/proc/base.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/base.c +0 -0
index 1b6c84c..58e801b 100644

fs/proc/base.c static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) · line 2762

Quoted 6 lines
fs/proc/base.c +2 -1
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  	pid_t tgid = task_tgid_nr_ns(current, ns);
  	char *name = ERR_PTR(-ENOENT);
  	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = kmalloc(11, GFP_KERNEL);
		^^^^^
Quoted 3 lines

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" ?

See INT_BUFSIZE_BOUND() in:
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;hb=HEAD

cheers,
Pádraig.

#3 lore raw
Quoted 9 lines · Pádraig Brady
Quoted 8 lines · Jeff Layton
Quoted 5 lines · On Fri, 7 Sep 2012 08:34:53 -0400 Jeff Layton<[email protected]> wrote:
---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines
Quoted 13 lines

fs/proc/base.c static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) · line 2762

Quoted 11 lines
fs/proc/base.c +2 -1
index 1b6c84c..58e801b 100644
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = kmalloc(11, GFP_KERNEL);
		^^^^^
Quoted 3 lines

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" ?

It'd be nice to have something--I've run across similar mistakes recently.

Quoted 3 lines

See INT_BUFSIZE_BOUND() in:
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;hb=HEAD

My lame attempt follows. It's simpler than Pádraig's but possibly also stupider.

--b.

commit 15e8c46104e0c1dd6a76d09b55563b6f83b61667

Author: J. Bruce Fields <[email protected]>
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?

include/linux/string.h +6 -0
index ffe0442..3674cf5 100644
@@ -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 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);
net/sunrpc/cache.c +1 -1
index 2afd2a8..1dcd2b3 100644
@@ -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(unsigned long) + 2];
 	unsigned long p = *ppos;
 	size_t len;
 
#4 lore raw
Quoted 73 lines · On Fri, 7 Sep 2012 15:44:16 -0400 "J. Bruce Fields" <[email protected]>…
Quoted 9 lines · Pádraig Brady
Quoted 8 lines · Jeff Layton
Quoted 5 lines · On Fri, 7 Sep 2012 08:34:53 -0400 Jeff Layton<[email protected]> wrote:
---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines
Quoted 13 lines

fs/proc/base.c static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) · line 2762

Quoted 11 lines
fs/proc/base.c +2 -1
index 1b6c84c..58e801b 100644
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = kmalloc(11, GFP_KERNEL);
		^^^^^
Quoted 3 lines

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" ?

It'd be nice to have something--I've run across similar mistakes recently.

Quoted 3 lines

See INT_BUFSIZE_BOUND() in:
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;hb=HEAD

My lame attempt follows. It's simpler than Pádraig's but possibly also stupider.

--b.

commit 15e8c46104e0c1dd6a76d09b55563b6f83b61667

Author: J. Bruce Fields <[email protected]>
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?

include/linux/string.h +6 -0
index ffe0442..3674cf5 100644
@@ -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 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);
net/sunrpc/cache.c +0 -0
index 2afd2a8..1dcd2b3 100644

net/sunrpc/cache.c static ssize_t read_flush(struct file *file, char __user *buf, · line 1415

Quoted 8 lines
net/sunrpc/cache.c +1 -1
@@ -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(unsigned long) + 2];
 	unsigned long p = *ppos;
 	size_t len;
 

Thanks Bruce and Pádraig...

Bruce, are you planning to push that patch for 3.7? If so, I'll base mine on top of yours...

Signature · 5 lines
Jeff Layton <[email protected]>
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw
Quoted 87 lines · Jeff Layton
Quoted 73 lines · On Fri, 7 Sep 2012 15:44:16 -0400 "J. Bruce Fields" <[email protected]>…
Quoted 9 lines · Pádraig Brady
Quoted 8 lines · Jeff Layton
Quoted 5 lines · On Fri, 7 Sep 2012 08:34:53 -0400 Jeff Layton<[email protected]> wrote:
---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 16 lines
Quoted 13 lines

fs/proc/base.c static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) · line 2762

Quoted 11 lines
fs/proc/base.c +2 -1
index 1b6c84c..58e801b 100644
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 10 for max length of an int in decimal + NULL terminator */
+		name = kmalloc(11, GFP_KERNEL);
		^^^^^
Quoted 3 lines

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" ?

It'd be nice to have something--I've run across similar mistakes recently.

Quoted 3 lines

See INT_BUFSIZE_BOUND() in:
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;hb=HEAD

My lame attempt follows. It's simpler than Pádraig's but possibly also stupider.

--b.

commit 15e8c46104e0c1dd6a76d09b55563b6f83b61667

Author: J. Bruce Fields <[email protected]>
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?

include/linux/string.h +6 -0
index ffe0442..3674cf5 100644
@@ -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 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);
net/sunrpc/cache.c +0 -0
index 2afd2a8..1dcd2b3 100644

net/sunrpc/cache.c static ssize_t read_flush(struct file *file, char __user *buf, · line 1415

Quoted 8 lines
net/sunrpc/cache.c +1 -1
@@ -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(unsigned long) + 2];
 	unsigned long p = *ppos;
 	size_t len;
 

Thanks Bruce and Pádraig...

Quoted 2 lines

Bruce, are you planning to push that patch for 3.7? If so, I'll base mine on top of yours...

I assumed string.h is outside my baliwick--I'm not sure where it should go. Linus or Andrew?

--b.
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

PATCH v2 1/1 procfs: don't need a PATH_MAX allocation to hold a string representation of an int

In reply to Jeff Layton

This patch is a minor respin that just increases the buffer size by 1 to account for a potential minus sign at the head.

---
 fs/proc/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/base.c +3 -2
index 1b6c84c..3e6ce3f 100644
@@ -2758,7 +2758,8 @@ static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 	pid_t tgid = task_tgid_nr_ns(current, ns);
 	char *name = ERR_PTR(-ENOENT);
 	if (tgid) {
-		name = __getname();
+		/* 11 for max length of signed int in decimal + NULL term */
+		name = kmalloc(12, GFP_KERNEL);
 		if (!name)
 			name = ERR_PTR(-ENOMEM);
 		else
@@ -2773,7 +2774,7 @@ static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
 {
 	char *s = nd_get_link(nd);
 	if (!IS_ERR(s))
-		__putname(s);
+		kfree(s);
 }
 
 static const struct inode_operations proc_self_inode_operations = {
Signature · 1 line
1.7.11.4