Skip to content

Commit

Permalink
increase warning-level to 2 (+Wextra) and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yvs2014 committed Nov 22, 2024
1 parent 1a33152 commit f08c809
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 146 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.3.43
- increase warning-level to 2 (+Wextra) and fix warnings

## 0.3.42
- notifier position: reassure again gtk_widget_measure() call

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(SRCS "${PROJECT_NAME}.c" common.c

# mandatory
add_compile_options("-Wall")
add_compile_options("-Wextra")
check_library_exists(c sqrt "${CMAKE_LIBRARY_PATH}" HAVE_SQRT_IN_C)
if(NOT HAVE_SQRT_IN_C)
check_library_exists(m sqrt "${CMAKE_LIBRARY_PATH}" HAVE_SQRT_IN_M)
Expand Down
1 change: 1 addition & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PKGS = gtk4

CC ?= gcc
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -I.
LIBS = -lm

Expand Down
23 changes: 12 additions & 11 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ static char* reorder_patt(const char *str, const char *patt) {
static void reorder_elems(const char *str, t_elem_desc *desc) {
if (!str || !desc || !desc->elems || !desc->patt) return;
char *order = reorder_patt(str, desc->patt);
int len = desc->mm.max - desc->mm.min + 1;
int num = desc->mm.max - desc->mm.min + 1;
size_t len = (num > 0) ? num : 0;
type2ndx_fn type2ndx = desc->t2n;
if (type2ndx && order && (len > 0) && (strnlen(order, len + 1) == len)) {
t_type_elem *elems = &desc->elems[desc->mm.min];
Expand All @@ -323,7 +324,7 @@ static void reorder_elems(const char *str, t_elem_desc *desc) {
g_free(order);
}

static char* cli_char_opts(int type, const char *value, int cat, t_elem_desc *desc, int max, const char *hdr) {
static char* cli_char_opts(int type, const char *value, unsigned cat, t_elem_desc *desc, int max, const char *hdr) {
if (!desc || !desc->elems) return NULL;
CLI_REORDER_PRINT_ELEMS(desc->elems, max);
clean_elems(type);
Expand All @@ -336,7 +337,7 @@ static char* cli_char_opts(int type, const char *value, int cat, t_elem_desc *de
#define RECAP_TYPE_EXPAND(type) (((type) == 't') ? "text" : (((type) == 'c') ? "csv" : \
((g_ascii_tolower(type) == 'j') ? "json" : "unknown")))

static gboolean cli_opt_elem(const char *name, const char *value, GError **error, int cat) {
static gboolean cli_opt_elem(const char *name, const char *value, GError **error, unsigned cat) {
if (!value) return false;
char *str = NULL;
switch (cat) {
Expand Down Expand Up @@ -370,28 +371,28 @@ static gboolean cli_opt_elem(const char *name, const char *value, GError **error
CLI_SET_ERR; return false;
}

static gboolean cli_opt_p(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_p(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_PAD);
}

static gboolean cli_opt_I(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_I(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_INFO);
}

static gboolean cli_opt_S(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_S(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_STAT);
}

static gboolean cli_opt_L(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_L(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_GRLG);
}

static gboolean cli_opt_G(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_G(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_GREX);
}

#ifdef WITH_PLOT
static gboolean cli_opt_P(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_P(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_PLEL);
}
#endif
Expand All @@ -417,11 +418,11 @@ static gboolean cli_opt_T(const char *name, const char *value, t_opts *opts, GEr
return okay;
}

static gboolean cli_opt_r(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_r(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
return cli_opt_elem(name, value, error, OPT_TYPE_RECAP);
}

static gboolean cli_opt_A(const char *name, const char *value, void *unused, GError **error) {
static gboolean cli_opt_A(const char *name, const char *value, t_opts *opts G_GNUC_UNUSED, GError **error) {
int aopt = activetab + 1;
gboolean valid = cli_int_opt(name, value, error, OPT_ATAB_HDR, ATAB_MIN + 1, ATAB_MAX + 1, &aopt);
if (valid) activetab = aopt - 1;
Expand Down
22 changes: 11 additions & 11 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ const int n_colors = G_N_ELEMENTS(colors);

//

static int elem_type2ndx(int type, t_type_elem *elem, int max) {
for (int i = 0; i < max; i++) if (type == elem[i].type) return i;
static unsigned elem_type2ndx(int type, t_type_elem *elem, unsigned max) {
for (unsigned i = 0; i < max; i++) if (type == elem[i].type) return i;
return -1;
}
int pingelem_type2ndx(int type) { return elem_type2ndx(type, pingelem, G_N_ELEMENTS(pingelem)); }
int graphelem_type2ndx(int type) { return elem_type2ndx(type, graphelem, G_N_ELEMENTS(graphelem)); }
unsigned pingelem_type2ndx(int type) { return elem_type2ndx(type, pingelem, G_N_ELEMENTS(pingelem)); }
unsigned graphelem_type2ndx(int type) { return elem_type2ndx(type, graphelem, G_N_ELEMENTS(graphelem)); }
#ifdef WITH_PLOT
int plotelem_type2ndx(int type) { return elem_type2ndx(type, plotelem, G_N_ELEMENTS(plotelem)); }
unsigned plotelem_type2ndx(int type) { return elem_type2ndx(type, plotelem, G_N_ELEMENTS(plotelem)); }
#endif

static const char* char_pattern[] = { [INFO_CHAR] = INFO_PATT, [STAT_CHAR] = STAT_PATT,
Expand Down Expand Up @@ -224,20 +224,20 @@ gboolean* pingelem_enabler(int type) { return elem_enabler(type, pingelem, G_N
gboolean* graphelem_enabler(int type) { return elem_enabler(type, graphelem, G_N_ELEMENTS(graphelem)); }

gboolean is_grelem_enabled(int type) {
int ndx = graphelem_type2ndx(type);
return ((ndx >= 0) && (ndx < G_N_ELEMENTS(graphelem))) ? graphelem[ndx].enable : false;
unsigned ndx = graphelem_type2ndx(type);
return (ndx < G_N_ELEMENTS(graphelem)) ? graphelem[ndx].enable : false;
}

#ifdef WITH_PLOT
gboolean* plotelem_enabler(int type) { return elem_enabler(type, plotelem, G_N_ELEMENTS(plotelem)); }

gboolean is_plelem_enabled(int type) {
int ndx = plotelem_type2ndx(type);
return ((ndx >= 0) && (ndx < G_N_ELEMENTS(plotelem))) ? plotelem[ndx].enable : false;
unsigned ndx = plotelem_type2ndx(type);
return (ndx < G_N_ELEMENTS(plotelem)) ? plotelem[ndx].enable : false;
}
#endif

#define CLEAN_ELEM_LOOP(elems, min, max) { for (int i = 0; i < G_N_ELEMENTS(elems); i++) \
#define CLEAN_ELEM_LOOP(elems, min, max) { for (unsigned i = 0; i < G_N_ELEMENTS(elems); i++) \
if (((min) <= (elems)[i].type) && ((elems)[i].type <= (max))) (elems)[i].enable = false; }

void clean_elems(int type) {
Expand Down Expand Up @@ -325,7 +325,7 @@ void print_refs(GSList *refs, const char *prefix) {
}
#endif

GSList* list_add_nodup(GSList **list, void *data, GCompareFunc cmp, int max) {
GSList* list_add_nodup(GSList **list, void *data, GCompareFunc cmp, unsigned max) {
if (!list || !data || !cmp) return NULL;
GSList *elem = g_slist_find_custom(*list, data, cmp);
if (elem) { g_free(data); return elem; }
Expand Down
20 changes: 9 additions & 11 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
#include "config.h"

#define APPNAME "pingpath"
#define VERSION "0.3.42"

#ifndef WITH_PLOT
//#define WITH_PLOT // tmp
#endif
#define VERSION "0.3.43"

enum { X_RES = 1024, Y_RES = 720 };
enum { MAXADDR = 10 };
Expand Down Expand Up @@ -490,14 +486,16 @@ typedef struct t_type_elem {

typedef struct t_legend { const char *name, *as, *cc, *av, *jt; } t_legend;

typedef int (*type2ndx_fn)(int);
typedef unsigned (*type2ndx_fn)(int);
typedef int (*type2ent_fn)(int);

typedef struct elem_desc {
t_type_elem *elems;
const t_minmax mm;
const int cat;
const char *patt;
type2ndx_fn t2n, t2e;
type2ndx_fn t2n;
type2ent_fn t2e;
} t_elem_desc;

typedef struct th_color { double ondark, onlight; } t_th_color;
Expand Down Expand Up @@ -530,13 +528,13 @@ char* get_nth_color(int nth);
int char2ndx(int cat, gboolean ent, char ch);
gboolean* pingelem_enabler(int type);
gboolean* graphelem_enabler(int type);
int pingelem_type2ndx(int type);
int graphelem_type2ndx(int type);
unsigned pingelem_type2ndx(int type);
unsigned graphelem_type2ndx(int type);
gboolean is_grelem_enabled(int type);
void clean_elems(int type);
#ifdef WITH_PLOT
gboolean* plotelem_enabler(int type);
int plotelem_type2ndx(int type);
unsigned plotelem_type2ndx(int type);
gboolean is_plelem_enabled(int type);
#endif

Expand All @@ -549,7 +547,7 @@ int host_cmp(const void *a, const void *b);
int ref_cmp(const t_ref *a, const t_ref *b);
t_ref* ref_new(t_hop *hop, int ndx);
void print_refs(GSList *refs, const char *prefix);
GSList* list_add_nodup(GSList **list, void *data, GCompareFunc cmp, int max);
GSList* list_add_nodup(GSList **list, void *data, GCompareFunc cmp, unsigned max);
GSList* list_add_ref(GSList **list, t_hop *hop, int ndx);
extern void log_add(const char *fmt, ...);

Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pingpath (0.3.43) mantic noble; urgency=low

* increase warning-level to 2 (+Wextra) and fix warnings

-- yvs <[email protected]> Fri, 22 Nov 2024 15:14:27 +0200

pingpath (0.3.42) mantic noble; urgency=low

* notifier position: reassure again gtk_widget_measure() call
Expand Down
13 changes: 10 additions & 3 deletions dns.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include <stdlib.h>

#include "dns.h"
#include "common.h"
#include "dns.h"


typedef struct dns_elem { // network stuff
Expand Down Expand Up @@ -182,13 +182,20 @@ void dns_lookup(t_hop *hop, int ndx) {
} else FAIL("g_resolver_get_default()");
}

static void dns_host_on_free(gpointer data, gpointer user_data G_GNUC_UNUSED) {
host_free(data);
}
static void dns_query_on_free(gpointer data, gpointer user_data G_GNUC_UNUSED) {
dns_query_free(data);
}

void dns_cache_free(void) {
PR_DNS_C;
g_slist_foreach(dns_cache, (GFunc)host_free, NULL);
g_slist_foreach(dns_cache, dns_host_on_free, NULL);
g_slist_free_full(dns_cache, g_free);
dns_cache = NULL;
PR_DNS_Q;
g_slist_foreach(dns_query, (GFunc)dns_query_free, NULL);
g_slist_foreach(dns_query, dns_query_on_free, NULL);
g_slist_free_full(dns_query, g_free);
dns_query = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

project('pingpath', 'c',
default_options: [ # 'c_std=gnu99,c99',
'warning_level=1' ],
'warning_level=2' ],
meson_version: '>= 0.53',
license: 'GPL-2.0-or-later',
version: '.'.join(['0', '3', find_program('git', required: false).found() ?
Expand Down
18 changes: 9 additions & 9 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ gboolean parser_init(void) {
hostname_char0_regex = compile_regex("^[" DIGIT_OR_LETTER ":]", 0);
hostname_chars_regex = compile_regex("^[" DIGIT_OR_LETTER ":.-]+$", 0);
gboolean okay = multiline_regex && hostname_char0_regex && hostname_chars_regex;
for (int i = 0; i < G_N_ELEMENTS(regexes); i++) {
for (unsigned i = 0; i < G_N_ELEMENTS(regexes); i++) {
regexes[i].rx.regex = compile_regex(regexes[i].rx.pattern, 0);
if (!regexes[i].rx.regex) okay = false;
}
for (int i = 0; i < G_N_ELEMENTS(str_rx); i++) {
for (unsigned i = 0; i < G_N_ELEMENTS(str_rx); i++) {
str_rx[i].regex = compile_regex(str_rx[i].pattern, 0);
if (!str_rx[i].regex) okay = false;
}
Expand Down Expand Up @@ -285,9 +285,9 @@ gboolean parser_mmint(const char *str, const char *option, t_minmax minmax, int
return okay;
}

char* parser_str(const char *str, const char *option, int cat) {
char* parser_str(const char *str, const char *option, unsigned cat) {
const int PARSE_MAX_CHARS = 128;
if ((cat >= 0) && (cat < G_N_ELEMENTS(str_rx))) {
if (cat < G_N_ELEMENTS(str_rx)) {
char *buff = g_strndup(str, PARSE_MAX_CHARS);
if (buff) {
char *val = g_strdup(g_strstrip(buff)); g_free(buff);
Expand Down Expand Up @@ -318,7 +318,7 @@ char* parser_valid_target(const char *target) {
void parser_whois(char *buff, t_whois *welem) {
// if there are multiple sources (despite -m query), take the last tag and mark it with '*'
static const char skip_as_prfx[] = "AS";
static int as_prfx_len = sizeof(skip_as_prfx) - 1;
static unsigned as_prfx_len = sizeof(skip_as_prfx) - 1;
if (!buff || !welem) return;
memset(welem, 0, sizeof(*welem)); // BUFFNOLINT
char *str = NULL;
Expand Down Expand Up @@ -371,16 +371,16 @@ gboolean parser_range(char *range, char delim, const char *option, t_minmax *min
}

#ifdef WITH_PLOT
gboolean parser_ivec(char *range, char delim, const char *option, int *dest, int max) {
gboolean parser_ivec(char *range, char delim, const char *option, int *dest, unsigned max) {
if (!range || (max <= 0)) return false;
const char delims[] = { delim, 0 };
char **abcs = g_strsplit(range, delims, max);
int val[max]; for (int i = 0; i < max; i++) val[i] = INT_MIN;
int val[max]; for (unsigned i = 0; i < max; i++) val[i] = INT_MIN;
gboolean okay = true;
if (abcs) for (int i = 0; abcs[i] && (i < G_N_ELEMENTS(val)); i++)
if (abcs) for (unsigned i = 0; abcs[i] && (i < G_N_ELEMENTS(val)); i++)
okay &= parser_valint(abcs[i], &val[i], option);
g_strfreev(abcs);
if (okay) for (int i = 0; i < max; i++) dest[i] = val[i];
if (okay) for (unsigned i = 0; i < max; i++) dest[i] = val[i];
return okay;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ enum { OPT_TYPE_INT, OPT_TYPE_PAD, OPT_TYPE_INFO, OPT_TYPE_STAT, OPT_TYPE_GRLG,
gboolean parser_init(void);
void parser_parse(int at, char *input);
char* parser_valid_target(const char *target);
char* parser_str(const char *str, const char *option, int cat);
char* parser_str(const char *str, const char *option, unsigned cat);
void parser_whois(char *buff, t_whois* welem);
gboolean parser_mmint(const char *str, const char *option, t_minmax minmax, int *value);
gboolean parser_range(char *range, char delim, const char *option, t_minmax *minmax);
#ifdef WITH_PLOT
gboolean parser_ivec(char *range, char delim, const char *option, int *dest, int max);
gboolean parser_ivec(char *range, char delim, const char *option, int *dest, unsigned max);
#endif

#endif
6 changes: 3 additions & 3 deletions pinger.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static void pinger_print_text(gboolean csv) {
for (int i = opts.min; i < lim; i++) { // data per hop
t_ping_column column[G_N_ELEMENTS(pingelem)] = {0};
int lines = 0;
for (int j = 0; j < G_N_ELEMENTS(pingelem); j++) if (pingelem[j].enable) {
for (unsigned j = 0; j < G_N_ELEMENTS(pingelem); j++) if (pingelem[j].enable) {
int type = pingelem[j].type;
switch (type) {
case PE_NO:
Expand Down Expand Up @@ -508,7 +508,7 @@ static gboolean create_ping(int at, t_proc *proc) {
return proc->active;
}

static int pinger_check_expired(void *unused) {
static gboolean pinger_on_expired(gpointer user_data G_GNUC_UNUSED) {
if (!atquit) for (int i = 0; i < MAXTTL; i++) if (pings[i].active) {
LOG("proc(%d) is still active after expire time, stopping..", i);
pinger_nth_stop(i, "runtime expired");
Expand Down Expand Up @@ -536,7 +536,7 @@ void pinger_start(void) {
// schedule expiration check out
if (exp_timer) { g_source_remove(exp_timer); exp_timer = 0; }
unsigned exp_in = round(opts.cycles * (opts.timeout * 1.024)) + opts.timeout * 2; // ~24msec of possible ping time resolution
exp_timer = g_timeout_add_seconds(exp_in, pinger_check_expired, NULL);
exp_timer = g_timeout_add_seconds(exp_in, pinger_on_expired, NULL);
LOG("expiration set to %d seconds for %d cycles", exp_in, opts.cycles);
}

Expand Down
Loading

0 comments on commit f08c809

Please sign in to comment.