Skip to content

Commit

Permalink
additional warnings (#653)
Browse files Browse the repository at this point in the history
Refactor: add stricter compile options to find common issues

Add:

-Wextra
-Werror
-Wpedantic
-Wmissing-prototypes
-Wshadow
-Wsequence-point

And set to c11.
  • Loading branch information
nichtsfrei authored Feb 14, 2022
1 parent ef5723d commit 9bb86d2
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 83 deletions.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ if (POLICY CMP0005)
cmake_policy (SET CMP0005 NEW)
endif (POLICY CMP0005)

set (C_STANDARD, 11)
set (CMAKE_C_STANDARD 11)
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Debug)
endif (NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -197,9 +199,23 @@ endif (ENABLE_COVERAGE)

set (HARDENING_FLAGS "-Wformat -Wformat-security -D_FORTIFY_SOURCE=2 -fstack-protector")
set (LINKER_HARDENING_FLAGS "-Wl,-z,relro -Wl,-z,now")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wextra -Werror ${COVERAGE_FLAGS}")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${COVERAGE_FLAGS}")
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${HARDENING_FLAGS}")

set (
CMAKE_C_FLAGS
"${CMAKE_C_FLAGS} ${GPGME_C_FLAGS} \
-std=c11 \
-Wall \
-Wextra \
-Werror \
-Wpedantic \
-Wmissing-prototypes \
-Wshadow \
-Wsequence-point \
-D_ISOC11_SOURCE \
-D_DEFAULT_SOURCE"
)

## Version

Expand Down
2 changes: 1 addition & 1 deletion base/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @return New array.
*/
GPtrArray *
make_array ()
make_array (void)
{
return g_ptr_array_new ();
}
Expand Down
2 changes: 1 addition & 1 deletion base/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
typedef GPtrArray array_t;

GPtrArray *
make_array ();
make_array (void);

void
array_reset (array_t **array);
Expand Down
4 changes: 3 additions & 1 deletion base/cvss.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@
* complete: 0.660
*/

#include "cvss.h"

#include <glib.h>
#include <math.h>
#include <string.h>
#include <strings.h>

#undef G_LOG_DOMAIN
/**
Expand Down
2 changes: 1 addition & 1 deletion base/cvss_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Ensure (cvss, get_cvss_score_from_base_metrics_null)
assert_that (get_cvss_score_from_base_metrics (NULL), is_equal_to (-1.0));
}

double
static double
nearest (double cvss)
{
return round (cvss * 10) / 10;
Expand Down
10 changes: 5 additions & 5 deletions base/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ port_in_port_ranges (int pnum, port_protocol_t ptype, array_t *pranges)
* @return 1 if IPv6 is enabled, 0 if disabled.
*/
int
ipv6_is_enabled ()
ipv6_is_enabled (void)
{
int sock = socket (PF_INET6, SOCK_STREAM, 0);

Expand All @@ -782,7 +782,7 @@ ipv6_is_enabled ()
*
* @return True if IP is localhost, else false.
*/
gboolean
static gboolean
ip_islocalhost (struct sockaddr_storage *storage)
{
struct in_addr addr;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ gvm_routethrough (struct sockaddr_storage *storage_dest,
*
* @return Socket number or -1 on error.
*/
int
static int
get_connected_udp_sock (struct sockaddr_storage *target_addr)
{
int family = target_addr->ss_family;
Expand Down Expand Up @@ -1184,7 +1184,7 @@ get_connected_udp_sock (struct sockaddr_storage *target_addr)
*
* @return 0 on success, -1 on error.
*/
int
static int
get_sock_addr (int sockfd, struct sockaddr_storage *sock_addr)
{
socklen_t len;
Expand Down Expand Up @@ -1220,7 +1220,7 @@ get_sock_addr (int sockfd, struct sockaddr_storage *sock_addr)
* @return Interface name of matching interface which to be freed by the caller.
* Null if no interface found or error.
*/
char *
static char *
get_ifname_from_ifaddr (struct sockaddr_storage *target_addr)
{
struct ifaddrs *ifaddr, *ifa;
Expand Down
2 changes: 1 addition & 1 deletion base/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int
port_in_port_ranges (int, port_protocol_t, array_t *);

int
ipv6_is_enabled ();
ipv6_is_enabled (void);

gchar *
gvm_routethrough (struct sockaddr_storage *, struct sockaddr_storage *);
Expand Down
12 changes: 10 additions & 2 deletions base/networking_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Ensure (networking, validate_port_range)
WEB_SERVICES,
};

#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Woverlength-strings"
const char *portlists[] = {
"T:1-50,52-80,82-99,101-113,115-224,242-248,256-257,259-271,280-284,286-"
"287,308-324,333,344-584,586-658,660-702,704-707,709-715,729-731,741-742,"
Expand Down Expand Up @@ -804,7 +806,7 @@ Ensure (networking, validate_port_range)
"32771,32815-32815,33281-33281,49152-49154,49156-49156,49181-49182,49185-"
"49186,49188-49188,49190-49194,49200-49201",
"T:80-80,443-443"};

#pragma GCC diagnostic pop
assert_that (
validate_port_range (portlists[ALL_IANA_ASSIGNED_TCP_2020_02_12]),
is_equal_to (0));
Expand Down Expand Up @@ -1018,6 +1020,9 @@ __real_g_io_channel_new_file (const char *filename, const char *mode,

gboolean g_g_io_channel_new_file_use_real = TRUE;
GIOChannel *
__wrap_g_io_channel_new_file (const char *filename, const char *mode,
GError **error);
GIOChannel *
__wrap_g_io_channel_new_file (const char *filename, const char *mode,
GError **error)
{
Expand All @@ -1033,6 +1038,9 @@ __real_g_io_channel_shutdown (GIOChannel *channel, gboolean flush,

gboolean g_g_io_channel_shutdown_use_real = TRUE;
GIOStatus
__wrap_g_io_channel_shutdown (GIOChannel *channel, gboolean flush,
GError **err);
GIOStatus
__wrap_g_io_channel_shutdown (GIOChannel *channel, gboolean flush, GError **err)
{
if (g_g_io_channel_shutdown_use_real)
Expand Down Expand Up @@ -1170,7 +1178,7 @@ Ensure (networking, gvm_source_addr)
assert_that ((src.s_addr != INADDR_ANY));
}

TestSuite *
static TestSuite *
gvm_routethough ()
{
TestSuite *suite = create_test_suite ();
Expand Down
2 changes: 2 additions & 0 deletions base/prefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* module.
*/

#include "prefs.h"

#include "settings.h" /* for init_settings_iterator_from_file */

#include <glib.h> /* for gchar */
Expand Down
2 changes: 1 addition & 1 deletion base/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @return 0 success, -1 error.
*/
int
static int
settings_init_from_file (settings_t *settings, const gchar *filename,
const gchar *group)
{
Expand Down
7 changes: 0 additions & 7 deletions boreas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ set (FILES alivedetection.c arp.c boreas_error.c boreas_io.c cli.c ping.c sniffe

set (HEADERS alivedetection.h arp.h boreas_error.h boreas_io.h cli.h ping.h sniffer.h util.h)

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
if(${GLIB_VERSION} VERSION_LESS "2.50.3")
add_definitions(-D_BSD_SOURCE)
endif(${GLIB_VERSION} VERSION_LESS "2.50.3")
add_compile_options(-std=gnu99)
endif(CMAKE_C_COMPILER_ID STREQUAL "GNU")

if (BUILD_STATIC)
set (LIBGVM_BOREAS_NAME gvm_boreas_static)
add_library (gvm_boreas_static STATIC ${FILES})
Expand Down
2 changes: 1 addition & 1 deletion boreas/boreas_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ max_scan_hosts_reached ()
/**
* @brief Set max_scan_hosts_reached to TRUE.
*/
void
static void
set_max_scan_hosts_reached ()
{
scan_restrictions.max_scan_hosts_reached = TRUE;
Expand Down
10 changes: 10 additions & 0 deletions boreas/util_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ __real_setsockopt (__attribute__ ((unused)) int sockfd,

bool g_socket_use_real = true;
int
__wrap_socket (__attribute__ ((unused)) int domain,
__attribute__ ((unused)) int type,
__attribute__ ((unused)) int protocol);
int
__wrap_socket (__attribute__ ((unused)) int domain,
__attribute__ ((unused)) int type,
__attribute__ ((unused)) int protocol)
Expand All @@ -57,6 +61,12 @@ __wrap_socket (__attribute__ ((unused)) int domain,

bool g_setsockopt_use_real = true;
int
__wrap_setsockopt (__attribute__ ((unused)) int sockfd,
__attribute__ ((unused)) int level,
__attribute__ ((unused)) int optname,
__attribute__ ((unused)) const void *optval,
__attribute__ ((unused)) socklen_t optlen);
int
__wrap_setsockopt (__attribute__ ((unused)) int sockfd,
__attribute__ ((unused)) int level,
__attribute__ ((unused)) int optname,
Expand Down
8 changes: 4 additions & 4 deletions gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include <errno.h> /* for ERANGE, errno */
#include <stdlib.h> /* for NULL, strtol, atoi */
#include <string.h> /* for strlen, strdup */
#include <strings.h>

#undef G_LOG_DOMAIN
/**
Expand Down Expand Up @@ -79,7 +79,7 @@ gmp_task_status (entity_t response)
*
* @return 0 on success, -1 or GMP response code on error.
*/
int
static int
gmp_check_response (gnutls_session_t *session, entity_t *entity)
{
int ret;
Expand Down Expand Up @@ -180,7 +180,7 @@ check_response_c (gvm_connection_t *connection, int convert_99)
*
* @return 0 on success, -1 or GMP response code on error.
*/
int
static int
gmp_check_response_c (gvm_connection_t *connection)
{
return check_response_c (connection, 0);
Expand Down Expand Up @@ -1292,7 +1292,7 @@ gmp_modify_task_file (gnutls_session_t *session, const char *id,
{
gchar *base64_content =
g_base64_encode ((guchar *) content, content_len);
int ret = gvm_server_sendf (session, "%s", base64_content);
ret = gvm_server_sendf (session, "%s", base64_content);
g_free (base64_content);
if (ret)
return -1;
Expand Down
7 changes: 0 additions & 7 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ if (BUILD_WITH_LDAP)
endif (NOT LIBLDAP)
endif (BUILD_WITH_LDAP)

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
# set C standard for older GNU compilers (GCC < 6.3.0)
if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3)
add_compile_options(-std=gnu99)
endif()
endif()

include_directories (${GLIB_INCLUDE_DIRS} ${GPGME_INCLUDE_DIRS} ${GCRYPT_INCLUDE_DIRS}
${LIBXML2_INCLUDE_DIRS})

Expand Down
6 changes: 3 additions & 3 deletions util/authutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static gboolean initialized = FALSE;
* @return 1 if enabled, else 0.
*/
int
gvm_auth_ldap_enabled ()
gvm_auth_ldap_enabled (void)
{
#ifdef ENABLE_LDAP_AUTH
return 1;
Expand All @@ -67,7 +67,7 @@ gvm_auth_ldap_enabled ()
* @return 1 if enabled, else 0.
*/
int
gvm_auth_radius_enabled ()
gvm_auth_radius_enabled (void)
{
#ifdef ENABLE_RADIUS_AUTH
return 1;
Expand Down Expand Up @@ -100,7 +100,7 @@ auth_method_name (auth_method_t method)
* @return 0 success, -1 error.
*/
int
gvm_auth_init ()
gvm_auth_init (void)
{
if (initialized == TRUE)
{
Expand Down
6 changes: 3 additions & 3 deletions util/authutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef enum authentication_method auth_method_t;
const gchar *auth_method_name (auth_method_t);

int
gvm_auth_init ();
gvm_auth_init (void);

int
gvm_authenticate_classic (const gchar *, const gchar *, const gchar *);
Expand All @@ -59,9 +59,9 @@ gchar *
digest_hex (int, const guchar *);

int
gvm_auth_ldap_enabled ();
gvm_auth_ldap_enabled (void);

int
gvm_auth_radius_enabled ();
gvm_auth_radius_enabled (void);

#endif /* not _GVM_AUTHUTILS_H */
17 changes: 11 additions & 6 deletions util/kb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1576,11 +1576,16 @@ redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename)
kbr = redis_kb (kb);
rep = redis_cmd (
kbr, "RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %s %s",
nvti_oid (nvt), filename, nvti_required_keys (nvt) ?: "",
nvti_mandatory_keys (nvt) ?: "", nvti_excluded_keys (nvt) ?: "",
nvti_required_udp_ports (nvt) ?: "", nvti_required_ports (nvt) ?: "",
nvti_dependencies (nvt) ?: "", nvti_tag (nvt) ?: "", cves ?: "", bids ?: "",
xrefs ?: "", nvti_category (nvt), nvti_family (nvt), nvti_name (nvt));
nvti_oid (nvt), filename,
nvti_required_keys (nvt) ? nvti_required_keys (nvt) : "",
nvti_mandatory_keys (nvt) ? nvti_mandatory_keys (nvt) : "",
nvti_excluded_keys (nvt) ? nvti_excluded_keys (nvt) : "",
nvti_required_udp_ports (nvt) ? nvti_required_udp_ports (nvt) : "",
nvti_required_ports (nvt) ? nvti_required_ports (nvt) : "",
nvti_dependencies (nvt) ? nvti_dependencies (nvt) : "",
nvti_tag (nvt) ? nvti_tag (nvt) : "", cves ? cves : "", bids ? bids : "",
xrefs ? xrefs : "", nvti_category (nvt), nvti_family (nvt),
nvti_name (nvt));
g_free (cves);
g_free (bids);
g_free (xrefs);
Expand Down Expand Up @@ -1722,7 +1727,7 @@ redis_flush_all (kb_t kb, const char *except)
*
* @return 0 on success, -1 on error.
*/
int
static int
redis_save (kb_t kb)
{
int rc;
Expand Down
2 changes: 1 addition & 1 deletion util/kb.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct kb_item
struct kb_item *next; /**< Next item in list. */

size_t namelen; /**< Name length (including final NULL byte). */
char name[0]; /**< Name of this knowledge base item. */
char name[]; /**< Name of this knowledge base item. */
};

struct kb_operations;
Expand Down
9 changes: 7 additions & 2 deletions util/ldaputils.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ ldap_log (const char *message)
* @return 0 success, -1 error.
*/
int
ldap_enable_debug ()
ldap_enable_debug (void)
{
int ret;
static int debug_level = 65535;

ret = ber_set_option (NULL, LBER_OPT_LOG_PRINT_FN, ldap_log);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
// although casting to object pointer is undefined it usually works.
// since this method is not defined by us it is ignored.
ret = ber_set_option (NULL, LBER_OPT_LOG_PRINT_FN, (void *) ldap_log);
#pragma GCC diagnostic pop
if (ret != LBER_OPT_SUCCESS)
{
g_warning ("%s: Failed to set LDAP debug print function: %s", __func__,
Expand Down
Loading

0 comments on commit 9bb86d2

Please sign in to comment.