From ec4733b645f8a3402c4e4adf454dab5ae565126a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Apr 2018 14:55:47 +0200 Subject: [PATCH] Make the memset call prior to FD_ZERO conditional to needing it Zeroing out an fd_set before calling FD_ZERO on it is in principle useless, but without it some memory sanitizers think the fd_set is still uninitialized after FD_ZERO (e.g. clang-msan/Glibc/x86_64 where FD_ZERO is implemented in assembly). Make the zeroing conditional on using a memory sanitizer. --- library/net_sockets.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/library/net_sockets.c b/library/net_sockets.c index f99d339ff..7b4a423cc 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -459,11 +459,15 @@ int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout ) if( fd < 0 ) return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); - /* Ensure that memory sanitizers consider - * read_fds and write_fds as initialized even - * if FD_ZERO is implemented in assembly. */ +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) + /* Ensure that memory sanitizers consider read_fds and write_fds as + * initialized even on platforms such as Glibc/x86_64 where FD_ZERO + * is implemented in assembly. */ memset( &read_fds, 0, sizeof( read_fds ) ); memset( &write_fds, 0, sizeof( write_fds ) ); +#endif +#endif FD_ZERO( &read_fds ); if( rw & MBEDTLS_NET_POLL_READ )