Skip to contentSkip to conversation

string_helpers: Move string_is_valid() to the header

PATCH Superseded Andy Shevchenko 3 participants 5 messages Feb 6, 2023 – Feb 8, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 string_helpers: Move string_is_valid() to the header

Move string_is_valid() to the header for wider use.

---
v2: added tag and updated subject (Simon)

 include/linux/string_helpers.h | 5 +++++
 net/tipc/netlink_compat.c      | 6 +-----
 2 files changed, 6 insertions(+), 5 deletions(-)
include/linux/string_helpers.h +5 -0
index 88fb8e1d0421..01c9a432865a 100644
@@ -12,6 +12,11 @@ struct device;
 struct file;
 struct task_struct;
 
+static inline bool string_is_valid(const char *s, int len)
+{
+	return memchr(s, '\0', len) ? true : false;
+}
+
 /* Descriptions of the types of units to
  * print in */
 enum string_size_units {
net/tipc/netlink_compat.c +1 -5
index dfea27a906f2..75186cd551a0 100644
@@ -39,6 +39,7 @@
 #include "node.h"
 #include "net.h"
 #include <net/genetlink.h>
+#include <linux/string_helpers.h>
 #include <linux/tipc_config.h>
 
 /* The legacy API had an artificial message length limit called
@@ -173,11 +174,6 @@ static struct sk_buff *tipc_get_err_tlv(char *str)
 	return buf;
 }
 
-static inline bool string_is_valid(char *s, int len)
-{
-	return memchr(s, '\0', len) ? true : false;
-}
-
 static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
 				   struct tipc_nl_compat_msg *msg,
 				   struct sk_buff *arg)
Signature · 1 line
2.39.1
#1 lore raw

PATCH v2 2/3 genetlink: Use string_is_valid() helper

Use string_is_valid() helper instead of cpecific memchr() call. This shows better the intention of the call.

---
v2: added tag and updated subject (Simon)

 net/netlink/genetlink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netlink/genetlink.c +2 -2
index 600993c80050..d7616c1bda93 100644
@@ -13,7 +13,7 @@
 #include <linux/errno.h>
 #include <linux/types.h>
 #include <linux/socket.h>
-#include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/skbuff.h>
 #include <linux/mutex.h>
 #include <linux/bitmap.h>
@@ -457,7 +457,7 @@ static int genl_validate_assign_mc_groups(struct genl_family *family)
 
 		if (WARN_ON(grp->name[0] == '\0'))
 			return -EINVAL;
-		if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
+		if (WARN_ON(!string_is_valid(grp->name, GENL_NAMSIZ)))
 			return -EINVAL;
 	}
 
Signature · 1 line
2.39.1
#2 lore raw

PATCH v2 3/3 openvswitch: Use string_is_valid() helper

In reply to Andy Shevchenko

Use string_is_valid() helper instead of cpecific memchr() call. This shows better the intention of the call.

---
v2: added tag and updated subject (Simon)

 net/openvswitch/conntrack.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/openvswitch/conntrack.c +3 -2
index 2172930b1f17..1d65805e79b4 100644
@@ -9,6 +9,7 @@
 #include <linux/udp.h>
 #include <linux/sctp.h>
 #include <linux/static_key.h>
+#include <linux/string_helpers.h>
 #include <net/ip.h>
 #include <net/genetlink.h>
 #include <net/netfilter/nf_conntrack_core.h>
@@ -1383,7 +1384,7 @@ static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
 #endif
 		case OVS_CT_ATTR_HELPER:
 			*helper = nla_data(a);
-			if (!memchr(*helper, '\0', nla_len(a))) {
+			if (!string_is_valid(*helper, nla_len(a))) {
 				OVS_NLERR(log, "Invalid conntrack helper");
 				return -EINVAL;
 			}
@@ -1404,7 +1405,7 @@ static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
 		case OVS_CT_ATTR_TIMEOUT:
 			memcpy(info->timeout, nla_data(a), nla_len(a));
-			if (!memchr(info->timeout, '\0', nla_len(a))) {
+			if (!string_is_valid(info->timeout, nla_len(a))) {
 				OVS_NLERR(log, "Invalid conntrack timeout");
 				return -EINVAL;
 			}
Signature · 1 line
2.39.1
maintainer
#3 lore raw
In reply to Andy Shevchenko

include/linux/string_helpers.h struct device; · line 18

Quoted 4 lines · On Mon, 6 Feb 2023 18:13:12 +0200 Andy Shevchenko wrote:
include/linux/string_helpers.h +4 -0
+static inline bool string_is_valid(const char *s, int len)
+{
+	return memchr(s, '\0', len) ? true : false;
+}

I was tempted to suggest adding a kdoc, but perhaps the function doesn't have an obvious enough name? Maybe we should call the helper string_is_terminated(), instead?

#4 lore raw
Quoted 9 lines · Jakub Kicinski

include/linux/string_helpers.h struct device; · line 18

Quoted 4 lines · On Mon, 6 Feb 2023 18:13:12 +0200 Andy Shevchenko wrote:
include/linux/string_helpers.h +4 -0
+static inline bool string_is_valid(const char *s, int len)
+{
+     return memchr(s, '\0', len) ? true : false;
+}

I was tempted to suggest adding a kdoc, but perhaps the function doesn't have an obvious enough name? Maybe we should call the helper string_is_terminated(), instead?

Sure.

Signature · 2 lines
With Best Regards,
Andy Shevchenko