From 2682edf205177a9639d2126238d6f83e19fd5d71 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Tue, 5 Dec 2017 12:08:15 +0000 Subject: [PATCH] Fix build using -std=c99 In each place where POSIX/GNU functions are used, the file must declare that it wants POSIX functionality before including any system headers. --- ChangeLog | 1 + library/entropy_poll.c | 5 +++++ library/net_sockets.c | 5 +++++ library/x509.c | 4 ++++ programs/aes/aescrypt2.c | 5 +++++ programs/aes/crypt_and_hash.c | 5 +++++ programs/ssl/ssl_mail_client.c | 5 +++++ tests/CMakeLists.txt | 5 +++++ tests/suites/helpers.function | 1 + 9 files changed, 36 insertions(+) diff --git a/ChangeLog b/ChangeLog index 517381bc5..4d5f5829f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ Changes * Allow overriding the time on Windows via the platform-time abstraction. Fixed by Nick Wilson. * Use gmtime_r/gmtime_s for thread-safety. Fixed by Nick Wilson. + * Fix build using -std=c99. Fixed by Nick Wilson. = mbed TLS 2.11.0 branch released 2018-06-18 diff --git a/library/entropy_poll.c b/library/entropy_poll.c index fd96258ce..31f608b83 100644 --- a/library/entropy_poll.c +++ b/library/entropy_poll.c @@ -19,6 +19,11 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +#if defined(__linux__) +/* Ensure that syscall() is available even when compiling with -std=c99 */ +#define _GNU_SOURCE +#endif + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/library/net_sockets.c b/library/net_sockets.c index 202da0171..4b267cf35 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -19,6 +19,11 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must + * be set before config.h, which pulls in glibc's features.h indirectly. + * Harmless on other platforms. */ +#define _POSIX_C_SOURCE 200112L + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/library/x509.c b/library/x509.c index 906d1714b..b47599b0d 100644 --- a/library/x509.c +++ b/library/x509.c @@ -29,6 +29,10 @@ * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf */ +/* Ensure gmtime_r is available even with -std=c99; must be included before + * config.h, which pulls in glibc's features.h. Harmless on other platforms. */ +#define _XOPEN_SOURCE 500 + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/programs/aes/aescrypt2.c b/programs/aes/aescrypt2.c index 4acf38dd7..36dabe940 100644 --- a/programs/aes/aescrypt2.c +++ b/programs/aes/aescrypt2.c @@ -19,6 +19,11 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +/* Enable definition of fileno() even when compiling with -std=c99. Must be + * set before config.h, which pulls in glibc's features.h indirectly. + * Harmless on other platforms. */ +#define _POSIX_C_SOURCE 1 + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c index 0e272ebe4..49c43b321 100644 --- a/programs/aes/crypt_and_hash.c +++ b/programs/aes/crypt_and_hash.c @@ -20,6 +20,11 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +/* Enable definition of fileno() even when compiling with -std=c99. Must be + * set before config.h, which pulls in glibc's features.h indirectly. + * Harmless on other platforms. */ +#define _POSIX_C_SOURCE 1 + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index 04b847a69..74d5d7270 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -19,6 +19,11 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ +/* Enable definition of hostname() even when compiling with -std=c99. Must be + * set before config.h, which pulls in glibc's features.h indirectly. + * Harmless on other platforms. */ +#define _POSIX_C_SOURCE 200112L + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f630edb83..084da59f4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,11 @@ if(NOT PERL_FOUND) message(FATAL_ERROR "Cannot build test suites without Perl") endif() +# Enable definition of various functions used throughout the testsuite +# (hostname, strdup, fileno...) even when compiling with -std=c99. Harmless +# on non-POSIX platforms. +add_definitions("-D_POSIX_C_SOURCE=200809L") + function(add_test_suite suite_name) if(ARGV1) set(data_name ${ARGV1}) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index f82694ada..8f04885a5 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -36,6 +36,7 @@ typedef UINT32 uint32_t; #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) #include +#include #endif /*----------------------------------------------------------------------------*/