Skip to contentSkip to conversation

lib: introduce string_escape_mem and %*pE specifier

PATCH In review Andy Shevchenko 5 participants 21 messages Aug 20, 2014 – Sep 1, 2014

Subsystems (8)
lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

v3 cover lib: introduce string_escape_mem and %*pE specifier

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 via %*pE specifier.

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

Changelog v3:
- address Joe's comments (typos and styling of documentation / commit messages)
- add patch 7/11
- rebase on top of recent linux-next

Changelog v2:
- addressed Joe's comments:
- added patch 4/10
- moving patches 5/10-10/10 to %*pE instead of direct string_escape_mem call, as a result the series and code looks cleaner
- address few of Andrew's comments:
- added patch 1/10
- kernel documentation moved to c-file
- when test case fails the necessary messages are printed on KERN_WARNING level

Andy Shevchenko (11):
lib / string_helpers: move documentation to c-file lib / string_helpers: refactoring the test suite lib / string_helpers: introduce string_escape_mem() lib/vsprintf: add %*pE[achnops] format specifier
wireless: libertas: print esaped string via %*pE
wireless: ipw2x00: print SSID via %*pE
wireless: hostap: proc: print properly escaped SSID
lib80211: remove unused print_ssid()
staging: wlan-ng: use %*pEhp to print SN
staging: rtl8192e: use %*pEn to escape buffer
staging: rtl8192u: use %*pEn to escape buffer

 Documentation/printk-formats.txt               |  32 +++
 drivers/net/wireless/hostap/hostap_proc.c      |   4 +-
 drivers/net/wireless/ipw2x00/ipw2100.c         |  22 +-
 drivers/net/wireless/ipw2x00/ipw2200.c         | 270 +++++++--------------
 drivers/net/wireless/ipw2x00/libipw_rx.c       |  65 +++---
 drivers/net/wireless/ipw2x00/libipw_wx.c       |  16 +-
 drivers/net/wireless/libertas/cfg.c            |   8 +-
 drivers/net/wireless/libertas/mesh.c           |   7 +-
 drivers/staging/rtl8192e/rtllib.h              |  14 +-
 drivers/staging/rtl8192u/ieee80211/ieee80211.h |  14 +-
 drivers/staging/wlan-ng/prism2sta.c            |  28 +--
 include/linux/string_helpers.h                 |  65 +++---
 include/net/lib80211.h                         |   5 -
 lib/string_helpers.c                           | 312 +++++++++++++++++++++++++
 lib/test-string_helpers.c                      | 250 +++++++++++++++++++-
 lib/vsprintf.c                                 |  72 ++++++
 net/wireless/lib80211.c                        |  32 ---
 17 files changed, 822 insertions(+), 394 deletions(-)
Signature · 1 line
2.1.0
author reviewer
#1 lore raw

PATCH v3 1/11 lib / string_helpers: move documentation to c-file

The documentation of API belongs to c-file. This patch moves it accordingly.

There is no functional change.

---
 include/linux/string_helpers.h | 34 ----------------------------------
 lib/string_helpers.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 34 deletions(-)
include/linux/string_helpers.h +0 -34
index 3eeee96..5a30f2a 100644
@@ -20,40 +20,6 @@ int string_get_size(u64 size, enum string_size_units units,
 #define UNESCAPE_ANY		\
 	(UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
 
-/**
- * string_unescape - unquote characters in the given string
- * @src:	source buffer (escaped)
- * @dst:	destination buffer (unescaped)
- * @size:	size of the destination buffer (0 to unlimit)
- * @flags:	combination of the flags (bitwise OR):
- *	%UNESCAPE_SPACE:
- *		'\f' - form feed
- *		'\n' - new line
- *		'\r' - carriage return
- *		'\t' - horizontal tab
- *		'\v' - vertical tab
- *	%UNESCAPE_OCTAL:
- *		'\NNN' - byte with octal value NNN (1 to 3 digits)
- *	%UNESCAPE_HEX:
- *		'\xHH' - byte with hexadecimal value HH (1 to 2 digits)
- *	%UNESCAPE_SPECIAL:
- *		'\"' - double quote
- *		'\\' - backslash
- *		'\a' - alert (BEL)
- *		'\e' - escape
- *	%UNESCAPE_ANY:
- *		all previous together
- *
- * Returns amount of characters processed to the destination buffer excluding
- * trailing '\0'.
- *
- * Because the size of the output will be the same as or less than the size of
- * the input, the transformation may be performed in place.
- *
- * Caller must provide valid source and destination pointers. Be aware that
- * destination buffer will always be NULL-terminated. Source string must be
- * NULL-terminated as well.
- */
 int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
 
 static inline int string_unescape_inplace(char *buf, unsigned int flags)
lib/string_helpers.c +38 -0
index 29033f3..74ec604 100644
@@ -168,6 +168,44 @@ static bool unescape_special(char **src, char **dst)
 	return true;
 }
 
+/**
+ * string_unescape - unquote characters in the given string
+ * @src:	source buffer (escaped)
+ * @dst:	destination buffer (unescaped)
+ * @size:	size of the destination buffer (0 to unlimit)
+ * @flags:	combination of the flags (bitwise OR):
+ *	%UNESCAPE_SPACE:
+ *		'\f' - form feed
+ *		'\n' - new line
+ *		'\r' - carriage return
+ *		'\t' - horizontal tab
+ *		'\v' - vertical tab
+ *	%UNESCAPE_OCTAL:
+ *		'\NNN' - byte with octal value NNN (1 to 3 digits)
+ *	%UNESCAPE_HEX:
+ *		'\xHH' - byte with hexadecimal value HH (1 to 2 digits)
+ *	%UNESCAPE_SPECIAL:
+ *		'\"' - double quote
+ *		'\\' - backslash
+ *		'\a' - alert (BEL)
+ *		'\e' - escape
+ *	%UNESCAPE_ANY:
+ *		all previous together
+ *
+ * Description:
+ * The function unquotes characters in the given string.
+ *
+ * Because the size of the output will be the same as or less than the size of
+ * the input, the transformation may be performed in place.
+ *
+ * Caller must provide valid source and destination pointers. Be aware that
+ * destination buffer will always be NULL-terminated. Source string must be
+ * NULL-terminated as well.
+ *
+ * Return:
+ * The amount of the characters processed to the destination buffer excluding
+ * trailing '\0' is returned.
+ */
 int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
 {
 	char *out = dst;
Signature · 1 line
2.1.0
author reviewer
#2 lore raw

PATCH v3 2/11 lib / string_helpers: refactoring the 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..ac44c92 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_warn("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.1.0
author reviewer
#3 lore raw

PATCH v3 3/11 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 |  31 +++++
 lib/string_helpers.c           | 274 +++++++++++++++++++++++++++++++++++++++++
 lib/test-string_helpers.c      | 213 ++++++++++++++++++++++++++++++++
 3 files changed, 518 insertions(+)
include/linux/string_helpers.h +31 -0
index 5a30f2a..6eb567a 100644
@@ -37,4 +37,35 @@ 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
+
+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 +274 -0

283 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.1.0
author reviewer
#4 lore raw

PATCH v3 4/11 lib/vsprintf: add %*pE[achnops] format specifier

In reply to Andy Shevchenko

This allows user to print a given buffer as an escaped string. The rules are applied according to an optional mix of flags provided by additional format letters.

For example, if the given buffer is:

	1b 62 20 5c 43 07 22 90 0d 5d

The result strings would be:

	%*pE            "\eb \C\a"\220\r]"
	%*pEhp          "\x1bb \C\x07"\x90\x0d]"
	%*pEa           "\e\142\040\\\103\a\042\220\r\135"

Please, read Documentation/printk-formats.txt and lib/string_helpers.c kernel documentation to get further information.

---
 Documentation/printk-formats.txt | 32 ++++++++++++++++++
 lib/vsprintf.c                   | 72 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)
Documentation/printk-formats.txt +32 -0
index 3b56a99..8858db8 100644
@@ -70,6 +70,38 @@ DMA addresses types dma_addr_t:
 	For printing a dma_addr_t type which can vary based on build options,
 	regardless of the width of the CPU data path. Passed by reference.
 
+Raw buffer as an escaped string:
+
+	%*pE[achnops]
+
+	For printing raw buffer as an escaped string. For the following buffer
+
+		1b 62 20 5c 43 07 22 90 0d 5d
+
+	few examples show how the conversion would be done (the result string
+	without surrounding quotes):
+
+		%*pE		"\eb \C\a"\220\r]"
+		%*pEhp		"\x1bb \C\x07"\x90\x0d]"
+		%*pEa		"\e\142\040\\\103\a\042\220\r\135"
+
+	The conversion rules are applied according to an optional combination
+	of flags (see string_escape_mem() kernel documentation for the
+	details):
+		a - ESCAPE_ANY
+		c - ESCAPE_SPECIAL
+		h - ESCAPE_HEX
+		n - ESCAPE_NULL
+		o - ESCAPE_OCTAL
+		p - ESCAPE_NP
+		s - ESCAPE_SPACE
+	By default ESCAPE_ANY_NP is used.
+
+	ESCAPE_ANY_NP is the sane choice for many cases, in particularly for
+	printing SSIDs.
+
+	If field width is omitted the 1 byte only will be escaped.
+
 Raw buffer as a hex string:
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
lib/vsprintf.c +72 -0

102 lines not on this page · raw message

Signature · 1 line
2.1.0
#5 lore raw

Re: [PATCH v3 04/11] lib/vsprintf: add %*pE[achnops] format specifier

Quoted 3 lines · Andy Shevchenko

This allows user to print a given buffer as an escaped string. The rules are applied according to an optional mix of flags provided by additional format letters.

Thanks Andy. Looks good to me.

Very trivial note:

lib/vsprintf.c

Quoted 1 line
lib/vsprintf.c +0 -0

[]

Quoted 9 lines
lib/vsprintf.c +5 -0
@@ -1101,6 +1102,63 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
 }
 
 static noinline_for_stack
+char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
+		     const char *fmt)
+{
+	bool found = true;
+	int count = 1;

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1119

Quoted 10 lines
lib/vsprintf.c +10 -0
+	unsigned int flags = 0;
+	int len;
+
+	if (spec.field_width == 0)
+		/* nothing to print */
+		return buf;
+
+	if (ZERO_OR_NULL_PTR(addr))
+		/* NULL pointer */
+		return string(buf, end, NULL, spec);

As the code seems more readable than the comments, it might be better to remove these comments or maybe move the comment above the test.

	if (spec.field_width == 0)
		return buf;

	if (ZERO_OR_NULL_PTR(addr))
		return string(buf, end, NULL, spec);
author reviewer
#6 lore raw

Re: [PATCH v3 04/11] lib/vsprintf: add %*pE[achnops] format specifier

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

This allows user to print a given buffer as an escaped string. The rules are applied according to an optional mix of flags provided by additional format letters.

Thanks Andy. Looks good to me.

Can I imply your Reviewed-by?

Quoted 1 line

Very trivial note:

Fixed locally. Waiting for few days for more comments and will send v5 then.

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

PATCH v3 5/11 wireless: libertas: print esaped string via %*pE

In reply to Andy Shevchenko

Instead of custom approach this allows to print escaped strings via recently added kernel extension: %*pE.

---
 drivers/net/wireless/libertas/cfg.c  | 8 ++------
 drivers/net/wireless/libertas/mesh.c | 7 +++----
 2 files changed, 5 insertions(+), 10 deletions(-)
drivers/net/wireless/libertas/cfg.c +2 -6
index 47a998d..1188814 100644
@@ -590,7 +590,6 @@ static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 		int chan_no = -1;
 		const u8 *ssid = NULL;
 		u8 ssid_len = 0;
-		DECLARE_SSID_BUF(ssid_buf);
 
 		int len = get_unaligned_le16(pos);
 		pos += 2;
@@ -644,10 +643,8 @@ static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 			struct ieee80211_channel *channel =
 				ieee80211_get_channel(wiphy, freq);
 
-			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
-				     "%d dBm\n",
-				     bssid, capa, chan_no,
-				     print_ssid(ssid_buf, ssid, ssid_len),
+			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
+				     bssid, capa, chan_no, ssid_len, ssid,
 				     LBS_SCAN_RSSI_TO_MBM(rssi)/100);
 
 			if (channel &&
@@ -1982,7 +1979,6 @@ static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
 	struct lbs_private *priv = wiphy_priv(wiphy);
 	int ret = 0;
 	struct cfg80211_bss *bss;
-	DECLARE_SSID_BUF(ssid_buf);
 
 	if (dev == priv->mesh_dev)
 		return -EOPNOTSUPP;
drivers/net/wireless/libertas/mesh.c +3 -4
index 01a67f6..d0c881d 100644
@@ -93,7 +93,6 @@ static int lbs_mesh_config(struct lbs_private *priv, uint16_t action,
 {
 	struct cmd_ds_mesh_config cmd;
 	struct mrvl_meshie *ie;
-	DECLARE_SSID_BUF(ssid);
 
 	memset(&cmd, 0, sizeof(cmd));
 	cmd.channel = cpu_to_le16(chan);
@@ -122,9 +121,9 @@ static int lbs_mesh_config(struct lbs_private *priv, uint16_t action,
 	default:
 		return -1;
 	}
-	lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
-		    action, priv->mesh_tlv, chan,
-		    print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
+	lbs_deb_cmd("mesh config action %d type %x channel %d SSID %*pE\n",
+		    action, priv->mesh_tlv, chan, priv->mesh_ssid_len,
+		    priv->mesh_ssid);
 
 	return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
 }
Signature · 1 line
2.1.0
author reviewer
#8 lore raw

PATCH v3 6/11 wireless: ipw2x00: print SSID via %*pE

In reply to Andy Shevchenko

Instead of custom approach this allows to print escaped strings via recently added kernel extension: %*pE.

---
 drivers/net/wireless/ipw2x00/ipw2100.c   |  22 +--
 drivers/net/wireless/ipw2x00/ipw2200.c   | 270 ++++++++++---------------------
 drivers/net/wireless/ipw2x00/libipw_rx.c |  65 +++-----
 drivers/net/wireless/ipw2x00/libipw_wx.c |  16 +-
 4 files changed, 126 insertions(+), 247 deletions(-)
drivers/net/wireless/ipw2x00/ipw2100.c +7 -15
index c3d726f..6fabea0 100644
@@ -2005,7 +2005,6 @@ static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
 	u32 chan;
 	char *txratename;
 	u8 bssid[ETH_ALEN];
-	DECLARE_SSID_BUF(ssid);
 
 	/*
 	 * TBD: BSSID is usually 00:00:00:00:00:00 here and not
@@ -2067,8 +2066,8 @@ static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
 		break;
 	}
 
-	IPW_DEBUG_INFO("%s: Associated with '%s' at %s, channel %d (BSSID=%pM)\n",
-		       priv->net_dev->name, print_ssid(ssid, essid, essid_len),
+	IPW_DEBUG_INFO("%s: Associated with '%*pE' at %s, channel %d (BSSID=%pM)\n",
+		       priv->net_dev->name, essid_len, essid,
 		       txratename, chan, bssid);
 
 	/* now we copy read ssid into dev */
@@ -2095,9 +2094,8 @@ static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
 		.host_command_length = ssid_len
 	};
 	int err;
-	DECLARE_SSID_BUF(ssid);
 
-	IPW_DEBUG_HC("SSID: '%s'\n", print_ssid(ssid, essid, ssid_len));
+	IPW_DEBUG_HC("SSID: '%*pE'\n", ssid_len, essid);
 
 	if (ssid_len)
 		memcpy(cmd.host_command_parameters, essid, ssid_len);
@@ -2138,11 +2136,8 @@ static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
 
 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
 {
-	DECLARE_SSID_BUF(ssid);
-
 	IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
-		  "disassociated: '%s' %pM\n",
-		  print_ssid(ssid, priv->essid, priv->essid_len),
+		  "disassociated: '%*pE' %pM\n", priv->essid_len, priv->essid,
 		  priv->bssid);
 
 	priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
@@ -6975,7 +6970,6 @@ static int ipw2100_wx_set_essid(struct net_device *dev,
 	char *essid = "";	/* ANY */
 	int length = 0;
 	int err = 0;
-	DECLARE_SSID_BUF(ssid);
 
 	mutex_lock(&priv->action_mutex);
 	if (!(priv->status & STATUS_INITIALIZED)) {
@@ -7005,8 +6999,7 @@ static int ipw2100_wx_set_essid(struct net_device *dev,
 		goto done;
 	}
 
-	IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n",
-		     print_ssid(ssid, essid, length), length);
+	IPW_DEBUG_WX("Setting ESSID: '%*pE' (%d)\n", length, essid, length);
 
 	priv->essid_len = length;
 	memcpy(priv->essid, essid, priv->essid_len);
@@ -7027,13 +7020,12 @@ static int ipw2100_wx_get_essid(struct net_device *dev,
 	 */
 
 	struct ipw2100_priv *priv = libipw_priv(dev);
-	DECLARE_SSID_BUF(ssid);
 
 	/* If we are associated, trying to associate, or have a statically
 	 * configured ESSID then return that; otherwise return ANY */
 	if (priv->config & CFG_STATIC_ESSID || priv->status & STATUS_ASSOCIATED) {
-		IPW_DEBUG_WX("Getting essid: '%s'\n",
-			     print_ssid(ssid, priv->essid, priv->essid_len));
+		IPW_DEBUG_WX("Getting essid: '%*pE'\n",
+			     priv->essid_len, priv->essid);
 		memcpy(extra, priv->essid, priv->essid_len);
 		wrqu->essid.length = priv->essid_len;
 		wrqu->essid.flags = 1;	/* active */
drivers/net/wireless/ipw2x00/ipw2200.c +87 -183

557 lines not on this page · raw message

drivers/net/wireless/ipw2x00/libipw_rx.c +26 -39

119 lines not on this page · raw message

drivers/net/wireless/ipw2x00/libipw_wx.c +6 -10
index 54aba47..dd29f46 100644
@@ -272,7 +272,6 @@ int libipw_wx_get_scan(struct libipw_device *ieee,
 	char *ev = extra;
 	char *stop = ev + wrqu->data.length;
 	int i = 0;
-	DECLARE_SSID_BUF(ssid);
 
 	LIBIPW_DEBUG_WX("Getting scan\n");
 
@@ -290,12 +289,10 @@ int libipw_wx_get_scan(struct libipw_device *ieee,
 			ev = libipw_translate_scan(ieee, ev, stop, network,
 						      info);
 		else {
-			LIBIPW_DEBUG_SCAN("Not showing network '%s ("
-					     "%pM)' due to age (%ums).\n",
-					     print_ssid(ssid, network->ssid,
-							 network->ssid_len),
-					     network->bssid,
-					     elapsed_jiffies_msecs(
+			LIBIPW_DEBUG_SCAN("Not showing network '%*pE (%pM)' due to age (%ums).\n",
+					  network->ssid_len, network->ssid,
+					  network->bssid,
+					  elapsed_jiffies_msecs(
 					               network->last_scanned));
 		}
 	}
@@ -322,7 +319,6 @@ int libipw_wx_set_encode(struct libipw_device *ieee,
 	int i, key, key_provided, len;
 	struct lib80211_crypt_data **crypt;
 	int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
-	DECLARE_SSID_BUF(ssid);
 
 	LIBIPW_DEBUG_WX("SET_ENCODE\n");
 
@@ -417,8 +413,8 @@ int libipw_wx_set_encode(struct libipw_device *ieee,
 		if (len > erq->length)
 			memset(sec.keys[key] + erq->length, 0,
 			       len - erq->length);
-		LIBIPW_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
-				   key, print_ssid(ssid, sec.keys[key], len),
+		LIBIPW_DEBUG_WX("Setting key %d to '%*pE' (%d:%d bytes)\n",
+				   key, len, sec.keys[key],
 				   erq->length, len);
 		sec.key_sizes[key] = len;
 		if (*crypt)
Signature · 1 line
2.1.0
author reviewer
#9 lore raw

PATCH v3 7/11 wireless: hostap: proc: print properly escaped SSID

In reply to Andy Shevchenko

Instead of substituting non-printable characters by '_' let's print SSID properly escaped by using recently added %*pE specifier.

---
 drivers/net/wireless/hostap/hostap_proc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/wireless/hostap/hostap_proc.c +1 -3
index 4e5c0f8..16a06b6 100644
@@ -181,9 +181,7 @@ static int prism2_bss_list_proc_show(struct seq_file *m, void *v)
 		   bss->bssid, bss->last_update,
 		   bss->count, bss->capab_info);
 
-	for (i = 0; i < bss->ssid_len; i++)
-		seq_putc(m,bss->ssid[i] >= 32 && bss->ssid[i] < 127 ?
-			   bss->ssid[i] : '_');
+	seq_printf(m, "%*pE", bss->ssid_len, bss->ssid);
 
 	seq_putc(m, '\t');
 	for (i = 0; i < bss->ssid_len; i++)
Signature · 1 line
2.1.0
author reviewer
#10 lore raw

Re: [PATCH v3 07/11] wireless: hostap: proc: print properly escaped SSID

Quoted 2 lines · Andy Shevchenko

Instead of substituting non-printable characters by '_' let's print SSID properly escaped by using recently added %*pE specifier.

Here is kbuild robot fixup.

Quoted 1 line

From f88ff364a1b117d3c86186206eeaa54ff906147a Mon Sep 17 00:00:00 2001

From: Andy Shevchenko <[email protected]>
Date: Mon, 1 Sep 2014 10:13:28 +0300
Subject: [PATCH] wireless: hostap: kbuild robot fixup

---
 drivers/net/wireless/hostap/hostap_proc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/net/wireless/hostap/hostap_proc.c
index 16a06b6..5b1a510 100644

drivers/net/wireless/hostap/hostap_proc.c +0 -0
@@ -181,7 +181,7 @@ static int prism2_bss_list_proc_show(struct seq_file

*m, void *v)
bss->bssid, bss->last_update,
bss->count, bss->capab_info);

- seq_printf(m, "%*pE", bss->ssid_len, bss->ssid);
+ seq_printf(m, "%*pE", (int)bss->ssid_len, bss->ssid);

seq_putc(m, '\t');
for (i = 0; i < bss->ssid_len; i++)

Signature · 1 line
2.1.0
Quoted 21 lines
---
 drivers/net/wireless/hostap/hostap_proc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/wireless/hostap/hostap_proc.c +1 -3
index 4e5c0f8..16a06b6 100644
@@ -181,9 +181,7 @@ static int prism2_bss_list_proc_show(struct seq_file *m, void *v)
 		   bss->bssid, bss->last_update,
 		   bss->count, bss->capab_info);
 
-	for (i = 0; i < bss->ssid_len; i++)
-		seq_putc(m,bss->ssid[i] >= 32 && bss->ssid[i] < 127 ?
-			   bss->ssid[i] : '_');
+	seq_printf(m, "%*pE", bss->ssid_len, bss->ssid);
 
 	seq_putc(m, '\t');
 	for (i = 0; i < bss->ssid_len; i++)
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author reviewer
#11 lore raw

PATCH v3 8/11 lib80211: remove unused print_ssid()

In reply to Andy Shevchenko

In kernel we have %*pE specifier to print an escaped buffer. All users now switched to that approach.

This fixes a bug as well. 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 by default the \f, \v, \a, and \e are escaped to their alphabetic representation. It's safe to do since it is currently used for messaging only.

---
 include/net/lib80211.h  |  5 -----
 net/wireless/lib80211.c | 32 --------------------------------
 2 files changed, 37 deletions(-)
include/net/lib80211.h +0 -5
index be95b92..aab0f42 100644
@@ -32,11 +32,6 @@
 #include <linux/timer.h>
 #include <linux/seq_file.h>
 
-/* print_ssid() is intended to be used in debug (and possibly error)
- * messages. It should never be used for passing ssid to user space. */
-const char *print_ssid(char *buf, const char *ssid, u8 ssid_len);
-#define DECLARE_SSID_BUF(var) char var[IEEE80211_MAX_SSID_LEN * 4 + 1] __maybe_unused
-
 #define NUM_WEP_KEYS	4
 
 enum {
net/wireless/lib80211.c +0 -32
index a55c27b..4596115 100644
@@ -46,38 +46,6 @@ static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
 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 = '\0';
-	return buf;
-}
-EXPORT_SYMBOL(print_ssid);
-
 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
 				spinlock_t *lock)
 {
Signature · 1 line
2.1.0
author reviewer
#12 lore raw

PATCH v3 9/11 staging: wlan-ng: use %*pEhp to print SN

In reply to Andy Shevchenko

This is a generic specifier to print an escaped buffer by given criteria. Let's use it instead of custom approach.

---
 drivers/staging/wlan-ng/prism2sta.c | 28 ++--------------------------
 1 file changed, 2 insertions(+), 26 deletions(-)
drivers/staging/wlan-ng/prism2sta.c +2 -26
index 799ce8a..df577df 100644
@@ -60,7 +60,6 @@
 #include <linux/netdevice.h>
 #include <linux/workqueue.h>
 #include <linux/byteorder/generic.h>
-#include <linux/ctype.h>
 
 #include <linux/io.h>
 #include <linux/delay.h>
@@ -81,27 +80,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);
 
@@ -607,7 +585,6 @@ static int prism2sta_getcardinfo(wlandevice_t *wlandev)
 	hfa384x_t *hw = (hfa384x_t *) wlandev->priv;
 	u16 temp;
 	u8 snum[HFA384x_RID_NICSERIALNUMBER_LEN];
-	char pstr[(HFA384x_RID_NICSERIALNUMBER_LEN * 4) + 1];
 
 	/* Collect version and compatibility info */
 	/*  Some are critical, some are not */
@@ -862,9 +839,8 @@ 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));
-		netdev_info(wlandev->netdev, "Prism2 card SN: %s\n", pstr);
+		netdev_info(wlandev->netdev, "Prism2 card SN: %*pEhp\n",
+			    HFA384x_RID_NICSERIALNUMBER_LEN, snum);
 	} else {
 		netdev_err(wlandev->netdev, "Failed to retrieve Prism2 Card SN\n");
 		goto failed;
Signature · 1 line
2.1.0
author reviewer
#13 lore raw

PATCH v3 10/11 staging: rtl8192e: use %*pEn to escape buffer

In reply to Andy Shevchenko

Let's use kernel's native specifier to escape a buffer.

---
 drivers/staging/rtl8192e/rtllib.h | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
drivers/staging/rtl8192e/rtllib.h +1 -13
index 83f5f57..9840be0 100644
@@ -2956,25 +2956,13 @@ 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)) {
 		memcpy(escaped, "<hidden>", sizeof("<hidden>"));
 		return escaped;
 	}
 
-	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
-	*d = '\0';
+	snprintf(escaped, sizeof(escaped), "%*pEn", essid_len, essid);
 	return escaped;
 }
 
Signature · 1 line
2.1.0
author reviewer
#14 lore raw

PATCH v3 11/11 staging: rtl8192u: use %*pEn to escape buffer

In reply to Andy Shevchenko

Let's use kernel's native specifier to escape a buffer.

---
 drivers/staging/rtl8192u/ieee80211/ieee80211.h | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
drivers/staging/rtl8192u/ieee80211/ieee80211.h +1 -13
index c118551..078333b 100644
@@ -2580,25 +2580,13 @@ 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)) {
 		memcpy(escaped, "<hidden>", sizeof("<hidden>"));
 		return escaped;
 	}
 
-	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
-	*d = '\0';
+	snprintf(escaped, sizeof(escaped), "%*pEn", essid_len, essid);
 	return escaped;
 }
 
Signature · 1 line
2.1.0
#15 lore raw
In reply to Andy Shevchenko

Any objections? Do you (Andy) want me to merge this through the wireless tree?

John

Quoted 59 lines · Andy Shevchenko

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 via %*pE specifier.

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

Changelog v3:
- address Joe's comments (typos and styling of documentation / commit messages)
- add patch 7/11
- rebase on top of recent linux-next

Changelog v2:
- addressed Joe's comments:
- added patch 4/10
- moving patches 5/10-10/10 to %*pE instead of direct string_escape_mem call, as a result the series and code looks cleaner
- address few of Andrew's comments:
- added patch 1/10
- kernel documentation moved to c-file
- when test case fails the necessary messages are printed on KERN_WARNING level

Andy Shevchenko (11):
lib / string_helpers: move documentation to c-file lib / string_helpers: refactoring the test suite lib / string_helpers: introduce string_escape_mem() lib/vsprintf: add %*pE[achnops] format specifier
wireless: libertas: print esaped string via %*pE
wireless: ipw2x00: print SSID via %*pE
wireless: hostap: proc: print properly escaped SSID
lib80211: remove unused print_ssid()
staging: wlan-ng: use %*pEhp to print SN
staging: rtl8192e: use %*pEn to escape buffer
staging: rtl8192u: use %*pEn to escape buffer

 Documentation/printk-formats.txt               |  32 +++
 drivers/net/wireless/hostap/hostap_proc.c      |   4 +-
 drivers/net/wireless/ipw2x00/ipw2100.c         |  22 +-
 drivers/net/wireless/ipw2x00/ipw2200.c         | 270 +++++++--------------
 drivers/net/wireless/ipw2x00/libipw_rx.c       |  65 +++---
 drivers/net/wireless/ipw2x00/libipw_wx.c       |  16 +-
 drivers/net/wireless/libertas/cfg.c            |   8 +-
 drivers/net/wireless/libertas/mesh.c           |   7 +-
 drivers/staging/rtl8192e/rtllib.h              |  14 +-
 drivers/staging/rtl8192u/ieee80211/ieee80211.h |  14 +-
 drivers/staging/wlan-ng/prism2sta.c            |  28 +--
 include/linux/string_helpers.h                 |  65 +++---
 include/net/lib80211.h                         |   5 -
 lib/string_helpers.c                           | 312 +++++++++++++++++++++++++
 lib/test-string_helpers.c                      | 250 +++++++++++++++++++-
 lib/vsprintf.c                                 |  72 ++++++
 net/wireless/lib80211.c                        |  32 ---
 17 files changed, 822 insertions(+), 394 deletions(-)
Signature · 1 line
2.1.0
Signature · 2 lines
John W. Linville		Someday the world will need a hero, and you
[email protected]			might be all we have.  Be ready.
maintainer
#16 lore raw
Quoted 9 lines · On Thu, 28 Aug 2014 14:33:30 -0400 "John W. Linville" <[email protected]>…
Quoted 4 lines · Andy Shevchenko

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 via %*pE specifier.

Any objections? Do you (Andy) want me to merge this through the wireless tree?

Who is this top-poster and what have you done with John?

I was going to apply them yesterday but in [4/11] Andy said he plans on sending out a v5. (v4 actually?)

#17 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Thu, 28 Aug 2014 14:33:30 -0400 "John W. Linville" <[email protected]>…
Quoted 4 lines · Andy Shevchenko

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 via %*pE specifier.

Quoted 5 lines
Quoted 3 lines

Any objections? Do you (Andy) want me to merge this through the wireless tree?

Who is this top-poster and what have you done with John?

:-)

Quoted 2 lines

I was going to apply them yesterday but in [4/11] Andy said he plans on sending out a v5. (v4 actually?)

OK, I'll not worry any more about it.

Signature · 2 lines
John W. Linville		Someday the world will need a hero, and you
[email protected]			might be all we have.  Be ready.
#18 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Thu, 28 Aug 2014 14:33:30 -0400 "John W. Linville" <[email protected]>…
Quoted 4 lines · Andy Shevchenko

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 via %*pE specifier.

Quoted 8 lines
Quoted 3 lines

Any objections? Do you (Andy) want me to merge this through the wireless tree?

Who is this top-poster and what have you done with John?

I was going to apply them yesterday but in [4/11] Andy said he plans on sending out a v5. (v4 actually?)

For now (so far no more comments) it is only couple of trivia fixes (removing useless comments). Would you like to resend whole series or just fixup would be enough?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#19 lore raw
Quoted 14 lines · On Thu, 28 Aug 2014 23:58:45 +0300 Andy Shevchenko <[email protected]>…
Quoted 12 lines · Andrew Morton
Quoted 9 lines · On Thu, 28 Aug 2014 14:33:30 -0400 "John W. Linville" <[email protected]>…
Quoted 4 lines · Andy Shevchenko

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 via %*pE specifier.

Any objections? Do you (Andy) want me to merge this through the wireless tree?

Quoted 8 lines
Quoted 4 lines

Who is this top-poster and what have you done with John?

I was going to apply them yesterday but in [4/11] Andy said he plans on sending out a v5. (v4 actually?)

For now (so far no more comments) it is only couple of trivia fixes (removing useless comments). Would you like to resend whole series or just fixup would be enough?

I fixed up the one Joe commented on.

lib/vsprintf.c renamed from lib/vsprintf.c~lib-vsprintf-add-%pe-format-specifier-fix +3 -4
@@ -1111,12 +1111,11 @@ char *escaped_string(char *buf, char *en
 	int len;
 
 	if (spec.field_width == 0)
-		/* nothing to print */
-		return buf;
+		return buf;				/* nothing to print */
 
 	if (ZERO_OR_NULL_PTR(addr))
-		/* NULL pointer */
-		return string(buf, end, NULL, spec);
+		return string(buf, end, NULL, spec);	/* NULL pointer */
+
 
 	do {
 		switch (fmt[count++]) {

_

#20 lore raw
Quoted 1 line · Andrew Morton

On Thu, 28 Aug 2014 23:58:45 +0300 Andy Shevchenko <[email protected]> wrote:

[]

Quoted 14 lines
Quoted 3 lines

For now (so far no more comments) it is only couple of trivia fixes (removing useless comments). Would you like to resend whole series or just fixup would be enough?

I fixed up the one Joe commented on.

lib/vsprintf.c renamed from lib/vsprintf.c~lib-vsprintf-add-%pe-format-specifier-fix +0 -2
@@ -1111,12 +1111,11 @@ char *escaped_string(char *buf, char *en
        int len;
 
        if (spec.field_width == 0)
-               /* nothing to print */
-               return buf;

lib/vsprintf.c char *escaped_string(char *buf, char *en · line 1121

Quoted 10 lines
lib/vsprintf.c renamed from lib/vsprintf.c~lib-vsprintf-add-%pe-format-specifier-fix +3 -2
+               return buf;                             /* nothing to print */
 
        if (ZERO_OR_NULL_PTR(addr))
-               /* NULL pointer */
-               return string(buf, end, NULL, spec);
+               return string(buf, end, NULL, spec);    /* NULL pointer */
+
 
        do {
                switch (fmt[count++]) {

Thanks! I'm fine with this fix.

Signature · 2 lines
With Best Regards,
Andy Shevchenko