From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1962) id 67D113858D33; Thu, 2 Mar 2023 13:24:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67D113858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677763456; bh=x53gHc/qFtcNrSQrOtr+MuJu0FfACf5NUbogo9c23xA=; h=From:To:Subject:Date:From; b=v+0DdO8gA42y9CX+BGaBNyChg3uX0F8TLiJaJ6TX3JzHUBkPVvGOm3hvyrUzYsMNA +6SSXjequ/9Y263UPOQTcOp6itbSiJTT/1CR1setDP6PbYAcrlZAD3k7TV1ArOZbLI /iIlIm2h+tz/6qAlG2yT/DPdaFhFunWP0I6vSw+o= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Stefan Liebler To: glibc-cvs@sourceware.org Subject: [glibc] nis: Fix stringop-truncation warning with -O3 in nis_local_host. X-Act-Checkin: glibc X-Git-Author: Stefan Liebler X-Git-Refname: refs/heads/master X-Git-Oldrev: 3bfdc4e2bceb601b90c81a9baa73c1904db58b2f X-Git-Newrev: 1e0c8356f591a62df9725b6c9387da78002ba412 Message-Id: <20230302132416.67D113858D33@sourceware.org> Date: Thu, 2 Mar 2023 13:24:16 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1e0c8356f591a62df9725b6c9387da78002ba412 commit 1e0c8356f591a62df9725b6c9387da78002ba412 Author: Stefan Liebler Date: Tue Feb 28 13:37:35 2023 +0100 nis: Fix stringop-truncation warning with -O3 in nis_local_host. 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" Reviewed-by: Wilco Dijkstra Diff: --- 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); } }