From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S945413AbcJaSez (ORCPT ); Mon, 31 Oct 2016 14:34:55 -0400 Received: from smtprelay0074.hostedemail.com ([216.40.44.74]:57632 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S944968AbcJaSex (ORCPT ); Mon, 31 Oct 2016 14:34:53 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1345:1381:1437:1534:1542:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:4321:4605:5007:6120:6261:7875:10004:10848:11026:11473:11657:11658:11914:12043:12296:12438:12555:13161:13229:14093:14096:14181:14721:14877:21080:21324:21434:21451:30029:30054:30062,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: drink55_81bfbedbc1922 X-Filterd-Recvd-Size: 3109 From: Joe Perches To: linux-kernel@vger.kernel.org Subject: [RFC PATCH] string: Add strzcmp for a length limited strcmp Date: Mon, 31 Oct 2016 11:34:49 -0700 Message-Id: X-Mailer: git-send-email 2.10.0.rc2.1.g053435c Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org strncmp is commonly used when parsing early_param options where a fixed string is matched without the trailing zero. A few errors exist where string comparisons use strncmp and the length of the comparison to perform is incorrectly specified. ie: strncmp(ptr, "string", 5) where 5 should likely have been 6 There are a few of these in the kernel tree like: drivers/net/ethernet/samsung/sxgbe/sxgbe_main.c:2307: if (!strncmp(opt, "eee_timer:", 6)) { sound/firewire/bebob/bebob.c:180: return strncmp(name, "FW Audiophile Bootloader", 15) != 0; Perhaps it would be useful to introduce a function that does a strcmp limited to the length of the 2nd string argument. The second argument for strzcmp requires a char [], not a const char * using a macro and BUILD_BUG_ON. Signed-off-by: Joe Perches --- include/linux/string.h | 7 +++++++ lib/string.c | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 26b6f6a66f83..536d00164b26 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -5,6 +5,7 @@ #include /* for inline */ #include /* for size_t */ #include /* for NULL */ +#include #include #include @@ -44,6 +45,12 @@ extern int strcmp(const char *,const char *); #ifndef __HAVE_ARCH_STRNCMP extern int strncmp(const char *,const char *,__kernel_size_t); #endif +int _strzcmp(const char *, const char *); +#define strzcmp(s1, s2) \ +({ \ + BUILD_BUG_ON(!__same_type(s2, char [])); \ + _strzcmp(s1, s2); \ +}) #ifndef __HAVE_ARCH_STRCASECMP extern int strcasecmp(const char *s1, const char *s2); #endif diff --git a/lib/string.c b/lib/string.c index ed83562a53ae..ca9e4eb26b85 100644 --- a/lib/string.c +++ b/lib/string.c @@ -360,6 +360,24 @@ int strncmp(const char *cs, const char *ct, size_t count) EXPORT_SYMBOL(strncmp); #endif +/** + * _strzcmp - Compare two strings limited to the length of the 2nd string + * @cs: 1st string + * @ct: 2nd string + */ +int _strzcmp(const char *cs, const char *ct) +{ + while (*cs == *ct && *cs != 0) { + cs++; + ct++; + } + if (*ct == 0) + return 0; + + return *(unsigned char *)cs < *(unsigned char *)ct ? -1: 1; +} +EXPORT_SYMBOL(_strzcmp); + #ifndef __HAVE_ARCH_STRCHR /** * strchr - Find the first occurrence of a character in a string -- 2.10.0.rc2.1.g053435c