Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1999147imu; Sat, 22 Dec 2018 09:49:39 -0800 (PST) X-Google-Smtp-Source: ALg8bN7CpmdY0KlrAX0HFGo6fvQ5W4qFnn4j8vXpFRVWANMO4bLxyDd3+YA85D1LxAKtMD9vdcSw X-Received: by 2002:a17:902:1008:: with SMTP id b8mr7000144pla.252.1545500979228; Sat, 22 Dec 2018 09:49:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545500979; cv=none; d=google.com; s=arc-20160816; b=gbMRJFe7hrm9qOxbmD2m0Hc7DxXo+DHc1J45M1nG97v4Qz4EPwvmngspgTbrCJ1c84 ZUhxjtEZ4GG066p5lmEikyxhOrp0B4afi/Jb9nmpwtnttXMNlVFiGZ9x4xzyXeMO2uoj rtb2bXX9GLEnDATeSJ/p7+OoiLyncWXZnkH53XKZCIkj2E0JeSFhRyiFueYtGAvZar4c K0Neq9XkJIVfQ0VIP9qhKv69c9GeRHexPIjjxMYGZ+XqcGnf2QlEdXEw9o0cWFIna1/g jX7BqCWFd2Egpnw8byS/g77YibEnwZl+u6WVize2w9z0RwArSVOntJyXEfLCwJCpeOIC eW9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=QtqdmhHaagko0Q3BA2dL/4EOl1nqfVCFMPGzWUC3X8w=; b=L0peS/6naQjr8QoaFqoNP3R5D6jPKaJrW5fOB5C+4G4D6ODA7BQoc6E0LD0krH2OkI kRLbDnKehB61bG2bDqfV6uQtM91rKWsnR5HR8tTwK4mnh/OsIvSULbBViZQyH48vYtx+ 8HmO0izZM0sTOasoBuP80WXpFM+0TFKiDvo8GPYgpIo6o70E+Yh6fRott2al6D3TO3Q8 qhMrctfUeP0g745Hm31Jlqyo29TL7LI9Iyv3vWDUPXhPLhqJMsaNb418kolEXc4zYfBM xmdXt38aCQaZgtGIRcUls8w62p06NYpVVoOImZ1+dQ6g2/75ZIAHErQx919oBtcvXbwv WwkA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b24si24105585pgg.288.2018.12.22.09.49.24; Sat, 22 Dec 2018 09:49:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390536AbeLUUqw (ORCPT + 99 others); Fri, 21 Dec 2018 15:46:52 -0500 Received: from smtprelay0105.hostedemail.com ([216.40.44.105]:60454 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731379AbeLUUqw (ORCPT ); Fri, 21 Dec 2018 15:46:52 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay02.hostedemail.com (Postfix) with ESMTP id C8BEE21FA; Fri, 21 Dec 2018 20:46:50 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::,RULES_HIT:41:355:379:599:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:1981:2194:2199:2393:2559:2562:2828:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:4605:5007:6119:7875:10004:10400:10848:11026:11232:11658:11914:12296:12740:12760:12895:13069:13161:13229:13311:13357:13439:14659:14721:21063:21080:21451:21627:21740:30029:30051:30054:30070:30091,0,RBL:47.151.153.53:@perches.com:.lbl8.mailshell.net-62.14.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:28,LUA_SUMMARY:none X-HE-Tag: coach68_1e6f4544b801b X-Filterd-Recvd-Size: 3099 Received: from XPS-9350.home (unknown [47.151.153.53]) (Authenticated sender: joe@perches.com) by omf02.hostedemail.com (Postfix) with ESMTPA; Fri, 21 Dec 2018 20:46:49 +0000 (UTC) Message-ID: <077eeb8b09baebe78822819b5f15d671b738a2b2.camel@perches.com> Subject: Re: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro From: Joe Perches To: Steven Rostedt , Linus Torvalds Cc: Linux List Kernel Mailing , Ingo Molnar , Andrew Morton , Namhyung Kim , Masami Hiramatsu , Tom Zanussi , Greg Kroah-Hartman Date: Fri, 21 Dec 2018 12:46:47 -0800 In-Reply-To: <20181221153526.5e6055ca@gandalf.local.home> References: <20181221175618.968519903@goodmis.org> <20181221175659.208858193@goodmis.org> <20181221144054.20bdeb33@gandalf.local.home> <20181221153526.5e6055ca@gandalf.local.home> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.30.1-1build1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2018-12-21 at 15:35 -0500, Steven Rostedt wrote: > At first I thought this would have issues, but with a slight change... > > #define have_prefix(str, prefix) ({ \ > const char *__pfx = (const char *)prefix; \ > > > And the rest the same, it appears to work. > > Need the cast because if for some reason someone passed in something > like "const unsigned char" then it wouldn't work. But that's just a nit. > > So something like this then? > > -- Steve > > diff --git a/include/linux/string.h b/include/linux/string.h [] > @@ -14,6 +14,28 @@ extern void *memdup_user(const void __user *, size_t); > extern void *vmemdup_user(const void __user *, size_t); > extern void *memdup_user_nul(const void __user *, size_t); > > +/** > + * have_prefix - Test if a string has a given prefix There is a naming mismatch of have/has here and has_prefix is probably too generic a name. How about str_has_prefix? > + * @str: The string to test > + * @prefix: The string to see if @str starts with > + * > + * A common way to test a prefix of a string is to do: > + * strncmp(str, prefix, sizeof(prefix) - 1) > + * > + * But this can lead to bugs due to typos, or if prefix is a pointer > + * and not a constant. Instead use has_prefix(). > + * > + * Returns: 0 if @str does not start with @prefix > + strlen(@prefix) if @str does start with @prefix > + */ > +#define has_prefix(str, prefix) \ > + ({ \ > + const char *____prefix____ = (const char *)(prefix); \ > + int ____len____ = strlen(____prefix____); \ > + strncmp(str, ____prefix____, ____len____) == 0 ? \ > + ____len____ : 0; \ > + }) I think all the underscores are unnecessary and confusing.