Skip to content

Commit df17e90

Browse files
Set Certain Warnings as Errors Unconditionally (#890)
Some constructs that are allowed in C are well-established to be virtually always erroneous, e.g. calling a function that hasn't been declared, implicitly binding a pointer to a different type, and implicitly converting an integral to a pointer. Implicitly converting a pointer-to-const to a pointer-to-mutable is not always an error, but is problematic enough that an explicit cast should be required. Many of these errors will be caught at a later time, but only due to test failures, linker failures, or on a build on CI that enables some warnings-as-errors (currently never on Windows). This change instead makes such constructs always-errors, thus adding a barrier to any changes that produce such errors that might otherwise have been missed. The selected warnings are well-established and detected by all modern compilers, and are very unlikely to appear anew do to compiler upgrades. Additional warnings that are not so definitely-wrong (etc. sign conversion/compare, unused variables) are not addressed by these changes. * MSVC fixes * More warnings, and fixes for C-only warnings * Only enable warnings in certain subdirectories * Include mongoc-warnings in dist * Disable some warnings on GCC < 5.0 * Use an OBJECT library for bundled zlib * Disable compiler warnings for zlib * Document MaintainerFlags versus MongoC-Warnings
1 parent 2bd2d84 commit df17e90

27 files changed

+141
-57
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ endif ()
130130
include (LoadVersion)
131131
LoadVersion (${PROJECT_SOURCE_DIR}/VERSION_CURRENT MONGOC)
132132

133+
# Enable "maintainer flags," which are supplementary but not mandatory.
134+
# (As opposed to MongoC-Warnings.cmake, which are required warnings)
133135
include (MaintainerFlags)
134136

135137
if ( (ENABLE_BUILD_DEPENDECIES STREQUAL OFF) AND (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) )
@@ -285,6 +287,10 @@ if (ENABLE_MONGOC)
285287
${SOURCE_DIR}/src/zlib-1.2.11/gzread.c
286288
${SOURCE_DIR}/src/zlib-1.2.11/gzwrite.c
287289
)
290+
add_library (zlib_obj OBJECT "${ZLIB_SOURCES}")
291+
set_property (TARGET zlib_obj PROPERTY POSITION_INDEPENDENT_CODE TRUE)
292+
# Disable all warnings for compiling Zlib
293+
target_compile_options (zlib_obj PRIVATE -w)
288294

289295
set (MONGOC_ENABLE_ICU 0)
290296

build/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set (build_cmake_MODULES
99
LoadVersion.cmake
1010
MaintainerFlags.cmake
1111
MongoCPackage.cmake
12+
MongoC-Warnings.cmake
1213
ParseVersion.cmake
1314
SphinxBuild.cmake
1415
CCache.cmake

build/cmake/MongoC-Warnings.cmake

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#[[
2+
This file sets warning options for the directories in which it is include()'d
3+
4+
These warnings are intended to be ported to each supported platform, and
5+
especially for high-value warnings that are very likely to catch latent bugs
6+
early in the process before the code is even run.
7+
]]
8+
9+
set (__is_gnu "$<C_COMPILER_ID:GNU>")
10+
set (__is_clang "$<OR:$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:AppleClang>>")
11+
set (__is_gnu_like "$<OR:${__is_gnu},${__is_clang}>")
12+
set (__is_msvc "$<C_COMPILER_ID:MSVC>")
13+
14+
# "Old" GNU is GCC < 5, which is missing several warning options
15+
set (__is_old_gnu "$<AND:${__is_gnu},$<VERSION_LESS:$<C_COMPILER_VERSION>,5>>")
16+
set (__not_old_gnu "$<NOT:${__is_old_gnu}>")
17+
18+
#[[
19+
Define additional compile options, conditional on the compiler being used.
20+
Each option should be prefixed by `gnu:`, `clang:`, `msvc:`, or `gnu-like:`.
21+
Those options will be conditionally enabled for GCC, Clang, or MSVC.
22+
23+
These options are attached to the source directory and its children.
24+
]]
25+
function (mongoc_add_platform_compile_options)
26+
foreach (opt IN LISTS ARGV)
27+
if (NOT opt MATCHES "^(gnu-like|gnu|clang|msvc):(.*)")
28+
message (SEND_ERROR "Invalid option '${opt}' (Should be prefixed by 'msvc:', 'gnu:', 'clang:', or 'gnu-like:'")
29+
continue ()
30+
endif ()
31+
if (CMAKE_MATCH_1 STREQUAL "gnu-like")
32+
add_compile_options ("$<${__is_gnu_like}:${CMAKE_MATCH_2}>")
33+
elseif (CMAKE_MATCH_1 STREQUAL gnu)
34+
add_compile_options ("$<${__is_gnu}:${CMAKE_MATCH_2}>")
35+
elseif (CMAKE_MATCH_1 STREQUAL clang)
36+
add_compile_options ("$<${__is_clang}:${CMAKE_MATCH_2}>")
37+
elseif (CMAKE_MATCH_1 STREQUAL "msvc")
38+
add_compile_options ("$<${__is_msvc}:${CMAKE_MATCH_2}>")
39+
else ()
40+
message (SEND_ERROR "Invalid option to mongoc_add_platform_compile_options(): '${opt}'")
41+
endif ()
42+
endforeach ()
43+
endfunction ()
44+
45+
if (CMAKE_VERSION VERSION_LESS 3.3)
46+
# On older CMake versions, we'll just always pass the warning options, even
47+
# if the generate warnings for the C++ check file
48+
set (is_c_lang "1")
49+
else ()
50+
# $<COMPILE_LANGUAGE> is only valid in CMake 3.3+
51+
set (is_c_lang "$<COMPILE_LANGUAGE:C>")
52+
endif ()
53+
54+
# These below warnings should always be unconditional hard errors, as the code is
55+
# almost definitely broken
56+
mongoc_add_platform_compile_options (
57+
# Implicit function or variable declarations
58+
gnu-like:$<${is_c_lang}:-Werror=implicit> msvc:/we4013 msvc:/we4431
59+
# Missing return types/statements
60+
gnu-like:-Werror=return-type msvc:/we4716
61+
# Incompatible pointer types
62+
gnu-like:$<$<AND:${is_c_lang},${__not_old_gnu}>:-Werror=incompatible-pointer-types> msvc:/we4113
63+
# Integral/pointer conversions
64+
gnu-like:$<$<AND:${is_c_lang},${__not_old_gnu}>:-Werror=int-conversion> msvc:/we4047
65+
# Discarding qualifiers
66+
gnu:$<$<AND:${is_c_lang},${__not_old_gnu}>:-Werror=discarded-qualifiers>
67+
clang:$<${is_c_lang}:-Werror=ignored-qualifiers>
68+
msvc:/we4090
69+
# Definite use of uninitialized value
70+
gnu-like:-Werror=uninitialized msvc:/we4700
71+
72+
# Aside: Disable CRT insecurity warnings
73+
msvc:/D_CRT_SECURE_NO_WARNINGS
74+
)

build/maintainer-flags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
-Wno-strict-aliasing
1111
-Wno-uninitialized
1212
-Wredundant-decls
13-
-Wreturn-type
1413
-Wshadow
1514
-Wswitch-default
1615
-Wswitch-enum

src/common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ if (ENABLE_DEBUG_ASSERTIONS)
55
set (MONGOC_ENABLE_DEBUG_ASSERTIONS 1)
66
endif ()
77

8+
include (MongoC-Warnings)
9+
810
configure_file (
911
"${PROJECT_SOURCE_DIR}/src/common/common-config.h.in"
1012
"${PROJECT_BINARY_DIR}/src/common/common-config.h"

src/common/common-thread-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ typedef struct {
107107
#define BSON_THREAD_FUN(_function_name, _arg_name) \
108108
unsigned (__stdcall _function_name) (void *(_arg_name))
109109
#define BSON_THREAD_FUN_TYPE(_function_name) \
110-
unsigned (__stdcall * _function_name) (void *)
110+
unsigned(__stdcall * _function_name) (void *)
111111
#define BSON_THREAD_RETURN return 0
112112
#endif
113113

src/kms-message/src/kms_port.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
*/
1616

1717
#include "kms_port.h"
18+
19+
#include <stdlib.h>
20+
#include <string.h>
21+
1822
#if defined(_WIN32)
19-
char * kms_strndup (const char *src, size_t len)
23+
char *
24+
kms_strndup (const char *src, size_t len)
2025
{
2126
char *dst = (char *) malloc (len + 1);
2227
if (!dst) {

src/libbson/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ if (ENABLE_TESTS AND NOT MONGOC_ENABLE_STATIC_BUILD)
1010
)
1111
endif ()
1212

13+
include (MongoC-Warnings)
14+
1315
set (BSON_OUTPUT_BASENAME "bson" CACHE STRING "Output bson library base name")
1416

1517
include (CheckFunctionExists)
@@ -98,9 +100,10 @@ else ()
98100
set (BSON_HAVE_STRLCPY 1)
99101
endif ()
100102

103+
CHECK_INCLUDE_FILE (stdbool.h BSON_HAVE_STDBOOL_H)
104+
101105
if (MSVC)
102106
set (BSON_HAVE_CLOCK_GETTIME 0)
103-
set (BSON_HAVE_STDBOOL_H 0)
104107
set (BSON_HAVE_STRNLEN 0)
105108
set (BSON_HAVE_SYSCALL_TID 0)
106109
else ()
@@ -112,10 +115,6 @@ else ()
112115
if (NOT BSON_HAVE_STRNLEN)
113116
set (BSON_HAVE_STRNLEN 0)
114117
endif ()
115-
CHECK_INCLUDE_FILE (stdbool.h BSON_HAVE_STDBOOL_H)
116-
if (NOT BSON_HAVE_STDBOOL_H)
117-
set (BSON_HAVE_STDBOOL_H 0)
118-
endif ()
119118
CHECK_SYMBOL_EXISTS (SYS_gettid sys/syscall.h BSON_HAVE_SYSCALL_TID)
120119
check_symbol_exists (syscall unistd.h _TMP_HAVE_SYSCALL)
121120
if (NOT BSON_HAVE_SYSCALL_TID OR NOT _TMP_HAVE_SYSCALL OR APPLE OR ANDROID)

src/libmongoc/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.1)
22

33
project (libmongoc C)
44

5-
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/../../build/cmake)
5+
include (MongoC-Warnings)
66

77
include (InstallRequiredSystemLibraries)
88
include (CheckStructHasMember)
@@ -45,9 +45,6 @@ if (NOT ENABLE_ZSTD MATCHES "ON|AUTO|OFF")
4545
message (FATAL_ERROR "ENABLE_ZSTD option must be ON, AUTO, or OFF")
4646
endif ()
4747

48-
# Disable warnings on bundled zlib source files.
49-
set_source_files_properties (${ZLIB_SOURCES} PROPERTIES COMPILE_FLAGS -w)
50-
5148
# Copy zconf.h.in to zconf.h; even when using system zlib, the 'dist' target
5249
# will look for zconf.h in that location.
5350
configure_file (
@@ -78,7 +75,7 @@ endif ()
7875
if ( (ENABLE_ZLIB STREQUAL "BUNDLED")
7976
OR (ENABLE_ZLIB STREQUAL "AUTO" AND NOT ZLIB_FOUND) )
8077
message (STATUS "Enabling zlib compression (bundled)")
81-
set (SOURCES ${SOURCES} ${ZLIB_SOURCES})
78+
set (SOURCES ${SOURCES} $<TARGET_OBJECTS:zlib_obj>)
8279
set (
8380
ZLIB_INCLUDE_DIRS
8481
"${SOURCE_DIR}/src/zlib-1.2.11"

src/libmongoc/tests/TestSuite.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ _print_getlasterror_win (const char *msg)
446446
NULL,
447447
GetLastError (),
448448
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
449-
&err_msg,
449+
/* FormatMessage is weird about this param. */
450+
(LPTSTR) &err_msg,
450451
0,
451452
NULL);
452453

src/libmongoc/tests/mock_server/request.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,22 +613,22 @@ request_matches_msg (const request_t *request,
613613
bool
614614
request_matches_msgv (const request_t *request, uint32_t flags, va_list *args)
615615
{
616-
const bson_t **docs;
616+
bson_t **docs;
617617
size_t n_docs, allocated;
618618
bool r;
619619

620620
n_docs = 0;
621621
allocated = 1;
622622
docs = bson_malloc (allocated * sizeof (bson_t *));
623-
while ((docs[n_docs] = va_arg (*args, const bson_t *))) {
623+
while ((docs[n_docs] = va_arg (*args, bson_t *))) {
624624
n_docs++;
625625
if (n_docs == allocated) {
626626
allocated = bson_next_power_of_two (allocated + 1);
627627
docs = bson_realloc (docs, allocated * sizeof (bson_t *));
628628
}
629629
}
630630

631-
r = request_matches_msg (request, flags, docs, n_docs);
631+
r = request_matches_msg (request, flags, (const bson_t **) docs, n_docs);
632632
bson_free (docs);
633633
return r;
634634
}

src/libmongoc/tests/test-conveniences.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test_conveniences_init ()
6060

6161

6262
void
63-
test_conveniences_cleanup ()
63+
test_conveniences_cleanup (void)
6464
{
6565
int i;
6666
bson_t *doc;

src/libmongoc/tests/test-conveniences.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test_conveniences_init ();
3939
* Called automatically at process exit.
4040
*/
4141
void
42-
test_conveniences_cleanup ();
42+
test_conveniences_cleanup (void);
4343

4444
/* Return a bson_t representation from a single-quoted JSON string, with
4545
* possible printf format directives.

src/libmongoc/tests/test-mongoc-async.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ test_hello_delay_initializer (mongoc_async_cmd_t *acmd)
326326
}
327327

328328
static void
329-
test_hello_delay ()
329+
test_hello_delay (void)
330330
{
331331
/* test that a delayed cmd works. */
332332
mock_server_t *server = mock_server_with_auto_hello (WIRE_VERSION_MAX);

src/libmongoc/tests/test-mongoc-bulk.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3476,7 +3476,8 @@ _test_numerous (bool ordered)
34763476
TEST_NUMEROUS (remove_one (bulk, doc), "{'q': {'_id': 1}, 'limit': 1}");
34773477
TEST_NUMEROUS (replace_one (bulk, doc, tmp_bson ("{}"), false),
34783478
"{'q': {'_id': 1}, 'u': {}}");
3479-
TEST_NUMEROUS (update_one (bulk, doc, tmp_bson ("{'$set': {'x': 1}}"), NULL),
3479+
TEST_NUMEROUS (
3480+
update_one (bulk, doc, tmp_bson ("{'$set': {'x': 1}}"), false),
34803481
"{'q': {'_id': 1}, 'u': {'$set': {'x': 1}}}");
34813482
TEST_NUMEROUS (update_many_with_opts (
34823483
bulk, doc, tmp_bson ("{'$set': {'x': 1}}"), NULL, NULL),

src/libmongoc/tests/test-mongoc-client-side-encryption.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ _test_multi_threaded (bool external_key_vault)
22182218
}
22192219

22202220
static void
2221-
test_multi_threaded ()
2221+
test_multi_threaded (void *ctx_unused)
22222222
{
22232223
_test_multi_threaded (true);
22242224
_test_multi_threaded (false);

src/libmongoc/tests/test-mongoc-cluster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,13 +1554,13 @@ _test_cluster_command_error (bool use_op_msg)
15541554
}
15551555

15561556
static void
1557-
test_cluster_command_error_op_msg ()
1557+
test_cluster_command_error_op_msg (void)
15581558
{
15591559
_test_cluster_command_error (true);
15601560
}
15611561

15621562
static void
1563-
test_cluster_command_error_op_query ()
1563+
test_cluster_command_error_op_query (void)
15641564
{
15651565
_test_cluster_command_error (false);
15661566
}

src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ _batch_size_test (bson_t *pipeline,
282282
}
283283

284284
static void
285-
test_aggregate_with_batch_size ()
285+
test_aggregate_with_batch_size (void)
286286
{
287287
bson_t *pipeline_dollar_out;
288288
bson_t *pipeline_dollar_merge;
@@ -1687,7 +1687,7 @@ test_index (void)
16871687
}
16881688

16891689
static void
1690-
test_index_w_write_concern ()
1690+
test_index_w_write_concern (void)
16911691
{
16921692
mongoc_collection_t *collection;
16931693
mongoc_database_t *database;

src/libmongoc/tests/test-mongoc-handshake.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ _get_bit (char *config_str, uint32_t bit)
740740
}
741741

742742
void
743-
test_handshake_platform_config ()
743+
test_handshake_platform_config (void)
744744
{
745745
/* Parse the config string, and check that it matches the defined flags. */
746746
char *config_str = _mongoc_handshake_get_config_hex_string ();

src/libmongoc/tests/test-mongoc-mongohouse.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ cmd_succeeded_cb (const mongoc_apm_command_succeeded_t *event)
151151

152152
/* Store cursor information from our initial find. */
153153
if (strcmp (cmd, "find") == 0) {
154-
155154
BSON_ASSERT (!test->parsed_cursor);
156155

157156
bson_iter_init (&iter, reply);
@@ -163,7 +162,7 @@ cmd_succeeded_cb (const mongoc_apm_command_succeeded_t *event)
163162
BSON_ASSERT (bson_iter_find_descendant (&iter, "cursor.ns", &child_iter));
164163
BSON_ASSERT (BSON_ITER_HOLDS_UTF8 (&child_iter));
165164

166-
test->cursor_ns = bson_strdup(bson_iter_utf8(&child_iter, NULL));
165+
test->cursor_ns = bson_strdup (bson_iter_utf8 (&child_iter, NULL));
167166
BSON_ASSERT (NULL != test->cursor_ns);
168167

169168
test->parsed_cursor = true;
@@ -194,7 +193,7 @@ cmd_succeeded_cb (const mongoc_apm_command_succeeded_t *event)
194193
/* Test that the driver properly constructs and issues a killCursors command to
195194
* ADL. */
196195
static void
197-
test_mongohouse_kill_cursors ()
196+
test_mongohouse_kill_cursors (void *ctx_unused)
198197
{
199198
mongoc_apm_callbacks_t *callbacks;
200199
mongoc_collection_t *coll;
@@ -273,7 +272,7 @@ _run_ping_test (const char *connection_string)
273272
/* Test that the driver can establish a connection to ADL with authentication.
274273
Test both SCRAM-SHA-1 and SCRAM-SHA-256. */
275274
static void
276-
test_mongohouse_auth ()
275+
test_mongohouse_auth (void *ctx_unused)
277276
{
278277
/* SCRAM-SHA-1 */
279278
_run_ping_test (
@@ -286,7 +285,7 @@ test_mongohouse_auth ()
286285

287286
/* Test that the driver can connect to ADL without authentication. */
288287
static void
289-
test_mongohouse_no_auth ()
288+
test_mongohouse_no_auth (void *ctx_unused)
290289
{
291290
_run_ping_test ("mongodb://localhost:27017");
292291
}

src/libmongoc/tests/test-mongoc-read-prefs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ aggregate (mongoc_collection_t *collection, mongoc_read_prefs_t *prefs)
10531053
/* direct connection to a secondary requires read pref primaryPreferred to
10541054
* avoid "not primary" error from server */
10551055
static void
1056-
test_op_msg_direct_secondary ()
1056+
test_op_msg_direct_secondary (void)
10571057
{
10581058
_test_op_msg_direct_connection (
10591059
false /* is_mongos */,
@@ -1075,7 +1075,7 @@ test_op_msg_direct_secondary ()
10751075

10761076
/* direct connection to mongos must not auto-add read pref primaryPreferred */
10771077
static void
1078-
test_op_msg_direct_mongos ()
1078+
test_op_msg_direct_mongos (void)
10791079
{
10801080
_test_op_msg_direct_connection (true /* is_mongos */,
10811081
find,

src/libmongoc/tests/test-mongoc-shared.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ my_value_free_v (void *ptr)
3434
}
3535

3636
static void
37-
test_simple ()
37+
test_simple (void)
3838
{
3939
int destroyed_value = 0;
4040
mongoc_shared_ptr ptr = MONGOC_SHARED_PTR_NULL;

0 commit comments

Comments
 (0)