Replace existing `mk-unity.pl` `--embed` workaround with running
`clang-tidy` manually on individual test source instead. This aligns
with how clang-tidy works and removes `mk-unity.pl` from the solution.
Also:
- mqttd: fix potentially uninitialized buffer by zero filling it.
```
tests/server/mqttd.c:484:41: error: The left operand of '<<' is a garbage value
[clang-analyzer-core.UndefinedBinaryOperatorResult,-warnings-as-errors]
484 | payload_len = (size_t)(buffer[10] << 8) | buffer[11];
| ^
[...]
tests/server/mqttd.c:606:45: error: The left operand of '<<' is a garbage value
[clang-analyzer-core.UndefinedBinaryOperatorResult,-warnings-as-errors]
606 | topiclen = (size_t)(buffer[1 + bytes] << 8) | buffer[2 + bytes];
| ^
```
- sockfilt: fix potential out-of-bound pointer:
```
tests/server/sockfilt.c:1128:33: error: The 2nd argument to 'send' is a buffer
with size 17010 but should be a buffer with size equal to or greater than
the value of the 3rd argument (which is
18446744073709551615)
[clang-analyzer-unix.StdCLibraryFunctions,-warnings-as-errors]
1128 | ssize_t bytes_written = swrite(sockfd, buffer, buffer_len);
| ^
```
- clang-tidy: suppress bogus `bzero()` warnings that happens
inside the notorious `FD_ZERO()` macros, on macOS.
Ref: https://github.com/curl/curl/pull/17680#issuecomment-
2991730158
Closes #17705
env:
MAKEFLAGS: -j 5
CURL_CI: github
- CURL_CLANG_TIDYFLAGS: '-checks=-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-valist.Uninitialized'
+ CURL_CLANG_TIDYFLAGS: '-checks=-clang-analyzer-security.insecureAPI.bzero,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-valist.Uninitialized'
# renovate: datasource=github-tags depName=libressl-portable/portable versioning=semver registryUrl=https://github.com
LIBRESSL_VERSION: 4.1.0
# renovate: datasource=github-tags depName=wolfSSL/wolfssl versioning=semver extractVersion=^v?(?<version>.+)-stable$ registryUrl=https://github.com
set(SIZEOF_${_type} ${_size})
set(SIZEOF_${_type}_CODE "#define SIZEOF_${_type} ${_size}")
endmacro()
+
+# Create a clang-tidy target for test targets
+macro(curl_clang_tidy_tests _target)
+ if(CURL_CLANG_TIDY)
+
+ # Collect header directories and macro definitions from lib dependencies
+ set(_includes_l "")
+ set(_definitions_l "")
+ get_target_property(_libs ${_target} LINK_LIBRARIES)
+ foreach(_lib IN LISTS _libs)
+ if(TARGET "${_lib}")
+ get_target_property(_val ${_lib} INCLUDE_DIRECTORIES)
+ if(_val)
+ list(APPEND _includes_l ${_val})
+ endif()
+ get_target_property(_val ${_lib} COMPILE_DEFINITIONS)
+ if(_val)
+ list(APPEND _definitions_l ${_val})
+ endif()
+ endif()
+ endforeach()
+
+ # Collect header directories applying to the target
+ get_directory_property(_includes_d INCLUDE_DIRECTORIES)
+ get_target_property(_includes_t ${_target} INCLUDE_DIRECTORIES)
+
+ set(_includes "${_includes_l};${_includes_d};${_includes_t}")
+ list(REMOVE_ITEM _includes "")
+ string(REPLACE ";" ";-I" _includes ";${_includes}")
+
+ # Collect macro definitions applying to the target
+ get_directory_property(_definitions_d COMPILE_DEFINITIONS)
+ get_target_property(_definitions_t ${_target} COMPILE_DEFINITIONS)
+
+ set(_definitions "${_definitions_l};${_definitions_d};${_definitions_t}")
+ list(REMOVE_ITEM _definitions "")
+ string(REPLACE ";" ";-D" _definitions ";${_definitions}")
+ list(SORT _definitions) # Sort like CMake does
+
+ # Assemble source list
+ set(_sources "")
+ foreach(_source IN ITEMS ${ARGN})
+ if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_source}") # if not in source tree
+ set(_source "${CMAKE_CURRENT_BINARY_DIR}/${_source}") # look in the build tree, for generated files, e.g. lib1521.c
+ endif()
+ list(APPEND _sources "${_source}")
+ endforeach()
+
+ add_custom_target("${_target}-clang-tidy" USES_TERMINAL
+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+ COMMAND ${CMAKE_C_CLANG_TIDY} ${_sources} -- ${_includes} ${_definitions}
+ DEPENDS ${_sources})
+ add_dependencies(tests-clang-tidy "${_target}-clang-tidy")
+
+ unset(_includes_d)
+ unset(_includes_t)
+ unset(_includes)
+ unset(_definitions_l)
+ unset(_definitions_d)
+ unset(_definitions_t)
+ unset(_definitions)
+ unset(_sources)
+ unset(_source)
+ unset(_libs)
+ unset(_lib)
+ unset(_val)
+ endif()
+endmacro()
option(CURL_CLANG_TIDY "Run the build through clang-tidy" OFF)
if(CURL_CLANG_TIDY)
- # clang-tidy is not looking into #included sources, thus not compatible with
- # unity builds. Make test bundles compatible by embedding the source code.
- set(CMAKE_UNITY_BUILD OFF)
- set(CURL_MK_UNITY_OPTION "--embed")
+ set(CMAKE_UNITY_BUILD OFF) # clang-tidy is not looking into #included sources, thus not compatible with unity builds.
set(_tidy_checks "")
+ list(APPEND _tidy_checks "-clang-analyzer-security.insecureAPI.bzero") # for FD_ZERO() (seen on macOS)
list(APPEND _tidy_checks "-clang-analyzer-security.insecureAPI.strcpy")
list(APPEND _tidy_checks "-clang-analyzer-optin.performance.Padding")
list(APPEND _tidy_checks "-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling")
endif
# disable the tests that are mostly causing false positives
-TIDYFLAGS := -checks=-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -quiet
+TIDYFLAGS := -checks=-clang-analyzer-security.insecureAPI.bzero,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -quiet
if CURL_WERROR
TIDYFLAGS += --warnings-as-errors=*
endif
use warnings;
if(!@ARGV) {
- die "Usage: $0 [--test <tests>] [--include <include-c-sources>] [--srcdir <srcdir>] [--embed]\n";
+ die "Usage: $0 [--test <tests>] [--include <include-c-sources>]\n";
}
my @src;
my %include;
my $in_include = 0;
-my $srcdir = "";
-my $in_srcdir = 0;
my $any_test = 0;
-my $embed = 0;
foreach my $src (@ARGV) {
if($src eq "--test") {
$in_include = 0;
elsif($src eq "--include") {
$in_include = 1;
}
- elsif($src eq "--embed") {
- $embed = 1;
- }
- elsif($src eq "--srcdir") {
- $in_srcdir = 1;
- }
- elsif($in_srcdir) {
- $srcdir = $src;
- $in_srcdir = 0;
- }
elsif($in_include) {
$include{$src} = 1;
push @src, $src;
foreach my $src (@src) {
if($src =~ /([a-z0-9_]+)\.c$/) {
my $name = $1;
- if($embed) {
- my $fn = $src;
- if($srcdir ne "" && -e "$srcdir/$fn") {
- $fn = $srcdir . "/" . $fn;
- }
- print "/* Embedding: \"$src\" */\n";
- my $content = do { local $/; open my $fh, '<', $fn or die $!; <$fh> };
- print $content;
- }
- else {
- print "#include \"$src\"\n";
- }
+ print "#include \"$src\"\n";
if(not exists $include{$src}) { # register test entry function
$tlist .= " {\"$name\", test_$name},\n";
}
curlx_src = $(CURLX_CFILES)
endif
curltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(curl_cfiles_gen) $(curlx_src)
- @PERL@ $(top_srcdir)/scripts/mk-unity.pl --srcdir $(srcdir) --include $(CURL_CFILES) $(curl_cfiles_gen) $(curlx_src) > curltool_unity.c
+ @PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(CURL_CFILES) $(curl_cfiles_gen) $(curlx_src) > curltool_unity.c
nodist_curl_SOURCES = curltool_unity.c
curl_SOURCES =
endif
# disable the tests that are mostly causing false positives
-TIDYFLAGS := -checks=-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -quiet
+TIDYFLAGS := -checks=-clang-analyzer-security.insecureAPI.bzero,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -quiet
if CURL_WERROR
TIDYFLAGS += --warnings-as-errors=*
endif
add_dependencies(testdeps curlinfo)
endif()
+if(CURL_CLANG_TIDY)
+ add_custom_target(tests-clang-tidy)
+ add_dependencies(testdeps tests-clang-tidy)
+endif()
+
add_subdirectory(http)
add_subdirectory(client)
add_subdirectory(server)
endif()
add_custom_command(OUTPUT "${BUNDLE}.c"
- COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --include ${CURLX_CFILES} --test ${TESTFILES}
- ${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE}.c"
+ COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl"
+ --include ${CURLX_CFILES} --test ${TESTFILES} > "${BUNDLE}.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc"
${FIRSTFILES} ${CURLX_CFILES} ${TESTFILES}
"${CMAKE_CURRENT_SOURCE_DIR}" # for the generated bundle source to find included test sources
)
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_NO_OLDIES")
-set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF)
+set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF C_CLANG_TIDY "")
+
+curl_clang_tidy_tests(${BUNDLE} ${FIRSTFILES} ${TESTFILES})
list(APPEND TESTFILES "lib1521.c")
add_custom_command(OUTPUT "${BUNDLE}.c"
- COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --include ${UTILS} ${CURLX_CFILES} --test ${TESTFILES}
- ${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE}.c"
+ COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl"
+ --include ${UTILS} ${CURLX_CFILES} --test ${TESTFILES} > "${BUNDLE}.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc"
${FIRSTFILES} ${UTILS} ${CURLX_CFILES} ${TESTFILES}
)
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "${CURL_DEBUG_MACROS}")
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_NO_OLDIES" "CURL_DISABLE_DEPRECATION")
-set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF)
+set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF C_CLANG_TIDY "")
+
+curl_clang_tidy_tests(${BUNDLE} ${FIRSTFILES} ${UTILS} ${TESTFILES} ${STUB_GSS})
if(HAVE_GSSAPI AND UNIX)
add_library(stubgss SHARED EXCLUDE_FROM_ALL ${STUB_GSS})
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
add_custom_command(OUTPUT "${BUNDLE}.c"
- COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --include ${UTILS} ${CURLX_CFILES} --test ${TESTFILES}
- ${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE}.c"
+ COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl"
+ --include ${UTILS} ${CURLX_CFILES} --test ${TESTFILES} > "${BUNDLE}.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc"
${FIRSTFILES} ${UTILS} ${CURLX_CFILES} ${TESTFILES}
if(WIN32)
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
endif()
-set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF)
+set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF C_CLANG_TIDY "")
+
+curl_clang_tidy_tests(${BUNDLE} ${FIRSTFILES} ${UTILS} ${TESTFILES})
logmsg("Out of memory, unable to allocate buffer");
goto end;
}
+ memset(buffer, 0, buff_size);
do {
unsigned char usr_flag = 0x80;
* *buffer_len is the amount of data in the buffer (output)
*/
static bool read_data_block(unsigned char *buffer, ssize_t maxlen,
- ssize_t *buffer_len)
+ ssize_t *buffer_len)
{
if(!read_stdin(buffer, 5))
return FALSE;
if(!read_data_block(buffer, sizeof(buffer), &buffer_len))
return FALSE;
+ if(buffer_len < 0)
+ return FALSE;
+
if(*mode == PASSIVE_LISTEN) {
logmsg("*** We are disconnected!");
if(!disc_handshake())
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
add_custom_command(OUTPUT "${BUNDLE}.c"
- COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --test ${TESTFILES}
- ${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE}.c"
+ COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl"
+ --test ${TESTFILES} > "${BUNDLE}.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc" ${FIRSTFILES} ${TESTFILES}
VERBATIM)
)
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "${CURL_DEBUG_MACROS}")
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_NO_OLDIES" "CURL_DISABLE_DEPRECATION")
-set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF)
+set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF C_CLANG_TIDY "")
+
+curl_clang_tidy_tests(${BUNDLE} ${FIRSTFILES} ${TESTFILES})
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
add_custom_command(OUTPUT "${BUNDLE}.c"
- COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --test ${TESTFILES}
- ${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE}.c"
+ COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl"
+ --test ${TESTFILES} > "${BUNDLE}.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc" ${FIRSTFILES} ${TESTFILES}
VERBATIM)
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_NO_OLDIES" "CURL_DISABLE_DEPRECATION")
# unit tests are small pretend-libcurl-programs
set_property(TARGET ${BUNDLE} APPEND PROPERTY COMPILE_DEFINITIONS "BUILDING_LIBCURL")
-set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF)
+set_target_properties(${BUNDLE} PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}" UNITY_BUILD OFF C_CLANG_TIDY "")
+
+curl_clang_tidy_tests(${BUNDLE} ${FIRSTFILES} ${TESTFILES})