Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4707388ybi; Tue, 30 Jul 2019 06:49:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqy8lC1kHySmr3cNMhrbOtXBJPZLIsGUmuRP5yYGltbSeFHYH49EPcKmO2BK6IeDC3LWFZVk X-Received: by 2002:a63:755e:: with SMTP id f30mr109796153pgn.246.1564494541666; Tue, 30 Jul 2019 06:49:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564494541; cv=none; d=google.com; s=arc-20160816; b=Jz75LeVLgOqCtoEnYslXgKpincpn2wC4GwACGvkkxuQsLib0TCww10oXN67DsHU+YX q4UNFNUfi5m1zFqozISYx19QJ3TdAdP6CaBi2BcW6Xhqdi2kmVbhestiz8et7et651TN wJVh4tgdC6NyCtP4r9iLH55m+76eR6SDvWi0clCMSc6aqYbVQlQ5W6p+aN97NQLjrSsL sIjR/GSlanGHt9QDfaxGC5FSbq9s0tJdIqqkqkVDscMsDjTI1BfgAML45b3YT6SyKUHa GJzEHujVqY13d/SlZpqcIdrpUaWzYv+A+JUH0EdU62YZ/JM7Gr0OroaKM7Cn53xc2koN 4KAw== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=zYQAxBettPtStNQE5fAZLmFzaIswYZZXO3tKKpC/bAE=; b=ZYw9OVs3o32FiipB5CXn5vebK7WmLmXDpPHEsqocII1NO2xkwbxDcVvCVi9GwsoMEg 314zLyKjz1LO7QSX5RnkdekoUyTztKtdkBTB51v+dZWF8d+8xRmdiftniH1EUih2j2oB ALUGt6m/8/NqNv9iIPKUboO8qr5MF0cxb4EeqkXYO4kXOTbgl8d5JHTTDtU1kPx2al/b 7DEKtefo3hN/dCY4Z0oeCmr2GGjCbf8wHCZbizZL4XAvPHU/drdpvocCYvj1hRXJw9Lq jS7s2QUKISrNfVBguyYNYtI1SH/vGl52AWrCZdcig6U/fk9nF3juAmtlDuH+NBzAaW8d QSFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DLeEOGpA; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i68si30801039pfc.153.2019.07.30.06.48.47; Tue, 30 Jul 2019 06:49:01 -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=@gmail.com header.s=20161025 header.b=DLeEOGpA; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727955AbfG3Gjv (ORCPT + 99 others); Tue, 30 Jul 2019 02:39:51 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46680 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727611AbfG3Gjv (ORCPT ); Tue, 30 Jul 2019 02:39:51 -0400 Received: by mail-ed1-f67.google.com with SMTP id d4so61561943edr.13; Mon, 29 Jul 2019 23:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=zYQAxBettPtStNQE5fAZLmFzaIswYZZXO3tKKpC/bAE=; b=DLeEOGpACpUsFdAW/vzvp2L2aQp+qrinDlxmAofpfkU5tgpTo1pxzGEOqdic1Ogy9y HJa+m1QQSStZdiTADDGujXWZly+2rfiWr1+u2Y3wiRXy0NoTU/eXNGRCinjnsXVTjq8T wDwYVH0kOoh3H+zq39G9QKdd8daFvxZGZsjOBuhSGgLelvedqxGK3SSJGLN6ibCGa0C6 jYRyaswMM6njWmGNHBVt+LOu/NrkIHtmmFfGU8E/aYcKHmd4Y9+H7/GxAkN73qu3mQvm nEHzkCScnQyPqnI44Wnq2Pmo52SBt6UQ8RGZRKwHVuNj+irI/yjDgSBUJjal8th6egaO 3J2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=zYQAxBettPtStNQE5fAZLmFzaIswYZZXO3tKKpC/bAE=; b=qy2AlreTJ4zFvYk8GdZR8SburI6W/ctgc3+O6Fj0DsSnesPg3wIvppS82xLlxwrK04 mD2gHITgodbTDWfkPkksWipUakg43aUvomkLHD4wS61Knd3MbxgdFJKAUofzOKayHfov jzvqmhM2+as2/ZbBPUe9LhVmfowGmVwsE/JUcncz5vu94nH2D8nwmWxUW+i2sOLjT0d4 MZdYSxSHbwLJ3tL3Ck8TLJ5wCpWrQZ5A+pypajeg3F2M8cr8UFScOj/JielMSojVnx8i 7cp+765aWCYsDcyi/3F8kYT4lIgZNP46dZucocVefCyEsF4NotcRLb0NqPmNx0nQJPMT hUhA== X-Gm-Message-State: APjAAAUhmy6zX4d18PGzvaWmxd2M+O0hJoa6cHedBf9gvFiWYfvqJr39 gT8oPWxfTAbHp5eDMdpjUtSzLTu1SxS8s51RlOo= X-Received: by 2002:aa7:ca41:: with SMTP id j1mr101952769edt.149.1564468789374; Mon, 29 Jul 2019 23:39:49 -0700 (PDT) MIME-Version: 1.0 References: <20190729151346.9280-1-hslester96@gmail.com> <201907292117.DA40CA7D@keescook> In-Reply-To: <201907292117.DA40CA7D@keescook> From: Chuhong Yuan Date: Tue, 30 Jul 2019 14:39:40 +0800 Message-ID: Subject: Re: [PATCH 01/12] rdmacg: Replace strncmp with str_has_prefix To: Kees Cook Cc: Tejun Heo , Li Zefan , Johannes Weiner , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Joe Perches , Laura Abbott , Michael Ellerman , Steven Rostedt Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kees Cook =E4=BA=8E2019=E5=B9=B47=E6=9C=8830=E6=97= =A5=E5=91=A8=E4=BA=8C =E4=B8=8B=E5=8D=8812:26=E5=86=99=E9=81=93=EF=BC=9A > > 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?) > Thanks for your advice! Does that mean replacing strstarts()'s implementation with str_has_prefix()'s and then use strstarts() to substitute strncmp? I am not very clear about how to add the test into checkpatch.pl. Should I write a check for this pattern or directly add strncmp into deprecated_apis? > 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: > Actually I am using a Coccinelle script to detect the cases and have found 800+ places of strncmp(str, const, len). But the script still needs some improvement since it has false negatives and only focuses on detecting, not replacement. I can upload it after improvement. In which form should I upload it? In a patch's description or put it in coccinelle scripts? > arch/alpha/kernel/setup.c: if (strncmp(p, "mem=3D", 4) =3D=3D 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 > I think with the help of Coccinelle script, all strncmp(str, const, len) can be replaced and these problems will be eliminated. :) Regards, Chuhong > > 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) =3D=3D 0) { > > + if (str_has_prefix(value, RDMACG_MAX_STR)) { > > *intval =3D S32_MAX; > > return i; > > } > > -- > > 2.20.1 > > > > -- > Kees Cook