socket: Use offsetof in SUN_LEN (bug 29578)

Message ID mvmh713qy9m.fsf@suse.de
State Committed
Commit c15318bfc5499d835c434a4d98b688aa2d928412
Headers
Series socket: Use offsetof in SUN_LEN (bug 29578) |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Andreas Schwab Sept. 19, 2022, 1:39 p.m. UTC
  Use offsetof instead of its traditional expansion in the definition of the
SUN_LEN macro, to avoid a warning from the undefined behaviour sanitizer.
---
 socket/sys/un.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Adhemerval Zanella Netto Oct. 6, 2022, 5:36 p.m. UTC | #1
On 19/09/22 10:39, Andreas Schwab via Libc-alpha wrote:
> Use offsetof instead of its traditional expansion in the definition of the
> SUN_LEN macro, to avoid a warning from the undefined behaviour sanitizer.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  socket/sys/un.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/socket/sys/un.h b/socket/sys/un.h
> index 454eafebc9..2b11300e28 100644
> --- a/socket/sys/un.h
> +++ b/socket/sys/un.h
> @@ -34,10 +34,11 @@ struct sockaddr_un
>  
>  
>  #ifdef __USE_MISC
> +# include <stddef.h>
>  # include <string.h>		/* For prototype of `strlen'.  */
>  
>  /* Evaluate to actual length of the `sockaddr_un' structure.  */
> -# define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path)	      \
> +# define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path)		      \
>  		      + strlen ((ptr)->sun_path))
>  #endif
>
  

Patch

diff --git a/socket/sys/un.h b/socket/sys/un.h
index 454eafebc9..2b11300e28 100644
--- a/socket/sys/un.h
+++ b/socket/sys/un.h
@@ -34,10 +34,11 @@  struct sockaddr_un
 
 
 #ifdef __USE_MISC
+# include <stddef.h>
 # include <string.h>		/* For prototype of `strlen'.  */
 
 /* Evaluate to actual length of the `sockaddr_un' structure.  */
-# define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path)	      \
+# define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path)		      \
 		      + strlen ((ptr)->sun_path))
 #endif