Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4472981ybi; Tue, 30 Jul 2019 02:40:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqzq675q/Rmj0zVC3ErE87Vaj4AHJO8i/EEUsRymwpSWFbaqRgUJ9km/nRa5er7CmxWESOY4 X-Received: by 2002:a63:fe15:: with SMTP id p21mr109454536pgh.149.1564479634572; Tue, 30 Jul 2019 02:40:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564479634; cv=none; d=google.com; s=arc-20160816; b=XlckeFnj0l+HFO/OBKIEoHOXZj+EiJSTPPLHrs/U7iS2dOhlrVd+kfTUG0G8Hqdo4S EHTwx4wHTbgzBOzMlMLY1YwReLdUWG7w24PVw6HnqDpXcbKr6HGc/tvJ+H16XtAdPq0V AUzVxTBkFRbU6cV5K4HBCxwZVPTmvljtf5PZl3aMu4VZRK4pYT8kR0g1+W21rkp9AJyF siCX+//UIhRMSaKAMEXPe9aiZ7UUtTz2EtmoDFebJbuEBWSpOR1UWgxxyuiavEw6Nnku X5lNv9mj0RW5hi0eKjuy7P961rYNMP99n+evUhtDdAP4ZzQBNT2y5a4fR/EFILqDgsis waHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=+uAEwHCS/q5YqbTZfM9UhcffVFTLKHWKlvMKu9AlaN4=; b=Zxjv5YIjcYD7KZdJA5krb9C5aXKGvOdcAOFod0ph5QlKxBWhVUo9z6QSJbAMuvGX42 qFg5PClTMZqc1xMFTw6fjHHlUL+X6gGCmNUeyMAOSSB3BQcmpUa5E/psBV5trgW/Rri5 /2R0Ty6QQIy/6LKbQtswKq1O533dRfy2xuRappDaLo5sqw7GX9NKxtNla75Rufsu91z6 cACF6cjjheGnme75ZwQ2kpDJJkRuIrNUNClnFtaMiok+I0DbdQZj/SuEXN77GQkgI1hZ FK2t7d04ZxnYeu6r+fzb8pN/b5DlMIwknoT8rPxcSScFGnmQ6plNWoFvk7GehB5sh94p KIEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EsXmEMrA; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u12si31513092pfh.6.2019.07.30.02.40.19; Tue, 30 Jul 2019 02:40:34 -0700 (PDT) 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; dkim=pass header.i=@chromium.org header.s=google header.b=EsXmEMrA; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726845AbfG3E0W (ORCPT + 99 others); Tue, 30 Jul 2019 00:26:22 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:38323 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726510AbfG3E0W (ORCPT ); Tue, 30 Jul 2019 00:26:22 -0400 Received: by mail-pg1-f196.google.com with SMTP id f5so20523832pgu.5 for ; Mon, 29 Jul 2019 21:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+uAEwHCS/q5YqbTZfM9UhcffVFTLKHWKlvMKu9AlaN4=; b=EsXmEMrAKLXYhP9zY31O+/h2ZdkuRU0uxUsvt12z/dYH/onDFgVEw8/psz+asNdBYU nFXBcaiag1L68Tv6zpHm7dfIu0NIX/b28PPUvE9EqMHFn//8WEUWABDEeEqYk5Fghedc pEQppssDC1/AlpgweEINkqIDwRAPLQbsEmQA0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=+uAEwHCS/q5YqbTZfM9UhcffVFTLKHWKlvMKu9AlaN4=; b=SMn4Ns2StDV6UVNneNmLfoyNTGcVL75TUKaZxS+fiYzOk0RjPmdzoteoAjnnR1OGQl HHuSDU6bcdPgki+Fq0ZHGJl5leSl2tzGSw4LAolRkWalwBtAra7JxijOzMJVsMYv9bWL KNYQcA4tUDduC49hrsIZMOG+UTlOO6So3UrChhYxXLsPrDtJ9QBbAcWRezWYOUb4BPgr xzHmIdsGBkbNDNorYwBBu8W0kaAO+9dMvnmwbA/es2L6yTzumyYVexJJJmbklVSYIZ7N DSf8LZUkKDeQpz5uzbq5/DERyXRg2xinJZ6/iYHxkQp4yWYpoqCXK8sq9YK6cCEpZiic m4IQ== X-Gm-Message-State: APjAAAWGW8tj1fxtdQbLQVeKTRQhrvdMsrMq3EfS0o9qPfzrOrR8apar MyBuSauqKEKmuhTzpzA2OtnzbQ== X-Received: by 2002:a62:174a:: with SMTP id 71mr41180815pfx.140.1564460781249; Mon, 29 Jul 2019 21:26:21 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id w22sm67693578pfi.175.2019.07.29.21.26.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 29 Jul 2019 21:26:20 -0700 (PDT) Date: Mon, 29 Jul 2019 21:26:19 -0700 From: Kees Cook To: Chuhong Yuan Cc: Tejun Heo , Li Zefan , Johannes Weiner , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Joe Perches , Laura Abbott , Michael Ellerman , Steven Rostedt Subject: Re: [PATCH 01/12] rdmacg: Replace strncmp with str_has_prefix Message-ID: <201907292117.DA40CA7D@keescook> References: <20190729151346.9280-1-hslester96@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190729151346.9280-1-hslester96@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 29, 2019 at 11:13:46PM +0800, Chuhong Yuan wrote: > strncmp(str, const, len) is error-prone. > We had better use newly introduced > str_has_prefix() instead of it. Wait, stop. :) After Laura called my attention to your conversion series, mpe pointed out that str_has_prefix() is almost redundant to strstarts() (from 2009), and the latter has many more users. Let's fix strstarts() match str_has_prefix()'s return behavior (all the existing callers are doing boolean tests, so the change in return value won't matter), and then we can continue with this replacement. (And add some documentation to Documenation/process/deprecated.rst along with a checkpatch.pl test maybe too?) Actually I'd focus first on the actually broken cases first (sizeof() without the "-1", etc): $ git grep strncmp.*sizeof | grep -v -- '-' | wc -l 17 I expect the "copy/paste" changes could just be a Coccinelle script that Linus could run to fix all the cases (and should be added to the kernel source's list of Coccinelle scripts). Especially since the bulk of the usage pattern are doing literals like this: arch/alpha/kernel/setup.c: if (strncmp(p, "mem=", 4) == 0) { $ git grep -E 'strncmp.*(sizeof|, *[0-9]*)' | wc -l 2565 And some cases are weirdly backwards: tools/perf/util/callchain.c: if (!strncmp(tok, "none", strlen(tok))) { -Kees > Signed-off-by: Chuhong Yuan > --- > kernel/cgroup/rdma.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/cgroup/rdma.c b/kernel/cgroup/rdma.c > index ae042c347c64..fd12a227f8e4 100644 > --- a/kernel/cgroup/rdma.c > +++ b/kernel/cgroup/rdma.c > @@ -379,7 +379,7 @@ static int parse_resource(char *c, int *intval) > return -EINVAL; > return i; > } > - if (strncmp(value, RDMACG_MAX_STR, len) == 0) { > + if (str_has_prefix(value, RDMACG_MAX_STR)) { > *intval = S32_MAX; > return i; > } > -- > 2.20.1 > -- Kees Cook