From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932566AbbJHLer (ORCPT ); Thu, 8 Oct 2015 07:34:47 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:42427 "EHLO out5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932495AbbJHLel (ORCPT ); Thu, 8 Oct 2015 07:34:41 -0400 X-Sasl-enc: k+JtXVPisETPfWcFRHQ40rpd51HcZNm98D5gYF/098j8 1444304078 From: Sergei Zviagintsev To: Greg Kroah-Hartman , Daniel Mack , David Herrmann , Djalal Harouni Cc: linux-kernel@vger.kernel.org, Sergei Zviagintsev Subject: [PATCH 40/44] kdbus: Cleanup kdbus_item_validate_name() Date: Thu, 8 Oct 2015 14:32:09 +0300 Message-Id: <04429ba8051ff804a932cba201d70ca614dc2b49.1444302968.git.sergei@s15v.net> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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