From patchwork Tue Feb 28 12:37:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Liebler X-Patchwork-Id: 65763 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 320063850875 for ; Tue, 28 Feb 2023 12:38:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 320063850875 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677587887; bh=ND93/zEaXLwgnYMEefWYF7dNs7owj8mu/YJyL6NgXMk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=A0/P6ezpGPQy4MbCCGsIZc59CcUH+NQNz3058mM2i2BLppLdEuAQnnZBOKOdY6BYs Sy2WecmmLnGk7Aqeu7RTF+YcsfyUIB33ALuXwd0ymasy2WTQDTkdX6V/EFns6tFdKK hAwqJQBsdeIMaR8ZIyal4rvbZtFRzKtjNq81b1YM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 62C633858422 for ; Tue, 28 Feb 2023 12:37:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 62C633858422 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31SC8AgQ015315 for ; Tue, 28 Feb 2023 12:37:43 GMT Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3p1hbh8r7x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 Feb 2023 12:37:42 +0000 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 31RJUZJx019520 for ; Tue, 28 Feb 2023 12:37:41 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma03ams.nl.ibm.com (PPS) with ESMTPS id 3nybcub32k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 Feb 2023 12:37:40 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 31SCbbk351970454 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Feb 2023 12:37:37 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5C63320043; Tue, 28 Feb 2023 12:37:37 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3936D20040; Tue, 28 Feb 2023 12:37:37 +0000 (GMT) Received: from t35lp56.lnxne.boe (unknown [9.152.108.100]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 28 Feb 2023 12:37:37 +0000 (GMT) To: libc-alpha@sourceware.org Cc: Stefan Liebler Subject: [PATCH] nis: Fix stringop-truncation warning with -O3 in nis_local_host. Date: Tue, 28 Feb 2023 13:37:35 +0100 Message-Id: <20230228123735.1058696-1-stli@linux.ibm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: O4vb3KBBmola5P-GGCEFEkDznePV9POz X-Proofpoint-ORIG-GUID: O4vb3KBBmola5P-GGCEFEkDznePV9POz X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-02-28_08,2023-02-28_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 lowpriorityscore=0 malwarescore=0 mlxscore=0 clxscore=1015 suspectscore=0 bulkscore=0 spamscore=0 adultscore=0 impostorscore=0 priorityscore=1501 mlxlogscore=822 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302280101 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Stefan Liebler via Libc-alpha From: Stefan Liebler Reply-To: Stefan Liebler Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When building with -O3 on s390x/x86_64, I get this stringop-truncation warning which leads to a build fail: In function ‘nis_local_host’, inlined from ‘nis_local_host’ at nis_local_names.c:147:1: nis_local_names.c:171:11: error: ‘strncpy’ output may be truncated copying between 0 and 1023 bytes from a string of length 1024 [-Werror=stringop-truncation] 171 | strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can just ignore this warning as the hostname + '.' + directory-name + '\0' always fits in __nishostname with length of (NIS_MAXNAMELEN + 1) as there is the runtime check above. Furthermore as we already know the length of the directory-name, we can also just use memcpy to copy the directory-name inclusive the NUL-termination. Note: This werror was introduced with commit 32c7acd46401530fdbd4e98508c9baaa705f8b53 "Replace rawmemchr (s, '\0') with strchr" --- nis/nis_local_names.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nis/nis_local_names.c b/nis/nis_local_names.c index e685255300..699ca04e66 100644 --- a/nis/nis_local_names.c +++ b/nis/nis_local_names.c @@ -161,15 +161,19 @@ nis_local_host (void) if (cp[-1] == '.') return __nishostname; - if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN) + nis_name local_directory = nis_local_directory (); + size_t local_directory_len = strlen (local_directory); + if (len + 1 + local_directory_len > NIS_MAXNAMELEN) { __nishostname[0] = '\0'; return __nishostname; } + /* We have enough space in __nishostname with length of + (NIS_MAXNAMELEN + 1) for + hostname + '.' + directory-name + '\0'. */ *cp++ = '.'; - strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1); - __nishostname[NIS_MAXNAMELEN] = '\0'; + memcpy (cp, local_directory, local_directory_len + 1); } }