Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2509705imu; Sun, 23 Dec 2018 01:09:32 -0800 (PST) X-Google-Smtp-Source: ALg8bN535bpp9j3FGvtBKPyMl2IwkKuwLfTXwkztuJTPcMtVFDua7N6l79YuD+GwiEYp2maRB1DZ X-Received: by 2002:a24:738f:: with SMTP id y137mr6089213itb.136.1545556172572; Sun, 23 Dec 2018 01:09:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545556172; cv=none; d=google.com; s=arc-20160816; b=EIN41L8WGEGErf1XeAFk4wSeOSlkH08iFYmQLLvjou41fefxYAi5Lq3MlwJwP5awAi tjTFMlCXQ0uzxMoMSRJwEFawHz25INUzrgDocorx4s+aRaOEB70hYn/aiXVk2qzeOZDr MJgbeNa4lfSYbP9d8ckQEtPKoasiQZ9oghRaJostA426uXAsfJDlb2ZSBQC/SOrdTFCC QD6mKBvvqtKKVgbTr2jiYq/V9kWiv/drt9D2Yt30nUQpN+ZvrrOJyaWoxrlyeoQArf6E O6RKBYKOy6ls4Kz2A+sU5khtzwtn+oTzgnueOyl9/oBNQvCcfawRWLEfossCq4jnypM/ 6SFA== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=skHK5EhcYjtddilQbRJhD4r4X4ieFV6u7AhFIt1V+eM=; b=mNXaxW3sHGBe/xdBJfUZiF8a5CgIgKYgb2JsPsr1pg/WIiAUmkicorXduo3PxRLlGp OeKALdN0qSKURlUoZeq+U88t+yqKusnU2p/rWynI/WwORMu3zrKpTEOwQiadCq7e7/RN RPL591GHVISVFJPlP6wvITqVqj80/zyMaHXEECSf+GiVo34vBnOJ/uPkDCU7eNn/IhbQ /AIkFEprQ/WVY5kxXejyCD4Johx/SCefzkIdqUUIJdrFzrdi5tEwqZ0jac0vyDhFq8ay EETgiYxdXEDQOBm3mcItwcMTaUfJDewPfpJ+cfAM0TJ3Vt5rzzQwyZisoEi7L/H2LS2f Ta0w== 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 u4si3526746itj.4.2018.12.23.01.09.04; Sun, 23 Dec 2018 01:09:32 -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 S2391879AbeLUUzR (ORCPT + 99 others); Fri, 21 Dec 2018 15:55:17 -0500 Received: from mail.kernel.org ([198.145.29.99]:60134 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391870AbeLUUzR (ORCPT ); Fri, 21 Dec 2018 15:55:17 -0500 Received: from gandalf.local.home (cpe-66-24-56-78.stny.res.rr.com [66.24.56.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E99A621929; Fri, 21 Dec 2018 20:55:14 +0000 (UTC) Date: Fri, 21 Dec 2018 15:55:13 -0500 From: Steven Rostedt To: Linus Torvalds Cc: Linux List Kernel Mailing , Ingo Molnar , Andrew Morton , Namhyung Kim , Masami Hiramatsu , Joe Perches , Tom Zanussi , Greg Kroah-Hartman Subject: Re: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro Message-ID: <20181221155513.11450ca6@gandalf.local.home> In-Reply-To: References: <20181221175618.968519903@goodmis.org> <20181221175659.208858193@goodmis.org> <20181221144054.20bdeb33@gandalf.local.home> <20181221153526.5e6055ca@gandalf.local.home> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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, 21 Dec 2018 12:41:17 -0800 Linus Torvalds wrote: > Parentheses.... ? -- Steve > > On Fri, Dec 21, 2018, 12:35 Steven Rostedt > > On Fri, 21 Dec 2018 12:01:48 -0800 > > Linus Torvalds wrote: > > > > > On Fri, Dec 21, 2018 at 11:40 AM Steven Rostedt > > wrote: > > > > > > > > OK, what about if we just use strlen() and say that this macro is not > > > > safe for parameters with side effects. > > > > > > I think gcc follows simple assignments just fine, and does the > > > optimized strlen() for them too. > > > > > > So why not just do > > > > > > #define have_prefix(str,prefix) ({ \ > > > const char *__pfx = prefix; \ > > > size_t __pfxlen = strlen(__pfx); \ > > > strncmp(str, __pfx, __pfxlen) ? 0 : __pfxlen); }) > > > > > > and be done with it safely? > > > > > > The above is ENTIRELY untested. > > > > > > > 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 > > index 27d0482e5e05..4586fee60194 100644 > > --- 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 > > + * @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; \ > > + }) > > + > > /* > > * Include machine specific inline routines > > */ > >