How to avoid `warning: passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign [-Wpointer-sign]`?

Building mbedtls for Android gives a few (similar) warnings. Here’s the first.

[ 38%] Built target mbedx509
Scanning dependencies of target mbedtls
[ 38%] Building C object library/CMakeFiles/mbedtls.dir/ssl_cache.c.o
[ 40%] Building C object library/CMakeFiles/mbedtls.dir/debug.c.o
[ 40%] Building C object library/CMakeFiles/mbedtls.dir/net_sockets.c.o
External/.build_area/mbedtls-2.16.3/library/net_sockets.c:332:37: warning: passing 'int *'
      to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with
      different sign [-Wpointer-sign]
                    (void *) &type, &type_len ) != 0 ||
                                    ^~~~~~~~~
/Users/pi/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/sys/socket.h:311:92: note: 
      passing argument to parameter '__value_length' here
__socketcall int getsockopt(int __fd, int __level, int __option, void* __value, socklen_t* __value_length);
                                                                                           ^
External/.build_area/mbedtls-2.16.3/library/net_sockets.c:342:80: warning: passing 'int *'
      to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with
      different sign [-Wpointer-sign]
                                             (struct sockaddr *) &client_addr, &n );

If I inspect External/.build_area/mbedtls-2.16.3/library/net_sockets.c:332:37 I see:

/*
 * Accept a connection from a remote client
 */
int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
                        mbedtls_net_context *client_ctx,
                        void *client_ip, size_t buf_size, size_t *ip_len )
{
    int ret;
    int type;

    struct sockaddr_storage client_addr;

#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) ||  \
    defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
    socklen_t n = (socklen_t) sizeof( client_addr );
    socklen_t type_len = (socklen_t) sizeof( type );
#else
    int n = (int) sizeof( client_addr );
    int type_len = (int) sizeof( type );
#endif

    /* Is this a TCP or UDP socket? */
    if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
                    (void *) &type, &type_len    /* <-- warning is HERE */    ) != 0 ||
        ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
    {
        return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
    }

… It appears that none of those four flags were defined:

#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) ||  \
    defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)

^ These ones.
Does this mean I’m missing something in my build setup?
Should I be passing in one of these flags somehow?
Can anyone help me get traction on this problem?

Hi @pii,
Thank you for reporting this issue!

Note that this definition was intended to distinguish between Linux type API, where socklen_t is defined, and Windows, where it isn’t.

Note that the signature of getsockopt on Linux is:

int getsockopt(int sockfd, int level, int optname,               void *optval,
socklen_t *optlen);

and on Windows:

int getsockopt(
  SOCKET s,
  int    level,
  int    optname,
  char   *optval,
  int    *optlen
);

Android has `socklen_t defined, but either with a different precompilation flag, or without any flag protection.

I would suggest you check in your toolchain’s socket.h if and how socklen_t is defined, and if possible, add it to the precompilation check. I have raised an issue in the repository to track this.
Regards,
Mbed TLS Support
Ron