Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp490784imm; Fri, 13 Jul 2018 00:45:53 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfDTXqcND1xaCfEU1dnn+pHB+h8CqVG5FjEJZ5jg+g8PQZGlJ3PQg+kylmKxWDUM1XH5tuO X-Received: by 2002:a63:231a:: with SMTP id j26-v6mr5011304pgj.138.1531467953163; Fri, 13 Jul 2018 00:45:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531467953; cv=none; d=google.com; s=arc-20160816; b=j/HosfcnfyfOAnYAqqWmPCqqHQcuXUh+s63nSQ0Rmc0hiOfqkG5gScZBnQKrvwTM8N 1+0+yyJ3g580LdzQ8HW8M2zgy41t/OSIk75+ONJpr566VHWmH5yBuscWHD/vaef8y8WC 6UuEZ1Trz25ZVx2COhLVt4lyYyZ+fLDT4xXvs2O/Ls+e/NRkXmWJ4FmWNTyaIlFGfxXJ dbihPxRsfKBx6qSg8D5Mr1gtYInbr+pm0VhaI8FpbMlXwHd/e0wrTvLxva3rOwzS9Iwu 412qOQp5iwDcdlS06KG46iYRgTGYwy0esg8NLelg2UgghQJavgDzS3iK61vb0NCKTDWr Bnfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=+fUhs1UPGwhlu61NEreLBTKrzD+cqQJaqAbjn3fRy0o=; b=WGG2+EN0GawSeKX8mEpq5q2gxI7MqhqeEmBSCiC/alPIjKmMF+0v1kY2OPWOM+i8CF z4K4jBjqjWSpm1Hd8QhS741zkxNU5tNZHfWIuujP8q07ZKdMjoXT47vXgO2F92Uer0oV +tGzMS4LyCn3vvQzYwuj5H0c2sdUCyTqZLOfnLm64+woqEnCktJYHHyEA+eUBYCwCLX4 GuYd8ugaD2NQRxQtJlwKMizrtsiAeTfxdo86r7OYtS9QftOVZNJdijobV3eHvk7CrSuT Mi1F6MEd8WBBHrtx/6Y7fm5myhHLGFq9P/GF0PBUJ+0GBKV7bSDvkANB/kyDT7culuMO C2EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=asRpaNAp; 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 1-v6si23506249plz.379.2018.07.13.00.45.38; Fri, 13 Jul 2018 00:45:53 -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=asRpaNAp; 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 S1729781AbeGMH62 (ORCPT + 99 others); Fri, 13 Jul 2018 03:58:28 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:37454 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727582AbeGMH62 (ORCPT ); Fri, 13 Jul 2018 03:58:28 -0400 Received: by mail-pf0-f193.google.com with SMTP id a26-v6so91334pfo.4 for ; Fri, 13 Jul 2018 00:45:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=+fUhs1UPGwhlu61NEreLBTKrzD+cqQJaqAbjn3fRy0o=; b=asRpaNApEPN8bEOz2UEqsyshIIoyjaa1B5+L/zsM1WKnSHbSnxfgyiwAPK/US4T2lh B466XNAPNNxbl/rz5RBeinBd4V5T0bGpDERGJTymt56JaQYI51QAdj87t+XkxHkVu+z3 h9R+qKOC1T9NRmn3zb+MBRNnfzmXnn4unZ6fFv8NCfzdkWsgrHDA4RhIijf7yI35b8Et VinYYDl3lM1OJ3bG7UxgnG3VsTVb6y7GWK1NeCRo9M+B1UBpNMOuxXTzWQuG6R2s0eS9 vIyNLaVh1r9nbFWepmaS2UvcUxGpbOZQNIORI5WA552ahWPH/miad+k9xpNAilQxwOES Tm1w== 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:content-transfer-encoding :in-reply-to:user-agent; bh=+fUhs1UPGwhlu61NEreLBTKrzD+cqQJaqAbjn3fRy0o=; b=aVt4gyO87yW7zoDaT92SuEds8fJD7SIvzSNIIU5jPFzuiBpQyJuBX2kZC2ms/RpaP3 W0GMUBnKAxkJBkoiWTA+uOfO7UKENXmMrl6EljrR5YT7iEesqadAuoW1fKqjtUKaPsgh dOtP9osvdNv+aWABE34uLkcLIxqYbqU5WGbq15P/czHBkdpIhkY22tInI4T+ZB3qts+C 9tmvdnA3jLkyGSvYNKnZJmS7MtsRjJKwTnJRlXFZNro9Knc31EWddq9QKQBKKsGzpmmy JxruT37OLL59XcUyR7/irVvWp9FOP0idfz5YgdRuDIa3da4uLsqXeHYbalUIA4deYvT5 cCIg== X-Gm-Message-State: AOUpUlGOo7pkgXxpXDcx/9YOxKWRiBqASmqA7JJe3E/36tFxBnMJUTpA 48sq7SrprEBrPeegkBjfpXo= X-Received: by 2002:a63:8648:: with SMTP id x69-v6mr5066834pgd.172.1531467902745; Fri, 13 Jul 2018 00:45:02 -0700 (PDT) Received: from himanshu-Vostro-3559 ([103.233.116.134]) by smtp.gmail.com with ESMTPSA id 10-v6sm52951460pfs.111.2018.07.13.00.44.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Jul 2018 00:45:02 -0700 (PDT) Date: Fri, 13 Jul 2018 13:14:55 +0530 From: Himanshu Jha To: Dominique Martinet Cc: Michal Marek , Nicolas Palix , linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr, Ville =?iso-8859-1?Q?Syrj=E4l=E4?= Subject: Re: [Cocci] [PATCH 01/18] coccinelle: change strncpy+truncation to strlcpy Message-ID: <20180713074455.GA6859@himanshu-Vostro-3559> References: <1531444483-17338-1-git-send-email-asmadeus@codewreck.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1531444483-17338-1-git-send-email-asmadeus@codewreck.org> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 13, 2018 at 03:14:43AM +0200, Dominique Martinet wrote: > Besides being simpler, using strlcpy instead of strncpy+truncation > fixes part of the following class of new gcc warnings: > > drivers/gpu/drm/i915/intel_tv.c: In function ‘intel_tv_get_modes’: > drivers/gpu/drm/i915/intel_tv.c:1358:3: error: ‘strncpy’ specified bound 32 equals > destination size [-Werror=stringop-truncation] > strncpy(mode_ptr->name, input->name, DRM_DISPLAY_MODE_LEN); > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > cc1: all warnings being treated as errors > > Note that this is not a proper fix for this warning (and not all of the > occurences give the warning either - the strings are not always static). > The warning was intended to have developers check the return code of > strncpy and act in case of truncation (print a warning, abort the > function or something similar if the original string was not nul > terminated); the change to strlcpy only works because gcc does not > handle the function the same way. > > Suggested-by: Ville Syrjälä > Signed-off-by: Dominique Martinet > --- > > Running this fixes 30 occurences of the problem in 17 different > components of the kernel, and while the produced patches are fairly > straight-forward I'm not sure who I should expect to pick this up as > it is sent as a series. > I expect each maintainer will pick their share of the patchs if they > agree with it and the rest will just be dropped? Masahiro Yamada takes coccinelle patches, so please cc him or your patch would be lost. > .../coccinelle/misc/strncpy_truncation.cocci | 41 +++++++++++++++++++ > 1 file changed, 41 insertions(+) > create mode 100644 scripts/coccinelle/misc/strncpy_truncation.cocci > > diff --git a/scripts/coccinelle/misc/strncpy_truncation.cocci b/scripts/coccinelle/misc/strncpy_truncation.cocci > new file mode 100644 > index 000000000000..28b5c2a290ac > --- /dev/null > +++ b/scripts/coccinelle/misc/strncpy_truncation.cocci > @@ -0,0 +1,41 @@ > +/// Use strlcpy rather than strncpy(dest,..,sz) + dest[sz-1] = '\0' > +/// > +// Confidence: High > +// Comments: > +// Options: --no-includes --include-headers > + > +virtual patch > +virtual context You might consider adding context rule or remove this line perhaps ? > +virtual report > +virtual org > + > +@r@ > +expression dest, src, sz; > +position p; > +@@ > + > +strncpy@p(dest, src, sz); > +dest[sz - 1] = '\0'; > + > +@script:python depends on org@ > +p << r.p; > +@@ > + > +cocci.print_main("strncpy followed by truncation can be strlcpy",p) > + > +@script:python depends on report@ > +p << r.p; > +@@ > + > +msg = "SUGGESTION: strncpy followed by truncation can be strlcpy" > +coccilib.report.print_report(p[0],msg) > + > +@ok depends on patch@ > +expression r.dest, r.src, r.sz; > +position r.p; > +@@ > + > +-strncpy@p( > ++strlcpy( > + dest, src, sz); > +-dest[sz - 1] = '\0'; The above rule produces an output that I think is not correct: -------------------------------------------------------------- diff = diff -u -p a//ti/wl1251/acx.c b//ti/wl1251/acx.c --- a//ti/wl1251/acx.c +++ b//ti/wl1251/acx.c @@ -150,14 +150,7 @@ int wl1251_acx_fw_version(struct wl1251 } /* be careful with the buffer sizes */ - strncpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version))); - - /* - * if the firmware version string is exactly - * sizeof(rev->fw_version) long or fw_len is less than - * sizeof(rev->fw_version) it won't be null terminated - */ - buf[min(len, sizeof(rev->fw_version)) - 1] = '\0'; + strlcpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version))); ----------------------------------------------------------------- I think the comment is useful and should not be removed. Also, consider changing Confidence level appropriately. Thanks. -- Himanshu Jha Undergraduate Student Department of Electronics & Communication Guru Tegh Bahadur Institute of Technology