From e8e6bc2c50cf2ae0fc46ab46c7ca490c327764e2 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sat, 27 Sep 2025 22:15:54 +0200 Subject: [PATCH] build: improve building in ISO and C89 modes, test in CI Note: On Linux with glibc, ISO mode also requires setting the `_DEFAULT_SOURCE` macro to enable necessary POSIX features when building tests (for `popen()`, `pclose()` and examples (for `fileno()`, `tcsetattr()`). On other platforms and environments it may require something else. - replace `strdup()` with local implementation. - replace `getline()` with C89 `fgets()`. - replace `u_char`, `u_int` with C89 types. - fix type mismatches in chacha-poly1305 code. - silence `-Wno-format-nonliteral` locally. The global method was not effective (like because it was overridden by `-Wformat=2`) and caused these warnings when building in C89 mode. - GHA: test ISO C89 in zlib-enabled Linux jobs with all crypto backends. - fix formatting nits along the way. - configure, cmake: silence silly `-Wno-comma` warnings with clang in C89 mode. - examples: include `select.h` again, where `select()` is used. As its canonical header. Seen required on Linux glibc non-GNU/ISO builds. Follow-up to a683133dfe96de126194f58f183131a84c7d36a2 #999 Assisted-by: Raul Tambre Ref: #1674 Closes #1688 --- .github/workflows/ci.yml | 11 +++++++- acinclude.m4 | 14 +++++----- cmake/PickyWarnings.cmake | 8 +++++- example/direct_tcpip.c | 3 +++ example/scp_nonblock.c | 3 +++ example/scp_write_nonblock.c | 3 +++ example/sftp_RW_nonblock.c | 3 +++ example/sftp_nonblock.c | 3 +++ example/sftp_write_nonblock.c | 3 +++ example/sftp_write_sliding.c | 3 +++ example/ssh2_agent_forwarding.c | 3 +++ example/ssh2_echo.c | 3 +++ example/ssh2_exec.c | 3 +++ example/tcpip-forward.c | 3 +++ example/x11.c | 3 +++ src/chacha.c | 17 ++++--------- src/chacha.h | 13 ++++++---- src/cipher-chachapoly.c | 26 +++++++++++-------- src/cipher-chachapoly.h | 12 ++++----- src/crypt.c | 8 +++--- src/libssh2_setup.h | 3 +-- src/misc.c | 7 +++++ src/poly1305.c | 7 ++--- src/poly1305.h | 5 ++-- tests/openssh_fixture.c | 45 +++++++++++++++++++++++++-------- tests/openssh_fixture.h | 2 ++ tests/session_fixture.c | 2 +- 27 files changed, 151 insertions(+), 65 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84db7ce1..aef0065b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -296,6 +296,7 @@ jobs: CC: ${{ matrix.compiler == 'clang-tidy' && 'clang' || matrix.compiler }} MATRIX_ARCH: '${{ matrix.arch }}' MATRIX_BUILD: '${{ matrix.build }}' + MATRIX_COMPILER: '${{ matrix.compiler }}' MATRIX_CRYPTO: '${{ matrix.crypto }}' MATRIX_ZLIB: '${{ matrix.zlib }}' FIXTURE_TRACE_ALL_CONNECT: 0 @@ -518,9 +519,17 @@ jobs: crypto="${MATRIX_CRYPTO}" fi [ -d "$HOME"/usr ] && options+=" -DCMAKE_PREFIX_PATH=$HOME/usr" - [ "${MATRIX_ARCH}" = 'i386' ] && options+=" -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_VERSION=1 -DCMAKE_SYSTEM_PROCESSOR=${MATRIX_ARCH} -DCMAKE_C_FLAGS=-m32" + if [ "${MATRIX_ARCH}" = 'i386' ]; then + options+=" -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_VERSION=1 -DCMAKE_SYSTEM_PROCESSOR=${MATRIX_ARCH}" + cflags+=' -m32' + fi [ "${MATRIX_COMPILER}" = 'clang-tidy' ] && options+=' -DLIBSSH2_CLANG_TIDY=ON' + if [[ "${MATRIX_CRYPTO}" =~ ^(OpenSSL|Libgcrypt|mbedTLS|wolfSSL)$ && "${MATRIX_COMPILER}" != 'clang-tidy' && "${MATRIX_ZLIB}" = 'ON' ]]; then + options+=' -DCMAKE_C_STANDARD=90' + cflags+=' -D_DEFAULT_SOURCE' # for glibc ISO mode, in tests: popen(), pclose(), in examples: fileno(), tcsetattr() + fi cmake -B bld -G Ninja ${options} $TOOLCHAIN_OPTION \ + -DCMAKE_C_FLAGS="${cflags}" \ -DCMAKE_UNITY_BUILD=ON \ -DENABLE_WERROR=ON \ -DCRYPTO_BACKEND=${crypto} \ diff --git a/acinclude.m4 b/acinclude.m4 index b507a449..b2219b51 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -233,7 +233,6 @@ AC_DEFUN([CURL_CC_DEBUG_OPTS], CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [sign-compare]) tmp_CFLAGS="$tmp_CFLAGS -Wno-multichar" CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [undef]) - tmp_CFLAGS="$tmp_CFLAGS -Wno-format-nonliteral" CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [endif-labels strict-prototypes]) CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [declaration-after-statement]) CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [cast-align]) @@ -338,6 +337,14 @@ AC_DEFUN([CURL_CC_DEBUG_OPTS], CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [xor-used-as-pow]) fi + case "$CFLAGS" in + *-std=c89*|*-std=c90*|*-std=gnu89*|*-std=gnu90*) + if test "$compiler_num" -ge "309"; then + tmp_CFLAGS="$tmp_CFLAGS -Wno-comma" # Just silly + fi + ;; + esac + CFLAGS="$CFLAGS $tmp_CFLAGS" AC_MSG_NOTICE([Added this set of compiler options: $tmp_CFLAGS]) @@ -411,11 +418,6 @@ AC_DEFUN([CURL_CC_DEBUG_OPTS], CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [undef]) fi # - dnl Only gcc 2.97 or later - if test "$compiler_num" -ge "297"; then - tmp_CFLAGS="$tmp_CFLAGS -Wno-format-nonliteral" - fi - # dnl Only gcc 3.0 or later if test "$compiler_num" -ge "300"; then tmp_CFLAGS="$tmp_CFLAGS -Wno-system-headers" diff --git a/cmake/PickyWarnings.cmake b/cmake/PickyWarnings.cmake index c3dd1c8e..ac63b8fa 100644 --- a/cmake/PickyWarnings.cmake +++ b/cmake/PickyWarnings.cmake @@ -91,7 +91,6 @@ if(PICKY_COMPILER) -Wignored-qualifiers # clang 2.8 gcc 4.3 -Wmissing-field-initializers # clang 2.7 gcc 4.1 -Wmissing-noreturn # clang 2.7 gcc 4.1 - -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0) -Wno-system-headers # clang 1.0 gcc 3.0 # -Wpadded # clang 2.9 gcc 4.1 # Not used: We cannot change public structs -Wold-style-definition # clang 2.7 gcc 3.4 @@ -289,6 +288,13 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC) endforeach() endif() +if(CMAKE_C_STANDARD STREQUAL 90) + if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 3.9) OR + (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1)) + list(APPEND _picky "-Wno-comma") # Just silly + endif() +endif() + if(_picky_nocheck OR _picky) set(_picky_tmp "${_picky_nocheck}" "${_picky}") string(REPLACE ";" " " _picky_tmp "${_picky_tmp}") diff --git a/example/direct_tcpip.c b/example/direct_tcpip.c index 67a188d5..17f930ea 100644 --- a/example/direct_tcpip.c +++ b/example/direct_tcpip.c @@ -15,6 +15,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/scp_nonblock.c b/example/scp_nonblock.c index 72ca34c1..43e91b89 100644 --- a/example/scp_nonblock.c +++ b/example/scp_nonblock.c @@ -20,6 +20,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index 5a09ec0e..ef681dc8 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -11,6 +11,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index 5d1171e7..a5141aa1 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -21,6 +21,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/sftp_nonblock.c b/example/sftp_nonblock.c index 36c1c209..3c6144de 100644 --- a/example/sftp_nonblock.c +++ b/example/sftp_nonblock.c @@ -21,6 +21,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index 78c34ca7..938a19c6 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -17,6 +17,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/sftp_write_sliding.c b/example/sftp_write_sliding.c index 99cc5318..431f04c9 100644 --- a/example/sftp_write_sliding.c +++ b/example/sftp_write_sliding.c @@ -17,6 +17,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/ssh2_agent_forwarding.c b/example/ssh2_agent_forwarding.c index 789d116a..b154f087 100644 --- a/example/ssh2_agent_forwarding.c +++ b/example/ssh2_agent_forwarding.c @@ -19,6 +19,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/ssh2_echo.c b/example/ssh2_echo.c index ff5b1c58..f81d653c 100644 --- a/example/ssh2_echo.c +++ b/example/ssh2_echo.c @@ -14,6 +14,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/ssh2_exec.c b/example/ssh2_exec.c index 62c3468a..03bdae98 100644 --- a/example/ssh2_exec.c +++ b/example/ssh2_exec.c @@ -16,6 +16,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/tcpip-forward.c b/example/tcpip-forward.c index 62d94997..5934a79f 100644 --- a/example/tcpip-forward.c +++ b/example/tcpip-forward.c @@ -15,6 +15,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/example/x11.c b/example/x11.c index 2a14d3b0..bfa76342 100644 --- a/example/x11.c +++ b/example/x11.c @@ -23,6 +23,9 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/src/chacha.c b/src/chacha.c index 262fb140..78994e99 100644 --- a/src/chacha.c +++ b/src/chacha.c @@ -13,11 +13,6 @@ /* $OpenBSD: chacha.c,v 1.1 2013/11/21 00:45:44 djm Exp $ */ -typedef unsigned char u8; -typedef unsigned int u32; - -typedef struct chacha_ctx chacha_ctx; - #define U8C(v) (v##U) #define U32C(v) (v##U) @@ -55,8 +50,7 @@ typedef struct chacha_ctx chacha_ctx; static const char sigma[17] = "expand 32-byte k"; static const char tau[17] = "expand 16-byte k"; -void -chacha_keysetup(chacha_ctx *x, const u8 *k, u32 kbits) +void chacha_keysetup(struct chacha_ctx *x, const u8 *k, u32 kbits) { const char *constants; @@ -81,8 +75,7 @@ chacha_keysetup(chacha_ctx *x, const u8 *k, u32 kbits) x->input[3] = U8TO32_LITTLE(constants + 12); } -void -chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter) +void chacha_ivsetup(struct chacha_ctx *x, const u8 *iv, const u8 *counter) { x->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0); x->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4); @@ -90,14 +83,14 @@ chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter) x->input[15] = U8TO32_LITTLE(iv + 4); } -void -chacha_encrypt_bytes(chacha_ctx *x, const u8 *m, u8 *c, u32 bytes) +void chacha_encrypt_bytes(struct chacha_ctx *x, const u8 *m, u8 *c, + size_t bytes) { u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; u8 *ctarget = NULL; u8 tmp[64]; - u_int i; + size_t i; if(!bytes) return; diff --git a/src/chacha.h b/src/chacha.h index 0a2ac817..d98a473e 100644 --- a/src/chacha.h +++ b/src/chacha.h @@ -14,8 +14,11 @@ #include +typedef unsigned char u8; +typedef unsigned int u32; + struct chacha_ctx { - u_int input[16]; + u32 input[16]; }; #define CHACHA_MINKEYLEN 16 @@ -24,9 +27,9 @@ struct chacha_ctx { #define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN) #define CHACHA_BLOCKLEN 64 -void chacha_keysetup(struct chacha_ctx *x, const u_char *k, u_int kbits); -void chacha_ivsetup(struct chacha_ctx *x, const u_char *iv, const u_char *ctr); -void chacha_encrypt_bytes(struct chacha_ctx *x, const u_char *m, - u_char *c, u_int bytes); +void chacha_keysetup(struct chacha_ctx *x, const u8 *k, u32 kbits); +void chacha_ivsetup(struct chacha_ctx *x, const u8 *iv, const u8 *ctr); +void chacha_encrypt_bytes(struct chacha_ctx *x, const u8 *m, + u8 *c, size_t bytes); #endif /* CHACHA_H */ diff --git a/src/cipher-chachapoly.c b/src/cipher-chachapoly.c index 9e4b1170..0ce717df 100644 --- a/src/cipher-chachapoly.c +++ b/src/cipher-chachapoly.c @@ -28,7 +28,8 @@ int chachapoly_timingsafe_bcmp(const void *b1, const void *b2, size_t n); int -chachapoly_init(struct chachapoly_ctx *ctx, const u_char *key, u_int keylen) +chachapoly_init(struct chachapoly_ctx *ctx, const unsigned char *key, + size_t keylen) { if(keylen != (32 + 32)) /* 2 x 256 bit keys */ return LIBSSH2_ERROR_INVAL; @@ -47,12 +48,15 @@ chachapoly_init(struct chachapoly_ctx *ctx, const u_char *key, u_int keylen) * tag. This tag is written on encryption and verified on decryption. */ int -chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, - const u_char *src, u_int len, u_int aadlen, int do_encrypt) +chachapoly_crypt(struct chachapoly_ctx *ctx, libssh2_uint64_t seqnr, + unsigned char *dest, const unsigned char *src, size_t len, + size_t aadlen, int do_encrypt) { - u_char seqbuf[8]; - const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ - u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; + unsigned char seqbuf[8]; + const unsigned char one[8] = { + 1, 0, 0, 0, 0, 0, 0, 0 /* NB little-endian */ + }; + unsigned char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; int r = LIBSSH2_ERROR_INVAL; unsigned char *ptr = NULL; @@ -69,7 +73,7 @@ chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, /* If decrypting, check tag before anything else */ if(!do_encrypt) { - const u_char *tag = src + aadlen + len; + const unsigned char *tag = src + aadlen + len; poly1305_auth(expected_tag, src, aadlen + len, poly_key); if(chachapoly_timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) @@ -106,10 +110,10 @@ out: /* Decrypt and extract the encrypted packet length */ int chachapoly_get_length(struct chachapoly_ctx *ctx, unsigned int *plenp, - unsigned int seqnr, const unsigned char *cp, - unsigned int len) + libssh2_uint64_t seqnr, const unsigned char *cp, + size_t len) { - u_char buf[4], seqbuf[8]; + unsigned char buf[4], seqbuf[8]; unsigned char *ptr = NULL; if(len < 4) @@ -130,5 +134,5 @@ chachapoly_timingsafe_bcmp(const void *b1, const void *b2, size_t n) for(; n > 0; n--) ret |= *p1++ ^ *p2++; - return (ret != 0); + return ret != 0; } diff --git a/src/cipher-chachapoly.h b/src/cipher-chachapoly.h index 4d0015cb..cc247818 100644 --- a/src/cipher-chachapoly.h +++ b/src/cipher-chachapoly.h @@ -30,12 +30,12 @@ struct chachapoly_ctx { }; int chachapoly_init(struct chachapoly_ctx *cpctx, - const u_char *key, u_int keylen); -int chachapoly_crypt(struct chachapoly_ctx *cpctx, u_int seqnr, - u_char *dest, const u_char *src, u_int len, u_int aadlen, - int do_encrypt); + const unsigned char *key, size_t keylen); +int chachapoly_crypt(struct chachapoly_ctx *cpctx, libssh2_uint64_t seqnr, + unsigned char *dest, const unsigned char *src, size_t len, + size_t aadlen, int do_encrypt); int chachapoly_get_length(struct chachapoly_ctx *cpctx, - u_int *plenp, u_int seqnr, const u_char *cp, - u_int len); + unsigned int *plenp, libssh2_uint64_t seqnr, + const unsigned char *cp, size_t len); #endif /* CHACHA_POLY_AEAD_H */ diff --git a/src/crypt.c b/src/crypt.c index e1c650fe..626fbb13 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -443,13 +443,13 @@ crypt_encrypt_chacha20_poly_buffer(LIBSSH2_SESSION * session, length */ ret = chachapoly_crypt(&ctx->chachapoly_ctx, seqno, buf, buf, - ((u_int)buf_len) - 4, 4, ctx->encrypt); + buf_len - 4, 4, ctx->encrypt); } else { /* buf is full packet including size and auth tag but buf_len doesn't include size */ ret = chachapoly_crypt(&ctx->chachapoly_ctx, seqno, buf, buf, - ((u_int)buf_len), 4, ctx->encrypt); + buf_len, 4, ctx->encrypt); /* the api expects the size field to already be removed from the decrypted packet so we'll help it out */ @@ -459,7 +459,7 @@ crypt_encrypt_chacha20_poly_buffer(LIBSSH2_SESSION * session, } } - return (ret == 0 ? 0 : 1); + return (ret == 0) ? 0 : 1; } static int @@ -472,7 +472,7 @@ crypt_get_length_chacha20_poly(LIBSSH2_SESSION * session, unsigned int seqno, (void)session; return chachapoly_get_length(&ctx->chachapoly_ctx, len, seqno, data, - (u_int)data_size); + data_size); } static int diff --git a/src/libssh2_setup.h b/src/libssh2_setup.h index c01bc47a..c99b31ad 100644 --- a/src/libssh2_setup.h +++ b/src/libssh2_setup.h @@ -75,7 +75,7 @@ # if !defined(LIBSSH2_LIBRARY) || defined(LIBSSH2_TESTS) /* apply to examples and tests only */ # ifndef _CRT_NONSTDC_NO_DEPRECATE -# define _CRT_NONSTDC_NO_DEPRECATE /* for strdup(), write() */ +# define _CRT_NONSTDC_NO_DEPRECATE /* for write() */ # endif # ifndef _WINSOCK_DEPRECATED_NO_WARNINGS # define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */ @@ -89,7 +89,6 @@ # endif # endif # if _MSC_VER < 1900 -# define strdup _strdup /* Silence bogus warning C4127: conditional expression is constant */ # pragma warning(disable:4127) # endif diff --git a/src/misc.c b/src/misc.c index 565beb3e..a0183db4 100644 --- a/src/misc.c +++ b/src/misc.c @@ -610,7 +610,14 @@ _libssh2_debug_low(LIBSSH2_SESSION * session, int context, const char *format, buflen -= len; msglen = len; va_start(vargs, format); +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif len = vsnprintf(buffer + msglen, buflen, format, vargs); +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif va_end(vargs); msglen += len < buflen ? len : buflen - 1; } diff --git a/src/poly1305.c b/src/poly1305.c index 5cbe038e..595439db 100644 --- a/src/poly1305.c +++ b/src/poly1305.c @@ -28,9 +28,10 @@ (p)[3] = (uint8_t)((v) >> 24); \ } while (0) -void -poly1305_auth(unsigned char out[POLY1305_TAGLEN], const unsigned char *m, - size_t inlen, const unsigned char key[POLY1305_KEYLEN]) { +void poly1305_auth(unsigned char out[POLY1305_TAGLEN], + const unsigned char *m, size_t inlen, + const unsigned char key[POLY1305_KEYLEN]) +{ uint32_t t0; uint32_t t1; uint32_t t2; diff --git a/src/poly1305.h b/src/poly1305.h index 21efca93..eee22a1b 100644 --- a/src/poly1305.h +++ b/src/poly1305.h @@ -14,7 +14,8 @@ #define POLY1305_KEYLEN 32 #define POLY1305_TAGLEN 16 -void poly1305_auth(u_char out[POLY1305_TAGLEN], const u_char *m, size_t inlen, - const u_char key[POLY1305_KEYLEN]); +void poly1305_auth(unsigned char out[POLY1305_TAGLEN], + const unsigned char *m, size_t inlen, + const unsigned char key[POLY1305_KEYLEN]); #endif /* POLY1305_H */ diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 6f47ac8a..01201b95 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -97,7 +97,14 @@ static int run_command_varg(char **output, const char *command, va_list args) } /* Format the command string */ +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif ret = vsnprintf(command_buf, sizeof(command_buf), command, args); +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif if(ret < 0 || ret >= BUFSIZ) { fprintf(stderr, "Unable to format command (%s)\n", command); return -1; @@ -142,7 +149,7 @@ static int run_command_varg(char **output, const char *command, va_list args) buf[end - 1] = '\0'; } - *output = strdup(buf); + *output = libssh2_strdup(buf); } return ret; } @@ -212,7 +219,7 @@ static int start_openssh_server(char **container_id_out) "libssh2/openssh_server"); } else { - *container_id_out = strdup(""); + *container_id_out = libssh2_strdup(""); return 0; } } @@ -237,24 +244,22 @@ static int is_running_inside_a_container(void) #ifdef _WIN32 return 0; #else - const char *cgroup_filename = "/proc/self/cgroup"; + static const char *cgroup_filename = "/proc/self/cgroup"; FILE *f; - char *line = NULL; - size_t len = 0; + char line[256]; int found = 0; f = fopen(cgroup_filename, "r"); if(!f) { /* Don't go further, we are not in a container */ return 0; } - while(getline(&line, &len, f) != -1) { + while(fgets(line, sizeof(line), f)) { if(strstr(line, "docker")) { found = 1; break; } } fclose(f); - free(line); return found; #endif } @@ -320,7 +325,7 @@ static int ip_address_from_container(char *container_id, char **ip_address_out) static int port_from_container(char *container_id, char **port_out) { if(is_running_inside_a_container()) { - *port_out = strdup("22"); + *port_out = libssh2_strdup("22"); return 0; } else { @@ -364,12 +369,12 @@ static libssh2_socket_t open_socket_to_container(char *container_id) if(!env) { env = "127.0.0.1"; } - ip_address = strdup(env); + ip_address = libssh2_strdup(env); env = openssh_server_port(); if(!env) { env = "4711"; } - port_string = strdup(env); + port_string = libssh2_strdup(env); } /* 0.0.0.0 is returned by Docker for Windows, because the container @@ -377,7 +382,7 @@ static libssh2_socket_t open_socket_to_container(char *container_id) instead we assume localhost and try to connect to 127.0.0.1. */ if(ip_address && strcmp(ip_address, "0.0.0.0") == 0) { free(ip_address); - ip_address = strdup("127.0.0.1"); + ip_address = libssh2_strdup("127.0.0.1"); } hostaddr = inet_addr(ip_address); @@ -487,3 +492,21 @@ void close_socket_to_openssh_server(libssh2_socket_t sock) { close_socket_to_container(sock); } + +char *libssh2_strdup(const char *str) +{ + size_t len; + char *newstr; + + if(!str) + return (char *)NULL; + + len = strlen(str) + 1; + + newstr = malloc(len); + if(!newstr) + return (char *)NULL; + + memcpy(newstr, str, len); + return newstr; +} diff --git a/tests/openssh_fixture.h b/tests/openssh_fixture.h index eb076a14..9c58d739 100644 --- a/tests/openssh_fixture.h +++ b/tests/openssh_fixture.h @@ -46,4 +46,6 @@ libssh2_socket_t open_socket_to_openssh_server(void); void close_socket_to_openssh_server(libssh2_socket_t sock); int openssh_fixture_have_docker(void); +char *libssh2_strdup(const char *str); + #endif diff --git a/tests/session_fixture.c b/tests/session_fixture.c index 70645c73..e84e3db6 100644 --- a/tests/session_fixture.c +++ b/tests/session_fixture.c @@ -309,7 +309,7 @@ static void kbd_callback(const char *name, int name_len, } if(num_prompts == 1) { - responses[0].text = strdup(kbd_password); + responses[0].text = libssh2_strdup(kbd_password); responses[0].length = (unsigned int)strlen(kbd_password); } }