Skip to contentSkip to conversation

lib / string_helpers: introduce string_escape_mem

PATCH In review Andy Shevchenko 3 participants 17 messages Jul 2, 2014 – Jul 3, 2014

Subsystems (4)
lore
Share & tools
lore
#4 lore raw

PATCH 2/6 lib / string_helpers: introduce string_escape_mem()

In reply to Andy Shevchenko

This is almost the opposite function to string_unescape(). Nevertheless it handles \0 and could be used for any byte buffer.

The documentation is supplied together with the function prototype.

The test cases covers most of the scenarios and would be expanded later on.

---
 include/linux/string_helpers.h |  83 ++++++++++++++++
 lib/string_helpers.c           | 215 +++++++++++++++++++++++++++++++++++++++++
 lib/test-string_helpers.c      | 213 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 511 insertions(+)
include/linux/string_helpers.h +83 -0
index 3eeee96..db13f20 100644
@@ -71,4 +71,87 @@ static inline int string_unescape_any_inplace(char *buf)
 	return string_unescape_any(buf, buf, 0);
 }
 
+#define ESCAPE_SPACE		0x01
+#define ESCAPE_SPECIAL		0x02
+#define ESCAPE_NULL		0x04
+#define ESCAPE_OCTAL		0x08
+#define ESCAPE_ANY		\
+	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
+#define ESCAPE_NP		0x10
+#define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
+#define ESCAPE_HEX		0x20
+
+/**
+ * string_escape_mem - quote characters in the given memory buffer
+ * @src:	source buffer (unescaped)
+ * @isz:	source buffer size
+ * @dst:	destination buffer (escaped)
+ * @osz:	destination buffer size
+ * @flags:	combination of the flags (bitwise OR):
+ *	%ESCAPE_SPACE:
+ *		'\f' - form feed
+ *		'\n' - new line
+ *		'\r' - carriage return
+ *		'\t' - horizontal tab
+ *		'\v' - vertical tab
+ *	%ESCAPE_SPECIAL:
+ *		'\\' - backslash
+ *		'\a' - alert (BEL)
+ *		'\e' - escape
+ *	%ESCAPE_NULL:
+ *		'\0' - null
+ *	%ESCAPE_OCTAL:
+ *		'\NNN' - byte with octal value NNN (3 digits)
+ *	%ESCAPE_ANY:
+ *		all previous together
+ *	%ESCAPE_NP:
+ *		escape only non-printable characters (checked by isprint)
+ *	%ESCAPE_ANY_NP:
+ *		all previous together
+ *	%ESCAPE_HEX:
+ *		'\xHH' - byte with hexadecimal value HH (2 digits)
+ * @esc:	NULL-terminated string of characters any of which, if found in
+ *		the source, has to be escaped
+ *
+ * Description:
+ * The process of escaping byte buffer includes several parts. They are applied
+ * in the following sequence.
+ *	1. The character is matched to the printable class, if asked, and in
+ *	   case of match it passes through to the output.
+ *	2. The character is not matched to the one from @esc string and thus
+ *	   must go as is to the output.
+ *	3. The character is checked if it falls into the class given by @flags.
+ *	   %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
+ *	   character. Note that they actually can't go together, otherwise
+ *	   %ESCAPE_HEX will be ignored.
+ *
+ * Caller must provide valid source and destination pointers. Be aware that
+ * destination buffer will not be NULL-terminated, thus caller have to append
+ * it if needs.
+ *
+ * Return: amount of characters processed to the destination buffer, or
+ * 	   %-ENOMEM if the size of buffer is not enough to put an escaped
+ * 	   character.
+ */
+int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
+		unsigned int flags, const char *esc);
+
+static inline int string_escape_mem_any_np(const char *src, size_t isz,
+		char *dst, size_t osz, const char *esc)
+{
+	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, esc);
+}
+
+static inline int string_escape_str(const char *src, char *dst, size_t sz,
+		unsigned int flags, const char *esc)
+{
+	return string_escape_mem(src, strlen(src), dst, sz, flags, esc);
+}
+
+static inline int string_escape_str_any_np(const char *src, char *dst,
+		size_t sz, const char *esc)
+{
+	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, esc);
+}
+
 #endif
lib/string_helpers.c +215 -0

224 lines not on this page · raw message

lib/test-string_helpers.c +213 -0

225 lines not on this page · raw message

Signature · 1 line
2.0.0
#7 lore raw

PATCH 3/6 lib80211: re-use string_escape_mem_any_np()

In reply to Andy Shevchenko

In kernel we have function to escape a given string. Let's use it instead of custom approach.

This fixes a bug. The current implementation wrongly prints octal numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

Additionally the \f, \v, \a, and \e are escaped to their alphabetic representation. It's safe to do since the print_ssid() is currently used for messaging only.

---
 net/wireless/lib80211.c | 24 ++----------------------
 1 file changed, 2 insertions(+), 22 deletions(-)
net/wireless/lib80211.c +2 -22
index a55c27b..0c2f67b 100644
@@ -22,6 +22,7 @@
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 
 #include <net/lib80211.h>
 
@@ -48,31 +49,10 @@ static void lib80211_crypt_deinit_handler(unsigned long data);
 
 const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
 {
-	const char *s = ssid;
 	char *d = buf;
 
 	ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
-	while (ssid_len--) {
-		if (isprint(*s)) {
-			*d++ = *s++;
-			continue;
-		}
-
-		*d++ = '\\';
-		if (*s == '\0')
-			*d++ = '0';
-		else if (*s == '\n')
-			*d++ = 'n';
-		else if (*s == '\r')
-			*d++ = 'r';
-		else if (*s == '\t')
-			*d++ = 't';
-		else if (*s == '\\')
-			*d++ = '\\';
-		else
-			d += snprintf(d, 3, "%03o", *s);
-		s++;
-	}
+	d += string_escape_mem_any_np(ssid, ssid_len, buf, ~0UL, NULL);
 	*d = '\0';
 	return buf;
 }
Signature · 1 line
2.0.0
#1 lore raw

PATCH 1/6 lib / string_helpers: clean up test suite

In reply to Andy Shevchenko

This patch prepares test suite for a following update. It introduces test_string_check_buf() helper which checks the result and dumps an error.

---
 lib/test-string_helpers.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
lib/test-string_helpers.c +27 -12
index 6ac48de..ea86e02 100644
@@ -10,6 +10,26 @@
 #include <linux/string.h>
 #include <linux/string_helpers.h>
 
+static __init bool test_string_check_buf(const char *name, unsigned int flags,
+					 char *in, size_t p,
+					 char *out_real, size_t q_real,
+					 char *out_test, size_t q_test)
+{
+	if (q_real == q_test && !memcmp(out_test, out_real, q_test))
+		return true;
+
+	pr_err("Test '%s' failed: flags = %u\n", name, flags);
+
+	print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
+		       in, p, true);
+	print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_test, q_test, true);
+	print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_real, q_real, true);
+
+	return false;
+}
+
 struct test_string {
 	const char *in;
 	const char *out;
@@ -39,7 +59,8 @@ static const struct test_string strings[] __initconst = {
 	},
 };
 
-static void __init test_string_unescape(unsigned int flags, bool inplace)
+static void __init test_string_unescape(const char *name, unsigned int flags,
+					bool inplace)
 {
 	char in[256];
 	char out_test[256];
@@ -77,15 +98,8 @@ static void __init test_string_unescape(unsigned int flags, bool inplace)
 		q_real = string_unescape(in, out_real, q_real, flags);
 	}
 
-	if (q_real != q_test || memcmp(out_test, out_real, q_test)) {
-		pr_warn("Test failed: flags = %u\n", flags);
-		print_hex_dump(KERN_WARNING, "Input: ",
-			       DUMP_PREFIX_NONE, 16, 1, in, p - 1, true);
-		print_hex_dump(KERN_WARNING, "Expected: ",
-			       DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true);
-		print_hex_dump(KERN_WARNING, "Got: ",
-			       DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true);
-	}
+	test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
+			      out_test, q_test);
 }
 
 static int __init test_string_helpers_init(void)
@@ -94,8 +108,9 @@ static int __init test_string_helpers_init(void)
 
 	pr_info("Running tests...\n");
 	for (i = 0; i < UNESCAPE_ANY + 1; i++)
-		test_string_unescape(i, false);
-	test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true);
+		test_string_unescape("unescape", i, false);
+	test_string_unescape("unescape inplace",
+			     get_random_int() % (UNESCAPE_ANY + 1), true);
 
 	return -EINVAL;
 }
Signature · 1 line
2.0.0
#13 lore raw

PATCH 5/6 staging: rtl8192e: re-use string_escape_mem()

In reply to Andy Shevchenko

Let's use kernel's library function to escape a buffer.

---
 drivers/staging/rtl8192e/rtllib.h | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/rtl8192e/rtllib.h +3 -10
index 83f5f57..cb99160 100644
@@ -31,6 +31,7 @@
 #include <linux/timer.h>
 #include <linux/sched.h>
 #include <linux/semaphore.h>
+#include <linux/string_helpers.h>
 
 #include <linux/delay.h>
 #include <linux/wireless.h>
@@ -2956,7 +2957,6 @@ extern inline int rtllib_get_scans(struct rtllib_device *ieee)
 static inline const char *escape_essid(const char *essid, u8 essid_len)
 {
 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
-	const char *s = essid;
 	char *d = escaped;
 
 	if (rtllib_is_empty_essid(essid, essid_len)) {
@@ -2965,15 +2965,8 @@ static inline const char *escape_essid(const char *essid, u8 essid_len)
 	}
 
 	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
+	d += string_escape_mem(essid, essid_len, escaped, sizeof(escaped) - 1,
+			       ESCAPE_NULL, NULL);
 	*d = '\0';
 	return escaped;
 }
Signature · 1 line
2.0.0
#0 lore raw

cover lib / string_helpers: introduce string_escape_mem

The introduced function is a kind of opposite to string_unescape. We have several users of such functionality each of them created custom implementation. The series contains clean up of test suite, adding new call, and switching few users to use it.

Test suite covers all of existing and most of potential use cases.

Andy Shevchenko (6):
lib / string_helpers: clean up test suite lib / string_helpers: introduce string_escape_mem()
lib80211: re-use string_escape_mem_any_np()
staging: wlan-ng: re-use string_escape_mem()
staging: rtl8192e: re-use string_escape_mem()
staging: rtl8192u: re-use string_escape_mem()

 drivers/staging/rtl8192e/rtllib.h              |  13 +-
 drivers/staging/rtl8192u/ieee80211/ieee80211.h |  13 +-
 drivers/staging/wlan-ng/prism2sta.c            |  29 +--
 include/linux/string_helpers.h                 |  83 ++++++++
 lib/string_helpers.c                           | 215 +++++++++++++++++++++
 lib/test-string_helpers.c                      | 250 +++++++++++++++++++++++--
 net/wireless/lib80211.c                        |  24 +--
 7 files changed, 551 insertions(+), 76 deletions(-)
Signature · 1 line
2.0.0
#12 lore raw

PATCH 4/6 staging: wlan-ng: re-use string_escape_mem()

This is a generic function to escape strings by given criteria. Let's use it instead of custom approach.

---
 drivers/staging/wlan-ng/prism2sta.c | 29 ++++++-----------------------
 1 file changed, 6 insertions(+), 23 deletions(-)
drivers/staging/wlan-ng/prism2sta.c +6 -23
index 209e4db..1e44c09 100644
@@ -61,6 +61,7 @@
 #include <linux/workqueue.h>
 #include <linux/byteorder/generic.h>
 #include <linux/ctype.h>
+#include <linux/string_helpers.h>
 
 #include <linux/io.h>
 #include <linux/delay.h>
@@ -81,27 +82,6 @@
 #include "hfa384x.h"
 #include "prism2mgmt.h"
 
-/* Create a string of printable chars from something that might not be */
-/* It's recommended that the str be 4*len + 1 bytes long */
-#define wlan_mkprintstr(buf, buflen, str, strlen) \
-{ \
-	int i = 0; \
-	int j = 0; \
-	memset(str, 0, (strlen)); \
-	for (i = 0; i < (buflen); i++) { \
-		if (isprint((buf)[i])) { \
-			(str)[j] = (buf)[i]; \
-			j++; \
-		} else { \
-			(str)[j] = '\\'; \
-			(str)[j+1] = 'x'; \
-			(str)[j+2] = hex_asc_hi((buf)[i]); \
-			(str)[j+3] = hex_asc_lo((buf)[i]); \
-			j += 4; \
-		} \
-	} \
-}
-
 static char *dev_info = "prism2_usb";
 static wlandevice_t *create_wlan(void);
 
@@ -862,8 +842,11 @@ static int prism2sta_getcardinfo(wlandevice_t *wlandev)
 	result = hfa384x_drvr_getconfig(hw, HFA384x_RID_NICSERIALNUMBER,
 					snum, HFA384x_RID_NICSERIALNUMBER_LEN);
 	if (!result) {
-		wlan_mkprintstr(snum, HFA384x_RID_NICSERIALNUMBER_LEN,
-				pstr, sizeof(pstr));
+		result = string_escape_mem(snum,
+				HFA384x_RID_NICSERIALNUMBER_LEN,
+				pstr, sizeof(pstr), ESCAPE_HEX | ESCAPE_NP,
+				NULL);
+		pstr[result] = '\0';
 		netdev_info(wlandev->netdev, "Prism2 card SN: %s\n", pstr);
 	} else {
 		netdev_err(wlandev->netdev, "Failed to retrieve Prism2 Card SN\n");
Signature · 1 line
2.0.0
#16 lore raw

PATCH 6/6 staging: rtl8192u: re-use string_escape_mem()

In reply to Andy Shevchenko

Let's use kernel's library function to escape a buffer instead of custom code.

---
 drivers/staging/rtl8192u/ieee80211/ieee80211.h | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/rtl8192u/ieee80211/ieee80211.h +3 -10
index 1040bab..bb5f604 100644
@@ -31,6 +31,7 @@
 #include <linux/sched.h>
 #include <linux/semaphore.h>
 #include <linux/interrupt.h>
+#include <linux/string_helpers.h>
 
 #include <linux/delay.h>
 #include <linux/wireless.h>
@@ -2576,7 +2577,6 @@ static inline int ieee80211_get_scans(struct ieee80211_device *ieee)
 
 static inline const char *escape_essid(const char *essid, u8 essid_len) {
 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
-	const char *s = essid;
 	char *d = escaped;
 
 	if (ieee80211_is_empty_essid(essid, essid_len)) {
@@ -2585,15 +2585,8 @@ static inline const char *escape_essid(const char *essid, u8 essid_len) {
 	}
 
 	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
+	d += string_escape_mem(essid, essid_len, escaped, sizeof(escaped) - 1,
+			       ESCAPE_NULL, NULL);
 	*d = '\0';
 	return escaped;
 }
Signature · 1 line
2.0.0
#14 lore raw

Re: [PATCH 5/6] staging: rtl8192e: re-use string_escape_mem()

In reply to Andy Shevchenko
Quoted 1 line · Andy Shevchenko

Let's use kernel's library function to escape a buffer.

[]

drivers/staging/rtl8192e/rtllib.h

Quoted 1 line
drivers/staging/rtl8192e/rtllib.h +0 -0

[]

Quoted 13 lines
drivers/staging/rtl8192e/rtllib.h +0 -2
@@ -2956,7 +2957,6 @@ extern inline int rtllib_get_scans(struct rtllib_device *ieee)
 static inline const char *escape_essid(const char *essid, u8 essid_len)
 {
 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
-	const char *s = essid;
 	char *d = escaped;
 
 	if (rtllib_is_empty_essid(essid, essid_len)) {
@@ -2965,15 +2965,8 @@ static inline const char *escape_essid(const char *essid, u8 essid_len)
 	}
 
 	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2969

Quoted 10 lines
drivers/staging/rtl8192e/rtllib.h +2 -8
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
+	d += string_escape_mem(essid, essid_len, escaped, sizeof(escaped) - 1,
+			       ESCAPE_NULL, NULL);

I'd've probably used

	d += string_escape_mem(essid, essid_len, d, ...

or
d = escaped + string_escap_mem(essid, essid_len, escaped, ...

so there's some relation between the thing being added to

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2970

Quoted 1 line
drivers/staging/rtl8192e/rtllib.h +0 -0
 	*d = '\0';

or maybe not used d at all with

	escaped[1 + string_escape_mem(etc...)] = 0;

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2972

Quoted 2 lines
drivers/staging/rtl8192e/rtllib.h +0 -0
 	return escaped;
 }

Unrelated but this isn't a thread safe or multiple instance safe function.

It seems it's used only in debugging message output though.

#8 lore raw

Re: [PATCH 3/6] lib80211: re-use string_escape_mem_any_np()

In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

In kernel we have function to escape a given string. Let's use it instead of custom approach.

This fixes a bug. The current implementation wrongly prints octal numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

[]

net/wireless/lib80211.c

Quoted 1 line
net/wireless/lib80211.c +0 -0

[]

Quoted 23 lines
net/wireless/lib80211.c +0 -16
@@ -48,31 +49,10 @@ static void lib80211_crypt_deinit_handler(unsigned long data);
 
 const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
 {
-	const char *s = ssid;
 	char *d = buf;
 
 	ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
-	while (ssid_len--) {
-		if (isprint(*s)) {
-			*d++ = *s++;
-			continue;
-		}
-
-		*d++ = '\\';
-		if (*s == '\0')
-			*d++ = '0';
-		else if (*s == '\n')
-			*d++ = 'n';
-		else if (*s == '\r')
-			*d++ = 'r';
-		else if (*s == '\t')
-			*d++ = 't';

net/wireless/lib80211.c static void lib80211_crypt_deinit_handler(unsigned long data); · line 58

Quoted 10 lines
net/wireless/lib80211.c +1 -6
-		else if (*s == '\\')
-			*d++ = '\\';
-		else
-			d += snprintf(d, 3, "%03o", *s);
-		s++;
-	}
+	d += string_escape_mem_any_np(ssid, ssid_len, buf, ~0UL, NULL);
 	*d = '\0';
 	return buf;
 }

This code looks like it was adapted from the old print_mac ethernet code that was eventually replaced by a vsprintf pointer extension %pM

So a better way to do this might be to add and use yet another vsprintf %p<foo> extension for ssids.

#9 lore raw

Re: [PATCH 3/6] lib80211: re-use string_escape_mem_any_np()

Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko

In kernel we have function to escape a given string. Let's use it instead of custom approach.

This fixes a bug. The current implementation wrongly prints octal numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

[]

Quoted 6 lines

This code looks like it was adapted from the old print_mac ethernet code that was eventually replaced by a vsprintf pointer extension %pM

So a better way to do this might be to add and use yet another vsprintf %p<foo> extension for ssids.

Might be, but it
- doesn't reduce necessity of string_escape_mem (not only ssid are
escaped in kernel)
- prevents user to choose a rule what exactly their would like to
escape (look at the other patches against ssid escaping)

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#15 lore raw

Re: [PATCH 5/6] staging: rtl8192e: re-use string_escape_mem()

In reply to Joe Perches
Quoted 2 lines · Joe Perches
Quoted 1 line · Andy Shevchenko

Let's use kernel's library function to escape a buffer.

[]

Quoted 22 lines
Quoted 5 lines
drivers/staging/rtl8192e/rtllib.h +0 -1
@@ -2965,15 +2965,8 @@ static inline const char *escape_essid(const char *essid, u8 essid_len)
 	}
 
 	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2969

Quoted 10 lines
drivers/staging/rtl8192e/rtllib.h +2 -8
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
+	d += string_escape_mem(essid, essid_len, escaped, sizeof(escaped) - 1,
+			       ESCAPE_NULL, NULL);

I'd've probably used

	d += string_escape_mem(essid, essid_len, d, ...

or
d = escaped + string_escap_mem(essid, essid_len, escaped, ...

Quoted 7 lines

so there's some relation between the thing being added to

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2970

Quoted 1 line
drivers/staging/rtl8192e/rtllib.h +0 -0
 	*d = '\0';

or maybe not used d at all with

	escaped[1 + string_escape_mem(etc...)] = 0;

Perhaps without '1 + ' part. I could update this as well if someone insists.

Quoted 6 lines

drivers/staging/rtl8192e/rtllib.h static inline const char *escape_essid(const char *essid, u8 essid_len) · line 2972

Quoted 3 lines
drivers/staging/rtl8192e/rtllib.h +0 -0
 	return escaped;
 }

Unrelated but this isn't a thread safe or multiple instance safe function.

Do you mean escape_ssid() or string_escape_mem() or both?

For the string_escape_mem() I think caller should take care of.

Quoted 1 line

It seems it's used only in debugging message output though.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#10 lore raw

Re: [PATCH 3/6] lib80211: re-use string_escape_mem_any_np()

In reply to Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko

In kernel we have function to escape a given string. Let's use it instead of custom approach.

This fixes a bug. The current implementation wrongly prints octal numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

[]

Quoted 4 lines

This code looks like it was adapted from the old print_mac ethernet code that was eventually replaced by a vsprintf pointer extension %pM

Quoted 9 lines
Quoted 2 lines

So a better way to do this might be to add and use yet another vsprintf %p<foo> extension for ssids.

Might be, but it
- doesn't reduce necessity of string_escape_mem (not only ssid are
escaped in kernel)
- prevents user to choose a rule what exactly their would like to
escape (look at the other patches against ssid escaping)

%pE<FLAGS> would allow the same arbitrary combinations of your flags.

maintainer
#2 lore raw

Re: [PATCH 1/6] lib / string_helpers: clean up test suite

In reply to Andy Shevchenko
Quoted 17 lines · On Wed, 2 Jul 2014 16:20:24 +0300 Andy Shevchenko…

This patch prepares test suite for a following update. It introduces test_string_check_buf() helper which checks the result and dumps an error.

...

lib/test-string_helpers.c +6 -0
@@ -10,6 +10,26 @@
 #include <linux/string.h>
 #include <linux/string_helpers.h>
 
+static __init bool test_string_check_buf(const char *name, unsigned int flags,
+					 char *in, size_t p,
+					 char *out_real, size_t q_real,
+					 char *out_test, size_t q_test)
+{
+	if (q_real == q_test && !memcmp(out_test, out_real, q_test))

lib/test-string_helpers.c line 28

Quoted 10 lines
lib/test-string_helpers.c +10 -0
+		return true;
+
+	pr_err("Test '%s' failed: flags = %u\n", name, flags);
+
+	print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
+		       in, p, true);
+	print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_test, q_test, true);
+	print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_real, q_real, true);

Seems strange to mix KERN_ERR and KERN_WARNING. The code's always been that way, but maybe it can be improved.

maintainer
#5 lore raw

Re: [PATCH 2/6] lib / string_helpers: introduce string_escape_mem()

In reply to Andy Shevchenko
Quoted 17 lines · On Wed, 2 Jul 2014 16:20:25 +0300 Andy Shevchenko…

This is almost the opposite function to string_unescape(). Nevertheless it handles \0 and could be used for any byte buffer.

The documentation is supplied together with the function prototype.

The test cases covers most of the scenarios and would be expanded later on.

...

include/linux/string_helpers.h +2 -0
@@ -71,4 +71,87 @@ static inline int string_unescape_any_inplace(char *buf)
 	return string_unescape_any(buf, buf, 0);
 }
 
+#define ESCAPE_SPACE		0x01
+#define ESCAPE_SPECIAL		0x02

include/linux/string_helpers.h static inline int string_unescape_any_inplace(char *buf) · line 85

Quoted 10 lines
include/linux/string_helpers.h +10 -0
+#define ESCAPE_NULL		0x04
+#define ESCAPE_OCTAL		0x08
+#define ESCAPE_ANY		\
+	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
+#define ESCAPE_NP		0x10
+#define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
+#define ESCAPE_HEX		0x20
+
+/**
+ * string_escape_mem - quote characters in the given memory buffer

It drive me nuts when the kerneldoc is in the .h file. Who thinks of looking there? I realise that string_unescape() already did that, but I'd prefer that we fix string_unescape() rather than imitate it.

lib/string_helpers.c

Quoted 2 lines
lib/string_helpers.c +0 -0

This is a lot of code! Adds nearly a kbyte. I'm surprised that escaping a string is so verbose.

I wonder if the implementation really needs to be so comprehensive?

Would a table-driven approach be more compact?

Quoted 9 lines

static int __init test_string_helpers_init(void)
{

lib/string_helpers.c +2 -0
 	unsigned int i;
@@ -112,6 +315,16 @@ static int __init test_string_helpers_init(void)
 	test_string_unescape("unescape inplace",
 			     get_random_int() % (UNESCAPE_ANY + 1), true);
 
+	/* Without dictionary */
+	for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)

lib/string_helpers.c static int __init test_string_helpers_init(void) · line 329

Quoted 10 lines
lib/string_helpers.c +8 -0
+		test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
+
+	/* With dictionary */
+	for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
+		test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
+
+	test_string_escape_nomem();
+
 	return -EINVAL;
 }

I wonder why this returns -EINVAL.

#3 lore raw

Re: [PATCH 1/6] lib / string_helpers: clean up test suite

In reply to Andrew Morton
Quoted 1 line · Andrew Morton

On Wed, 2 Jul 2014 16:20:24 +0300 Andy Shevchenko <[email protected]> wrote:

[]

Quoted 11 lines

lib/test-string_helpers.c line 28

Quoted 8 lines
lib/test-string_helpers.c +8 -0
+	pr_err("Test '%s' failed: flags = %u\n", name, flags);
+
+	print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
+		       in, p, true);
+	print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_test, q_test, true);
+	print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_real, q_real, true);

Seems strange to mix KERN_ERR and KERN_WARNING. The code's always been that way, but maybe it can be improved.

Will fix this.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#6 lore raw

Re: [PATCH 2/6] lib / string_helpers: introduce string_escape_mem()

In reply to Andrew Morton
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Wed, 2 Jul 2014 16:20:25 +0300 Andy Shevchenko…

This is almost the opposite function to string_unescape(). Nevertheless it handles \0 and could be used for any byte buffer.

The documentation is supplied together with the function prototype.

The test cases covers most of the scenarios and would be expanded later on.

...

include/linux/string_helpers.h +0 -0
@@ -71,4 +71,87 @@ static inline int string_unescape_any_inplace(char *buf)
 	return string_unescape_any(buf, buf, 0);
 }
 
Quoted 16 lines

include/linux/string_helpers.h static inline int string_unescape_any_inplace(char *buf) · line 85

Quoted 12 lines
include/linux/string_helpers.h +12 -0
+#define ESCAPE_SPACE		0x01
+#define ESCAPE_SPECIAL		0x02
+#define ESCAPE_NULL		0x04
+#define ESCAPE_OCTAL		0x08
+#define ESCAPE_ANY		\
+	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
+#define ESCAPE_NP		0x10
+#define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
+#define ESCAPE_HEX		0x20
+
+/**
+ * string_escape_mem - quote characters in the given memory buffer

It drive me nuts when the kerneldoc is in the .h file. Who thinks of looking there? I realise that string_unescape() already did that, but I'd prefer that we fix string_unescape() rather than imitate it.

No problem, I will do this in separate patch.

Quoted 7 lines

lib/string_helpers.c

Quoted 3 lines
lib/string_helpers.c +0 -0

This is a lot of code! Adds nearly a kbyte. I'm surprised that escaping a string is so verbose.
I wonder if the implementation really needs to be so comprehensive?

I studied several kernel cases and try to cover most of them.

Quoted 1 line

Would a table-driven approach be more compact?

Do you mean something like ctype set of functions?

I wouldn't think so, we have a lot of variations how we would like to escape. Currently I have such cases covered in terms of flags I introduced (not all of them in this series):
- ESCAPE_OCTAL (with dictionary)
- ESCAPE_ANY_NP
- ESCAPE_HEX | ESCAPE_NP
- ESCAPE_NULL
... not yet in form of patch
- ESCAPE_SPACE | ESCAPE_SPECIAL (with dictionary)

I can't currently realize how table could cover all of that.

Quoted 7 lines
Quoted 7 lines

static int __init test_string_helpers_init(void)
{

lib/string_helpers.c +0 -0
 	unsigned int i;
@@ -112,6 +315,16 @@ static int __init test_string_helpers_init(void)
 	test_string_unescape("unescape inplace",
 			     get_random_int() % (UNESCAPE_ANY + 1), true);
 
Quoted 14 lines

lib/string_helpers.c static int __init test_string_helpers_init(void) · line 329

Quoted 12 lines
lib/string_helpers.c +10 -0
+	/* Without dictionary */
+	for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
+		test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
+
+	/* With dictionary */
+	for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
+		test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
+
+	test_string_escape_nomem();
+
 	return -EINVAL;
 }

I wonder why this returns -EINVAL.

The idea of course to not let module be loaded. I saw this return code in test_kstrtox.c. Would you suggest better approach?

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#11 lore raw

Re: [PATCH 3/6] lib80211: re-use string_escape_mem_any_np()

In reply to Joe Perches
Quoted 26 lines · Joe Perches
Quoted 15 lines · Andy Shevchenko
Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko

In kernel we have function to escape a given string. Let's use it instead of custom approach.

This fixes a bug. The current implementation wrongly prints octal numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

[]

Quoted 4 lines

This code looks like it was adapted from the old print_mac ethernet code that was eventually replaced by a vsprintf pointer extension %pM

Quoted 9 lines
Quoted 2 lines

So a better way to do this might be to add and use yet another vsprintf %p<foo> extension for ssids.

Might be, but it
- doesn't reduce necessity of string_escape_mem (not only ssid are
escaped in kernel)
- prevents user to choose a rule what exactly their would like to
escape (look at the other patches against ssid escaping)

Quoted 2 lines

%pE<FLAGS> would allow the same arbitrary combinations of your flags.

Makes sense.
Can do this as a separate patch.
Though I would have a sane default for escaping SSIDs. I hope that one from print_ssid() could be the one.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy