[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <04429ba8051ff804a932cba201d70ca614dc2b49.1444302968.git.sergei@s15v.net>
Date: Thu, 8 Oct 2015 14:32:09 +0300
From: Sergei Zviagintsev <sergei@...v.net>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Daniel Mack <daniel@...que.org>,
David Herrmann <dh.herrmann@...glemail.com>,
Djalal Harouni <tixxdz@...ndz.org>
Cc: linux-kernel@...r.kernel.org, Sergei Zviagintsev <sergei@...v.net>
Subject: [PATCH 40/44] kdbus: Cleanup kdbus_item_validate_name()
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 <sergei@...v.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 majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists