Calculate length of the string directly from item's payload size and
simplify tests. Do not use strlen(), as kdbus_str_valid() guarantees
that '\0' is located in the end of provided buffer. When examining
contents of the string, use pointer instead of array index to stay more
concise and readable.

Signed-off-by: Sergei Zviagintsev <ser...@s15v.net>
---
 ipc/kdbus/item.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/ipc/kdbus/item.c b/ipc/kdbus/item.c
index ce78dba03426..e6b6e2ca7e05 100644
--- a/ipc/kdbus/item.c
+++ b/ipc/kdbus/item.c
@@ -37,32 +37,26 @@ static bool kdbus_str_valid(const char *str, size_t size)
  */
 int kdbus_item_validate_name(const struct kdbus_item *item)
 {
-       const char *name = item->str;
-       unsigned int i;
-       size_t len;
+       const char *p, *name = item->str;
+       size_t len = KDBUS_ITEM_PAYLOAD_SIZE(item) - 1;
 
-       if (item->size < KDBUS_ITEM_HEADER_SIZE + 2)
+       if (len > item->size || len < 1)
                return -EINVAL;
 
-       if (item->size > KDBUS_ITEM_HEADER_SIZE +
-                        KDBUS_SYSNAME_MAX_LEN + 1)
+       if (len > KDBUS_SYSNAME_MAX_LEN)
                return -ENAMETOOLONG;
 
-       if (!kdbus_str_valid(name, KDBUS_ITEM_PAYLOAD_SIZE(item)))
+       if (!kdbus_str_valid(name, len + 1))
                return -EINVAL;
 
-       len = strlen(name);
-       if (len == 0)
-               return -EINVAL;
-
-       for (i = 0; i < len; i++) {
-               if (isalpha(name[i]))
+       for (p = name; *p; ++p) {
+               if (isalpha(*p))
                        continue;
-               if (isdigit(name[i]))
+               if (isdigit(*p))
                        continue;
-               if (name[i] == '_')
+               if (*p == '_')
                        continue;
-               if (i > 0 && i + 1 < len && (name[i] == '-' || name[i] == '.'))
+               if (p > name && *(p + 1) && (*p == '-' || *p == '.'))
                        continue;
 
                return -EINVAL;
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to