1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-23 16:21:00 +03:00

build: prepare builds for clang-cl, add cmake ossfuzz support

- cmake: add support to build ossfuzz.
  Enable with `-DBUILD_OSSFUZZ=ON`.
  Also supports `-DLIB_FUZZING_ENGINE=` like autotools does.
- check for `__clang__` when suppressing warnings in source. Necessary
  for clang-cl, which set `__clang__`, but doesn't set `__GNU__`.
- cmake: optimize out 4 picky warning option detections with gcc.
- cmake: bring `-pedantic-error`, `-Wall` use closer to curl's.
- cmake: set `-Wno-language-extension-token` for clang-cl.
- cmake: escape only the necessary `-W` options for clang-cl.
- cmake: apply picky warnings to C++.
- cmake: replace `unset(VAR)` with `set(VAR "")` for init.
- cmake: prefer dash-style MSVC options.
- cmake: simplify `MATCHES` expression.
- cmake: formatting/whitespace.
- ci/GHA: bump `actions/upload-artifact` to v4

Closes #1524
This commit is contained in:
Viktor Szakats
2024-10-30 00:59:03 +01:00
parent af5c9fb88f
commit 854cfa8292
22 changed files with 158 additions and 131 deletions

View File

@ -31,7 +31,7 @@ jobs:
dry-run: false dry-run: false
language: c language: c
- name: Upload Crash - name: Upload Crash
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v4
if: ${{ failure() && steps.build.outcome == 'success' }} if: ${{ failure() && steps.build.outcome == 'success' }}
with: with:
name: artifacts name: artifacts

View File

@ -122,8 +122,9 @@ endif()
option(BUILD_EXAMPLES "Build libssh2 examples" ON) option(BUILD_EXAMPLES "Build libssh2 examples" ON)
option(BUILD_TESTING "Build libssh2 test suite" ON) option(BUILD_TESTING "Build libssh2 test suite" ON)
option(BUILD_OSSFUZZ "Build libssh2 OSS-Fuzz" OFF)
if(NOT BUILD_STATIC_LIBS AND NOT BUILD_SHARED_LIBS) if((NOT BUILD_STATIC_LIBS AND NOT BUILD_SHARED_LIBS) OR BUILD_OSSFUZZ)
set(BUILD_STATIC_LIBS ON) set(BUILD_STATIC_LIBS ON)
endif() endif()
@ -149,7 +150,7 @@ if(HIDE_SYMBOLS)
(CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0) OR (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0) OR
(CMAKE_C_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1)) (CMAKE_C_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1))
set(LIB_SHARED_C_FLAGS "-fvisibility=hidden") set(LIB_SHARED_C_FLAGS "-fvisibility=hidden")
set(LIBSSH2_API "__attribute__ ((__visibility__ (\"default\")))") set(LIBSSH2_API "__attribute__((__visibility__(\"default\")))")
elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0) elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0)
set(LIB_SHARED_C_FLAGS "-xldscope=hidden") set(LIB_SHARED_C_FLAGS "-xldscope=hidden")
set(LIBSSH2_API "__global") set(LIBSSH2_API "__global")
@ -228,7 +229,7 @@ endif()
# duration of these tests. # duration of these tests.
if(MSVC AND ENABLE_WERROR) if(MSVC AND ENABLE_WERROR)
cmake_push_check_state() cmake_push_check_state()
set(CMAKE_REQUIRED_FLAGS "/WX-") set(CMAKE_REQUIRED_FLAGS "-WX-")
endif() endif()
if(HAVE_SYS_TIME_H) if(HAVE_SYS_TIME_H)

View File

@ -3,46 +3,47 @@
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
set(_picky "")
option(ENABLE_WERROR "Turn compiler warnings into errors" OFF) option(ENABLE_WERROR "Turn compiler warnings into errors" OFF)
option(PICKY_COMPILER "Enable picky compiler options" ON) option(PICKY_COMPILER "Enable picky compiler options" ON)
if(ENABLE_WERROR) if(ENABLE_WERROR)
if(MSVC) if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -WX")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -WX")
else() # llvm/clang and gcc style options else() # llvm/clang and gcc style options
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif() endif()
if(((CMAKE_COMPILER_IS_GNUCC AND
NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
CMAKE_C_COMPILER_ID MATCHES "Clang"))
list(APPEND _picky "-pedantic-errors")
endif()
endif() endif()
if(MSVC) if(MSVC)
# Use the highest warning level for Visual Studio. # Use the highest warning level for Visual Studio.
if(PICKY_COMPILER) if(CMAKE_C_FLAGS MATCHES "[/-]W[0-4]")
if(CMAKE_CXX_FLAGS MATCHES "[/-]W[0-4]") string(REGEX REPLACE "[/-]W[0-4]" "-W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REGEX REPLACE "[/-]W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") else()
else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W4")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
if(CMAKE_C_FLAGS MATCHES "[/-]W[0-4]")
string(REGEX REPLACE "[/-]W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
endif()
endif() endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_ID MATCHES "Clang") if(CMAKE_CXX_FLAGS MATCHES "[/-]W[0-4]")
string(REGEX REPLACE "[/-]W[0-4]" "-W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# https://clang.llvm.org/docs/DiagnosticsReference.html else()
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W4")
if(NOT CMAKE_CXX_FLAGS MATCHES "-Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
endif()
if(NOT CMAKE_C_FLAGS MATCHES "-Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
endif() endif()
endif()
if(PICKY_COMPILER) if(PICKY_COMPILER)
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_ID MATCHES "Clang")
# https://clang.llvm.org/docs/DiagnosticsReference.html
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# _picky_enable = Options we want to enable as-is. # _picky_enable = Options we want to enable as-is.
# _picky_detect = Options we want to test first and enable if available. # _picky_detect = Options we want to test first and enable if available.
@ -55,15 +56,9 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
endif() endif()
list(APPEND _picky_enable list(APPEND _picky_enable
-pedantic -Wall -pedantic
) )
if(ENABLE_WERROR)
list(APPEND _picky_enable
-pedantic-errors
)
endif()
# ---------------------------------- # ----------------------------------
# Add new options here, if in doubt: # Add new options here, if in doubt:
# ---------------------------------- # ----------------------------------
@ -119,29 +114,29 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
-Wvla # clang 2.8 gcc 4.3 -Wvla # clang 2.8 gcc 4.3
) )
set(_picky_common
-Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
-Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
-Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
-Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
)
if(CMAKE_C_COMPILER_ID MATCHES "Clang") if(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND _picky_enable list(APPEND _picky_enable
${_picky_common_old} ${_picky_common_old}
-Wshift-sign-overflow # clang 2.9 -Wshift-sign-overflow # clang 2.9
-Wshorten-64-to-32 # clang 1.0 -Wshorten-64-to-32 # clang 1.0
-Wlanguage-extension-token # clang 3.0
-Wformat=2 # clang 3.0 gcc 4.8 -Wformat=2 # clang 3.0 gcc 4.8
) )
if(NOT MSVC)
list(APPEND _picky_enable
-Wlanguage-extension-token # clang 3.0
)
endif()
# Enable based on compiler version # Enable based on compiler version
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3)) (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
list(APPEND _picky_enable list(APPEND _picky_enable
${_picky_common} -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
# -Wunreachable-code-break # clang 3.5 appleclang 6.0 # Not used: Silent in "unity" builds -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
-Wheader-guard # clang 3.4 appleclang 5.1 -Wheader-guard # clang 3.4 appleclang 5.1
-Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
-Wsometimes-uninitialized # clang 3.2 appleclang 4.6 -Wsometimes-uninitialized # clang 3.2 appleclang 4.6
# -Wunreachable-code-break # clang 3.5 appleclang 6.0 # Not used: Silent in "unity" builds
-Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
) )
endif() endif()
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
@ -165,9 +160,6 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
) )
endif() endif()
else() # gcc else() # gcc
list(APPEND _picky_detect
${_picky_common}
)
# Enable based on compiler version # Enable based on compiler version
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
list(APPEND _picky_enable list(APPEND _picky_enable
@ -175,8 +167,8 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
-Wclobbered # gcc 4.3 -Wclobbered # gcc 4.3
-Wmissing-parameter-type # gcc 4.3 -Wmissing-parameter-type # gcc 4.3
-Wold-style-declaration # gcc 4.3 -Wold-style-declaration # gcc 4.3
-Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
-Wstrict-aliasing=3 # gcc 4.0 -Wstrict-aliasing=3 # gcc 4.0
-Wtrampolines # gcc 4.3
) )
endif() endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
@ -186,7 +178,9 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
endif() endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
list(APPEND _picky_enable list(APPEND _picky_enable
-Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
-Wformat=2 # clang 3.0 gcc 4.8 -Wformat=2 # clang 3.0 gcc 4.8
-Wtrampolines # gcc 4.6
) )
endif() endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
@ -201,6 +195,7 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
-fdelete-null-pointer-checks -fdelete-null-pointer-checks
-Wshift-negative-value # clang 3.7 gcc 6.0 (clang default) -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
-Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow) -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
-Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
) )
endif() endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
@ -216,14 +211,13 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
list(APPEND _picky_enable list(APPEND _picky_enable
-Warith-conversion # gcc 10.0 -Warith-conversion # gcc 10.0
-Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
) )
endif() endif()
endif() endif()
# #
unset(_picky)
foreach(_ccopt IN LISTS _picky_enable) foreach(_ccopt IN LISTS _picky_enable)
list(APPEND _picky "${_ccopt}") list(APPEND _picky "${_ccopt}")
endforeach() endforeach()
@ -239,24 +233,28 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
list(APPEND _picky "${_ccopt}") list(APPEND _picky "${_ccopt}")
endif() endif()
endforeach() endforeach()
# clang-cl
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
if(CMAKE_VERSION VERSION_LESS 3.12)
set(_picky_tmp "")
foreach(_ccopt IN LISTS _picky)
list(APPEND _picky_tmp "/clang:${_ccopt}")
endforeach()
set(_picky ${_picky_tmp})
else()
list(TRANSFORM _picky PREPEND "/clang:")
endif()
endif()
if(_picky)
string(REPLACE ";" " " _picky "${_picky}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_picky}")
message(STATUS "Picky compiler options: ${_picky}")
endif()
endif() endif()
endif() endif()
# clang-cl
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
list(APPEND _picky "-Wno-language-extension-token") # Allow __int64
set(_picky_tmp "")
foreach(_ccopt IN LISTS _picky)
# Prefix -Wall, otherwise clang-cl interprets it as an MSVC option and translates it to -Weverything
if(_ccopt MATCHES "^-W" AND NOT _ccopt STREQUAL "-Wall")
list(APPEND _picky_tmp ${_ccopt})
else()
list(APPEND _picky_tmp "-clang:${_ccopt}")
endif()
endforeach()
set(_picky ${_picky_tmp})
endif()
if(_picky)
string(REPLACE ";" " " _picky "${_picky}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_picky}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_picky}")
message(STATUS "Picky compiler options: ${_picky}")
endif()

View File

@ -248,12 +248,12 @@ int main(int argc, char *argv[])
for(;;) { for(;;) {
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(forwardsock, &fds); FD_SET(forwardsock, &fds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
tv.tv_sec = 0; tv.tv_sec = 0;
@ -263,12 +263,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to select().\n"); fprintf(stderr, "failed to select().\n");
goto shutdown; goto shutdown;
} }
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
if(rc && FD_ISSET(forwardsock, &fds)) { if(rc && FD_ISSET(forwardsock, &fds)) {
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
len = recv(forwardsock, buf, sizeof(buf), 0); len = recv(forwardsock, buf, sizeof(buf), 0);

View File

@ -64,12 +64,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -48,12 +48,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -56,12 +56,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
@ -258,13 +258,13 @@ int main(int argc, char *argv[])
FD_ZERO(&fd); FD_ZERO(&fd);
FD_ZERO(&fd2); FD_ZERO(&fd2);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(sock, &fd); FD_SET(sock, &fd);
FD_SET(sock, &fd2); FD_SET(sock, &fd2);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
@ -329,13 +329,13 @@ int main(int argc, char *argv[])
FD_ZERO(&fd); FD_ZERO(&fd);
FD_ZERO(&fd2); FD_ZERO(&fd2);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(sock, &fd); FD_SET(sock, &fd);
FD_SET(sock, &fd2); FD_SET(sock, &fd2);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -65,12 +65,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -54,12 +54,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -55,12 +55,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -50,12 +50,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -47,12 +47,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -51,12 +51,12 @@ static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
FD_ZERO(&fd); FD_ZERO(&fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(socket_fd, &fd); FD_SET(socket_fd, &fd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -246,12 +246,12 @@ int main(int argc, char *argv[])
for(;;) { for(;;) {
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(forwardsock, &fds); FD_SET(forwardsock, &fds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
tv.tv_sec = 0; tv.tv_sec = 0;
@ -261,12 +261,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to select().\n"); fprintf(stderr, "failed to select().\n");
goto shutdown; goto shutdown;
} }
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
if(rc && FD_ISSET(forwardsock, &fds)) { if(rc && FD_ISSET(forwardsock, &fds)) {
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
ssize_t nwritten; ssize_t nwritten;

View File

@ -210,12 +210,12 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, libssh2_socket_t sock)
timeval_out.tv_usec = 0; timeval_out.tv_usec = 0;
FD_ZERO(&set); FD_ZERO(&set);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(sock, &set); FD_SET(sock, &set);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
@ -415,12 +415,12 @@ int main(int argc, char *argv[])
for(;;) { for(;;) {
FD_ZERO(&set); FD_ZERO(&set);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(fileno(stdin), &set); FD_SET(fileno(stdin), &set);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -49,7 +49,7 @@ endif()
## Options ## Options
unset(_libssh2_definitions) set(_libssh2_definitions "")
option(CLEAR_MEMORY "Enable clearing of memory before being freed" ON) option(CLEAR_MEMORY "Enable clearing of memory before being freed" ON)
if(NOT CLEAR_MEMORY) if(NOT CLEAR_MEMORY)
@ -76,8 +76,8 @@ list(APPEND LIBSSH2_LIBS ${LIBSSH2_LIBS_SOCKET})
list(APPEND libssh2_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}) list(APPEND libssh2_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR})
if(MSVC) if(MSVC)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Zi -Od")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /DEBUG") set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -DEBUG")
endif() endif()
## Sources ## Sources
@ -100,7 +100,7 @@ if(WIN32 AND (BUILD_STATIC_LIBS OR BUILD_STATIC_FOR_TESTS) AND BUILD_SHARED_LIBS
set(STATIC_LIB_SUFFIX "_static") set(STATIC_LIB_SUFFIX "_static")
endif() endif()
unset(_libssh2_export) set(_libssh2_export "")
# we want it to be called libssh2 on all platforms # we want it to be called libssh2 on all platforms
if(BUILD_STATIC_LIBS OR BUILD_STATIC_FOR_TESTS) if(BUILD_STATIC_LIBS OR BUILD_STATIC_FOR_TESTS)
@ -212,7 +212,7 @@ endif()
set(_ldflags "") set(_ldflags "")
# Avoid getting unnecessary -L options for known system directories. # Avoid getting unnecessary -L options for known system directories.
unset(_sys_libdirs) set(_sys_libdirs "")
foreach(_libdir IN LISTS CMAKE_SYSTEM_PREFIX_PATH) foreach(_libdir IN LISTS CMAKE_SYSTEM_PREFIX_PATH)
if(_libdir MATCHES "/$") if(_libdir MATCHES "/$")
set(_libdir "${_libdir}lib") set(_libdir "${_libdir}lib")
@ -237,7 +237,7 @@ foreach(_libdir IN LISTS LIBSSH2_LIBDIRS)
endif() endif()
endforeach() endforeach()
unset(_implicit_libs) set(_implicit_libs "")
if(NOT MINGW AND NOT UNIX) if(NOT MINGW AND NOT UNIX)
set(_implicit_libs ${CMAKE_C_IMPLICIT_LINK_LIBRARIES}) set(_implicit_libs ${CMAKE_C_IMPLICIT_LINK_LIBRARIES})
endif() endif()
@ -259,7 +259,7 @@ foreach(_lib IN LISTS _implicit_libs LIBSSH2_LIBS)
endif() endif()
if(_lib MATCHES "^-") if(_lib MATCHES "^-")
list(APPEND _ldflags "${_lib}") list(APPEND _ldflags "${_lib}")
elseif(_lib MATCHES ".*/.*") elseif(_lib MATCHES "/")
# This gets a bit more complex, because we want to specify the # This gets a bit more complex, because we want to specify the
# directory separately, and only once per directory # directory separately, and only once per directory
get_filename_component(_libdir ${_lib} DIRECTORY) get_filename_component(_libdir ${_lib} DIRECTORY)

View File

@ -45,7 +45,7 @@
/* FIXME: Disable warnings for 'src' */ /* FIXME: Disable warnings for 'src' */
#if !defined(LIBSSH2_TESTS) && !defined(LIBSSH2_WARN_SIGN_CONVERSION) #if !defined(LIBSSH2_TESTS) && !defined(LIBSSH2_WARN_SIGN_CONVERSION)
#ifdef __GNUC__ #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
#endif #endif

View File

@ -41,7 +41,7 @@
#define LIBSSH2_CRYPTO_ENGINE libssh2_mbedtls #define LIBSSH2_CRYPTO_ENGINE libssh2_mbedtls
#ifdef __GNUC__ #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
/* mbedTLS (as of v3.5.1) has a `[-Werror=arith-conversion]` /* mbedTLS (as of v3.5.1) has a `[-Werror=arith-conversion]`
warning in its public headers. */ warning in its public headers. */
@ -73,7 +73,7 @@
#include <mbedtls/pk.h> #include <mbedtls/pk.h>
#include <mbedtls/error.h> #include <mbedtls/error.h>
#ifdef __GNUC__ #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@ -675,12 +675,12 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time)
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) { if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) {
FD_ZERO(&rfd); FD_ZERO(&rfd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(session->socket_fd, &rfd); FD_SET(session->socket_fd, &rfd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
readfd = &rfd; readfd = &rfd;
@ -688,12 +688,12 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time)
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) { if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
FD_ZERO(&wfd); FD_ZERO(&wfd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(session->socket_fd, &wfd); FD_SET(session->socket_fd, &wfd);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
writefd = &wfd; writefd = &wfd;
@ -1655,24 +1655,24 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
switch(fds[i].type) { switch(fds[i].type) {
case LIBSSH2_POLLFD_SOCKET: case LIBSSH2_POLLFD_SOCKET:
if(fds[i].events & LIBSSH2_POLLFD_POLLIN) { if(fds[i].events & LIBSSH2_POLLFD_POLLIN) {
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(fds[i].fd.socket, &rfds); FD_SET(fds[i].fd.socket, &rfds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
if(fds[i].fd.socket > maxfd) if(fds[i].fd.socket > maxfd)
maxfd = fds[i].fd.socket; maxfd = fds[i].fd.socket;
} }
if(fds[i].events & LIBSSH2_POLLFD_POLLOUT) { if(fds[i].events & LIBSSH2_POLLFD_POLLOUT) {
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(fds[i].fd.socket, &wfds); FD_SET(fds[i].fd.socket, &wfds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
if(fds[i].fd.socket > maxfd) if(fds[i].fd.socket > maxfd)
@ -1681,12 +1681,12 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
break; break;
case LIBSSH2_POLLFD_CHANNEL: case LIBSSH2_POLLFD_CHANNEL:
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(fds[i].fd.channel->session->socket_fd, &rfds); FD_SET(fds[i].fd.channel->session->socket_fd, &rfds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
if(fds[i].fd.channel->session->socket_fd > maxfd) if(fds[i].fd.channel->session->socket_fd > maxfd)
@ -1696,12 +1696,12 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
break; break;
case LIBSSH2_POLLFD_LISTENER: case LIBSSH2_POLLFD_LISTENER:
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
FD_SET(fds[i].fd.listener->session->socket_fd, &rfds); FD_SET(fds[i].fd.listener->session->socket_fd, &rfds);
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
if(fds[i].fd.listener->session->socket_fd > maxfd) if(fds[i].fd.listener->session->socket_fd > maxfd)
@ -1880,7 +1880,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
for(i = 0; i < nfds; i++) { for(i = 0; i < nfds; i++) {
switch(fds[i].type) { switch(fds[i].type) {
case LIBSSH2_POLLFD_SOCKET: case LIBSSH2_POLLFD_SOCKET:
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-conversion"
#endif #endif
@ -1893,7 +1893,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
if(fds[i].revents) { if(fds[i].revents) {
active_fds++; active_fds++;
} }
#if defined(__GNUC__) #if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
break; break;

View File

@ -85,7 +85,7 @@ foreach(_test IN LISTS DOCKER_TESTS STANDALONE_TESTS SSHD_TESTS)
elseif(TARGET ${LIB_STATIC}) elseif(TARGET ${LIB_STATIC})
set(_lib_for_tests ${LIB_STATIC}) set(_lib_for_tests ${LIB_STATIC})
else() else()
unset(_lib_for_tests) set(_lib_for_tests "")
message(STATUS "Skip test requiring static libssh2 lib: ${_test}") message(STATUS "Skip test requiring static libssh2 lib: ${_test}")
endif() endif()
@ -132,7 +132,7 @@ foreach(_test IN LISTS STANDALONE_TESTS)
endforeach() endforeach()
if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE) if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE)
unset(_sshd_test_targets) set(_sshd_test_targets "")
foreach(_test IN LISTS SSHD_TESTS) foreach(_test IN LISTS SSHD_TESTS)
if(TARGET ${_test}) if(TARGET ${_test})
set(_sshd_test_targets "${_sshd_test_targets} $<TARGET_FILE:${_test}>") set(_sshd_test_targets "${_sshd_test_targets} $<TARGET_FILE:${_test}>")
@ -176,3 +176,7 @@ add_target_to_copy_dependencies(
TARGET copy_test_dependencies TARGET copy_test_dependencies
DEPENDENCIES ${RUNTIME_DEPENDENCIES} DEPENDENCIES ${RUNTIME_DEPENDENCIES}
BEFORE_TARGETS ${TEST_TARGETS}) BEFORE_TARGETS ${TEST_TARGETS})
if(BUILD_OSSFUZZ)
add_subdirectory(ossfuzz)
endif()

View File

@ -0,0 +1,24 @@
# Copyright (C) Viktor Szakats
# SPDX-License-Identifier: BSD-3-Clause
enable_language(CXX)
add_library(standaloneengine STATIC "standaloneengine.cc")
target_include_directories(standaloneengine PRIVATE
"${PROJECT_SOURCE_DIR}/include")
set_target_properties(standaloneengine PROPERTIES UNITY_BUILD OFF)
add_executable(ssh2_client_fuzzer "ssh2_client_fuzzer.cc" "testinput.h")
target_include_directories(ssh2_client_fuzzer PRIVATE
"${PROJECT_SOURCE_DIR}/include")
if(LIB_FUZZING_ENGINE)
if(LIB_FUZZING_ENGINE STREQUAL "-fsanitize=fuzzer") # FIXME: compiler-specific
set_target_properties(ssh2_client_fuzzer PROPERTIES LINK_FLAGS "${LIB_FUZZING_ENGINE}")
else()
target_link_libraries(ssh2_client_fuzzer ${LIB_FUZZING_ENGINE})
endif()
else()
target_link_libraries(ssh2_client_fuzzer standaloneengine)
endif()
target_link_libraries(ssh2_client_fuzzer ${LIB_STATIC} ${LIBSSH2_LIBS_SOCKET})
set_target_properties(ssh2_client_fuzzer PROPERTIES UNITY_BUILD OFF)

View File

@ -31,4 +31,4 @@ ssh2_client_fuzzer_LDFLAGS = $(AM_LDFLAGS) -static
libstandaloneengine_a_SOURCES = standaloneengine.cc libstandaloneengine_a_SOURCES = standaloneengine.cc
libstandaloneengine_a_CXXFLAGS = $(AM_CXXFLAGS) libstandaloneengine_a_CXXFLAGS = $(AM_CXXFLAGS)
EXTRA_DIST = ossfuzz.sh EXTRA_DIST = CMakeLists.txt ossfuzz.sh