diff options
author | Stefan Liebler <stli@linux.ibm.com> | 2023-02-28 13:37:35 +0100 |
---|---|---|
committer | Stefan Liebler <stli@linux.ibm.com> | 2023-03-02 14:22:54 +0100 |
commit | 1e0c8356f591a62df9725b6c9387da78002ba412 (patch) | |
tree | b0934c68c884057e777a189edffabeedd9834bcb | |
parent | 3bfdc4e2bceb601b90c81a9baa73c1904db58b2f (diff) | |
download | glibc-1e0c8356f591a62df9725b6c9387da78002ba412.zip glibc-1e0c8356f591a62df9725b6c9387da78002ba412.tar.gz glibc-1e0c8356f591a62df9725b6c9387da78002ba412.tar.bz2 |
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 <Wilco.Dijkstra@arm.com>
-rw-r--r-- | nis/nis_local_names.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/nis/nis_local_names.c b/nis/nis_local_names.c index e685255..699ca04 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); } } |