Skip to contentSkip to conversation

sunrpc/cache: convert to use string_escape_str()

PATCH In review Andy Shevchenko 2 participants 4 messages Nov 28, 2014 – Dec 9, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sunrpc/cache: convert to use string_escape_str()

There is nice kernel helper to escape a given strings by provided rules. Let's use it instead of custom approach.

---
 net/sunrpc/cache.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
net/sunrpc/cache.c +6 -21
index 0663621..5cf60a4 100644
@@ -20,6 +20,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
+#include <linux/string_helpers.h>
 #include <asm/uaccess.h>
 #include <linux/poll.h>
 #include <linux/seq_file.h>
@@ -1067,32 +1068,16 @@ void qword_add(char **bpp, int *lp, char *str)
 {
 	char *bp = *bpp;
 	int len = *lp;
-	char c;
+	int ret;
 
 	if (len < 0) return;
 
-	while ((c=*str++) && len)
-		switch(c) {
-		case ' ':
-		case '\t':
-		case '\n':
-		case '\\':
-			if (len >= 4) {
-				*bp++ = '\\';
-				*bp++ = '0' + ((c & 0300)>>6);
-				*bp++ = '0' + ((c & 0070)>>3);
-				*bp++ = '0' + ((c & 0007)>>0);
-			}
-			len -= 4;
-			break;
-		default:
-			*bp++ = c;
-			len--;
-		}
-	if (c || len <1) len = -1;
+	ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t");
+	if (ret < 0 || ret == len)
+		len = -1;
 	else {
 		*bp++ = ' ';
-		len--;
+		len -= ret - 1;
 	}
 	*bpp = bp;
 	*lp = len;
Signature · 1 line
2.1.3
#1 lore raw
Quoted 2 lines · Andy Shevchenko

There is nice kernel helper to escape a given strings by provided rules. Let's use it instead of custom approach.

Looks good, but it broke nfsd.

After staring at the patch for a while:

Quoted 42 lines
---
 net/sunrpc/cache.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
net/sunrpc/cache.c +2 -17
index 0663621..5cf60a4 100644
@@ -20,6 +20,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
+#include <linux/string_helpers.h>
 #include <asm/uaccess.h>
 #include <linux/poll.h>
 #include <linux/seq_file.h>
@@ -1067,32 +1068,16 @@ void qword_add(char **bpp, int *lp, char *str)
 {
 	char *bp = *bpp;
 	int len = *lp;
-	char c;
+	int ret;
 
 	if (len < 0) return;
 
-	while ((c=*str++) && len)
-		switch(c) {
-		case ' ':
-		case '\t':
-		case '\n':
-		case '\\':
-			if (len >= 4) {
-				*bp++ = '\\';
-				*bp++ = '0' + ((c & 0300)>>6);
-				*bp++ = '0' + ((c & 0070)>>3);
-				*bp++ = '0' + ((c & 0007)>>0);
-			}
-			len -= 4;
-			break;
-		default:
-			*bp++ = c;

net/sunrpc/cache.c void qword_add(char **bpp, int *lp, char *str) · line 1080

Quoted 10 lines
net/sunrpc/cache.c +4 -4
-			len--;
-		}
-	if (c || len <1) len = -1;
+	ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t");
+	if (ret < 0 || ret == len)
+		len = -1;
 	else {
 		*bp++ = ' ';
-		len--;
+		len -= ret - 1;

Looks like that should be ret + 1, not ret - 1. With that change, things work.

Inclined to actually commit that as:

	len -= ret;
	*bp++ = ' ';
	len--;

just to make the arithmetic really obvious.

--b.

Quoted 6 lines
net/sunrpc/cache.c +0 -0
 	}
 	*bpp = bp;
 	*lp = len;
Signature · 1 line
2.1.3
#2 lore raw
Quoted 66 lines · J. Bruce Fields
Quoted 2 lines · Andy Shevchenko

There is nice kernel helper to escape a given strings by provided rules. Let's use it instead of custom approach.

Looks good, but it broke nfsd.

After staring at the patch for a while:

Quoted 42 lines
---
 net/sunrpc/cache.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
net/sunrpc/cache.c +2 -17
index 0663621..5cf60a4 100644
@@ -20,6 +20,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
+#include <linux/string_helpers.h>
 #include <asm/uaccess.h>
 #include <linux/poll.h>
 #include <linux/seq_file.h>
@@ -1067,32 +1068,16 @@ void qword_add(char **bpp, int *lp, char *str)
 {
 	char *bp = *bpp;
 	int len = *lp;
-	char c;
+	int ret;
 
 	if (len < 0) return;
 
-	while ((c=*str++) && len)
-		switch(c) {
-		case ' ':
-		case '\t':
-		case '\n':
-		case '\\':
-			if (len >= 4) {
-				*bp++ = '\\';
-				*bp++ = '0' + ((c & 0300)>>6);
-				*bp++ = '0' + ((c & 0070)>>3);
-				*bp++ = '0' + ((c & 0007)>>0);
-			}
-			len -= 4;
-			break;
-		default:
-			*bp++ = c;

net/sunrpc/cache.c void qword_add(char **bpp, int *lp, char *str) · line 1080

Quoted 10 lines
net/sunrpc/cache.c +4 -4
-			len--;
-		}
-	if (c || len <1) len = -1;
+	ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t");
+	if (ret < 0 || ret == len)
+		len = -1;
 	else {
 		*bp++ = ' ';
-		len--;
+		len -= ret - 1;

Looks like that should be ret + 1, not ret - 1. With that change, things work.

Inclined to actually commit that as:

Quoted 7 lines
	len -= ret;
	*bp++ = ' ';
	len--;

just to make the arithmetic really obvious.

--b.

Good catch, thanks! It should decrement length indeed. In the form of -= it must be + 1. Shall I resubmit patch? If so, can I include your tag (Tested-by I suppose) ?

Quoted 7 lines
Quoted 7 lines
net/sunrpc/cache.c +0 -0
 	}
 	*bpp = bp;
 	*lp = len;
Signature · 1 line
2.1.3
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#3 lore raw
Quoted 71 lines · Andy Shevchenko
Quoted 70 lines · J. Bruce Fields
Quoted 2 lines · Andy Shevchenko

There is nice kernel helper to escape a given strings by provided rules. Let's use it instead of custom approach.

Looks good, but it broke nfsd.

After staring at the patch for a while:

Quoted 42 lines
---
 net/sunrpc/cache.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
net/sunrpc/cache.c +2 -17
index 0663621..5cf60a4 100644
@@ -20,6 +20,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
+#include <linux/string_helpers.h>
 #include <asm/uaccess.h>
 #include <linux/poll.h>
 #include <linux/seq_file.h>
@@ -1067,32 +1068,16 @@ void qword_add(char **bpp, int *lp, char *str)
 {
 	char *bp = *bpp;
 	int len = *lp;
-	char c;
+	int ret;
 
 	if (len < 0) return;
 
-	while ((c=*str++) && len)
-		switch(c) {
-		case ' ':
-		case '\t':
-		case '\n':
-		case '\\':
-			if (len >= 4) {
-				*bp++ = '\\';
-				*bp++ = '0' + ((c & 0300)>>6);
-				*bp++ = '0' + ((c & 0070)>>3);
-				*bp++ = '0' + ((c & 0007)>>0);
-			}
-			len -= 4;
-			break;
-		default:
-			*bp++ = c;

net/sunrpc/cache.c void qword_add(char **bpp, int *lp, char *str) · line 1080

Quoted 10 lines
net/sunrpc/cache.c +4 -4
-			len--;
-		}
-	if (c || len <1) len = -1;
+	ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t");
+	if (ret < 0 || ret == len)
+		len = -1;
 	else {
 		*bp++ = ' ';
-		len--;
+		len -= ret - 1;

Looks like that should be ret + 1, not ret - 1. With that change, things work.

Inclined to actually commit that as:

	len -= ret;
	*bp++ = ' ';
	len--;
Quoted 7 lines
Quoted 3 lines

just to make the arithmetic really obvious.

--b.

Good catch, thanks! It should decrement length indeed. In the form of -= it must be + 1. Shall I resubmit patch? If so, can I include your tag (Tested-by I suppose) ?

I'll fix it up, thanks.

--b.