public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nis: Fix stringop-truncation warning with -O3 in nis_local_host.
@ 2023-02-28 12:37 Stefan Liebler
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Liebler @ 2023-02-28 12:37 UTC (permalink / raw)
  To: libc-alpha; +Cc: Stefan Liebler

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);
 	}
     }
 
-- 
2.39.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nis: Fix stringop-truncation warning with -O3 in nis_local_host.
  2023-03-02 10:12 Wilco Dijkstra
@ 2023-03-02 13:26 ` Stefan Liebler
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Liebler @ 2023-03-02 13:26 UTC (permalink / raw)
  To: Wilco Dijkstra; +Cc: 'GNU C Library'

On 02.03.23 11:12, Wilco Dijkstra wrote:
> Hi Stefan,
> 
>> 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.
> 
> Agreed, that is a much more natural way of doing it.
> 
>> Note: This werror was introduced with commit
>> 32c7acd46401530fdbd4e98508c9baaa705f8b53
>> "Replace rawmemchr (s, '\0') with strchr"
> 
> The compiler now uses strlen and so it can do more optimization, including
> giving warnings.
> 
> LGTM
> 
> Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>
> 
> Cheers,
> Wilco
> 
> @@ -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)
> 
> OK
>  	    {
>  	      __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);
>  
> OK

I've just committed the patch.

Thanks,
Stefan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nis: Fix stringop-truncation warning with -O3 in nis_local_host.
@ 2023-03-02 10:12 Wilco Dijkstra
  2023-03-02 13:26 ` Stefan Liebler
  0 siblings, 1 reply; 3+ messages in thread
From: Wilco Dijkstra @ 2023-03-02 10:12 UTC (permalink / raw)
  To: stli; +Cc: 'GNU C Library'

Hi Stefan,

> 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.

Agreed, that is a much more natural way of doing it.

> Note: This werror was introduced with commit
> 32c7acd46401530fdbd4e98508c9baaa705f8b53
> "Replace rawmemchr (s, '\0') with strchr"

The compiler now uses strlen and so it can do more optimization, including
giving warnings.

LGTM

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>

Cheers,
Wilco

@@ -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)

OK
 	    {
 	      __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);
 
OK

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-02 13:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 12:37 [PATCH] nis: Fix stringop-truncation warning with -O3 in nis_local_host Stefan Liebler
2023-03-02 10:12 Wilco Dijkstra
2023-03-02 13:26 ` Stefan Liebler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).