lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date:   Sun, 3 Mar 2019 18:47:32 +0100
From:   Heiner Kallweit <hkallweit1@...il.com>
To:     Guenter Roeck <linux@...ck-us.net>,
        Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Cc:     Linux Kernel Mailing List <linux-kernel@...r.kernel.org>
Subject: Fwd: [PATCH net-next 1/2] lib: string: add strreplace_nonalnum

I submitted this through the netdev tree, maybe relevant for you as well.
See also here: https://marc.info/?t=155103900100003&r=1&w=2

-------- Forwarded Message --------
Subject: [PATCH net-next 1/2] lib: string: add strreplace_nonalnum
Date: Sun, 3 Mar 2019 18:20:50 +0100
From: Heiner Kallweit <hkallweit1@...il.com>
To: Florian Fainelli <f.fainelli@...il.com>, Andrew Lunn <andrew@...n.ch>, David Miller <davem@...emloft.net>
CC: netdev@...r.kernel.org <netdev@...r.kernel.org>

Add a new function strreplace_nonalnum that replaces all
non-alphanumeric characters. Such functionality is needed e.g. when a
string is supposed to be used in a sysfs file name. If '\0' is given
as new character then non-alphanumeric characters are cut. 

Signed-off-by: Heiner Kallweit <hkallweit1@...il.com>
---
 include/linux/string.h |  1 +
 lib/string.c           | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 7927b875f..d827b0b0f 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -169,6 +169,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+char *strreplace_nonalnum(char *s, char new);
 
 extern void kfree_const(const void *x);
 
diff --git a/lib/string.c b/lib/string.c
index 38e4ca08e..f2b1baf96 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -1047,6 +1047,33 @@ char *strreplace(char *s, char old, char new)
 }
 EXPORT_SYMBOL(strreplace);
 
+/**
+ * strreplace_nonalnum - Replace all non-alphanumeric characters in a string.
+ * @s: The string to operate on.
+ * @new: The character non-alphanumeric characters are replaced with.
+ *
+ * If new is '\0' then non-alphanumeric characters are cut.
+ *
+ * Returns pointer to the nul byte at the end of the modified string.
+ */
+char *strreplace_nonalnum(char *s, char new)
+{
+	char *p = s;
+
+	for (; *s; ++s)
+		if (isalnum(*s)) {
+			if (p != s)
+				*p = *s;
+			++p;
+		} else if (new) {
+			*p++ = new;
+		}
+	*p = '\0';
+
+	return p;
+}
+EXPORT_SYMBOL(strreplace_nonalnum);
+
 void fortify_panic(const char *name)
 {
 	pr_emerg("detected buffer overflow in %s\n", name);
-- 
2.21.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ