Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp235431imm; Thu, 12 Jul 2018 18:16:55 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdzO/XzzrmjHBPefcERE8JsFPgIMz/Qw1iAsyy80v0u36gda+5VlLIGqbQ27z7srlYmr4WG X-Received: by 2002:a62:a3d1:: with SMTP id q78-v6mr4730819pfl.5.1531444615016; Thu, 12 Jul 2018 18:16:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531444614; cv=none; d=google.com; s=arc-20160816; b=HyE5QETRVxYj6t1BZForkIV0GsZ9J9SO2Dbjb+7kyf3KGUV/OYhvXhmCraw4Mll/kE 0WRo9Mla67gnAhiZRwFXV92ql5HXlB+cfTQu0JygUXMYsJZG+oZUHqGpuCNSlSOKIhcU QoUzHb8+PlDCbacCHguJSP2CpntbzhopdKUEaaPgEDr4wIMedfSjdez5rDwbXyRu+XGO K3VRdil4uiBJNzedqZipRCip2Yl5goaG9sWcbm0ZwXjaDEd4U6d0rgf1+mPB/oe6qmqo lEO1AB73Y+c5+VpEikBG3r0lGVZ2bg2zeSwUItk+vnZy6y1IFmKlOwM2aNrqORM0uQSg NyZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:arc-authentication-results; bh=4wDEU+1zpukxsv4HTx8fMmytpYSpcwLO+hGdtWd1hrs=; b=rKxnawQyxmI3ZHR8iK29vdI9yymMILzRldV+y+dcTogCjzIi88rnDbYRhYZ88vdv/H pNkAPBGDRMi2ZXI3IZt5VHuiIpaOjqXa2ewJcwjeYs3LN/8yMLVudhnqoq7xiIMfw8Ye FkwlkCPPiCjbQwWrLV891887HbyjmkwRNCilL5luOohO6/75AXTC2X8+JU0evTJNMVY7 IC0610l2VJhvHS4wA8xIp/yhK9t2yt7Fodv4h3dDjJsiKhqdbs3QVrhBdTuefvKnPVJn 0i1fOZUMxw6wAhaet6Jaz/OUueEi1gULfH0abbc9ujoOpF7vBqksN6YAoiPBrghxSl47 9IBg== 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 i4-v6si22090738pgl.435.2018.07.12.18.16.16; Thu, 12 Jul 2018 18:16:54 -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; 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 S2387929AbeGMB1E (ORCPT + 99 others); Thu, 12 Jul 2018 21:27:04 -0400 Received: from nautica.notk.org ([91.121.71.147]:53546 "EHLO nautica.notk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732813AbeGMB1E (ORCPT ); Thu, 12 Jul 2018 21:27:04 -0400 Received: by nautica.notk.org (Postfix, from userid 1001) id BA777C009; Fri, 13 Jul 2018 03:14:52 +0200 (CEST) From: Dominique Martinet Cc: Dominique Martinet , =?UTF-8?q?Ville=20Syrj=C3=A4l=C3=A4?= , Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH 01/18] coccinelle: change strncpy+truncation to strlcpy Date: Fri, 13 Jul 2018 03:14:43 +0200 Message-Id: <1531444483-17338-1-git-send-email-asmadeus@codewreck.org> X-Mailer: git-send-email 1.7.10.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? .../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 +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'; -- 2.17.1