Skip to content

Commit

Permalink
string_helpers: Move string_is_valid() to the header
Browse files Browse the repository at this point in the history
Move string_is_valid() to the header for wider use.

While at it, rename to string_is_terminated() to be
precise about its semantics.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Simon Horman <simon.horman@corigine.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20230208133153.22528-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Andy Shevchenko authored and Jakub Kicinski committed Feb 10, 2023
1 parent a136391 commit f1db99c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 5 additions & 0 deletions include/linux/string_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct device;
struct file;
struct task_struct;

static inline bool string_is_terminated(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 {
Expand Down
16 changes: 6 additions & 10 deletions net/tipc/netlink_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -445,7 +441,7 @@ static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
return -EINVAL;

len = min_t(int, len, TIPC_MAX_BEARER_NAME);
if (!string_is_valid(b->name, len))
if (!string_is_terminated(b->name, len))
return -EINVAL;

if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
Expand Down Expand Up @@ -486,7 +482,7 @@ static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
return -EINVAL;

len = min_t(int, len, TIPC_MAX_BEARER_NAME);
if (!string_is_valid(name, len))
if (!string_is_terminated(name, len))
return -EINVAL;

if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
Expand Down Expand Up @@ -584,7 +580,7 @@ static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
return -EINVAL;

len = min_t(int, len, TIPC_MAX_LINK_NAME);
if (!string_is_valid(name, len))
if (!string_is_terminated(name, len))
return -EINVAL;

if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
Expand Down Expand Up @@ -819,7 +815,7 @@ static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
return -EINVAL;

len = min_t(int, len, TIPC_MAX_LINK_NAME);
if (!string_is_valid(lc->name, len))
if (!string_is_terminated(lc->name, len))
return -EINVAL;

media = tipc_media_find(lc->name);
Expand Down Expand Up @@ -856,7 +852,7 @@ static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
return -EINVAL;

len = min_t(int, len, TIPC_MAX_LINK_NAME);
if (!string_is_valid(name, len))
if (!string_is_terminated(name, len))
return -EINVAL;

if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
Expand Down

0 comments on commit f1db99c

Please sign in to comment.