Fixes for a recent Coverity tool run
Phil Sutter
reviewercover Fixes for a recent Coverity tool run
Phil Sutter (8):
hash: Flush tables when destroying
cache: Fix features array allocation
Fix potential buffer overrun in snprintf() calls
helpers: ftp: Avoid ugly casts
read_config_yy: Drop extra argument from dlog() call Don't call exit() from signal handler
Drop pointless assignments
connntrack: Fix for memleak when parsing -j arg
src/cache.c | 4 ++-- src/conntrack.c | 2 ++ src/hash.c | 1 + src/helpers/ftp.c | 20 +++++++++----------- src/helpers/ssdp.c | 1 - src/main.c | 2 +- src/process.c | 2 +- src/queue.c | 4 ++-- src/read_config_yy.y | 2 +- src/run.c | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 1/8 hash: Flush tables when destroying
This is cosmetics only, but stops valgrind from complaining about definitely lost memory.
--- src/hash.c | 1 + 1 file changed, 1 insertion(+)
src/hash.c +1 -0
@@ -55,6 +55,7 @@ hashtable_create(int hashsize, int limit,void hashtable_destroy (struct hashtable *h) { +hashtable_flush (h);free (h); }
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 2/8 cache: Fix features array allocation
In reply to Phil Sutterstruct cache::features is of type struct cache_feature **, allocate and populate accordingly.
--- src/cache.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/cache.c +2 -2
@@ -69,12 +69,12 @@ struct cache *cache_create(const char *name, enum cache_type type,memcpy (c->feature_type, feature_type,sizeof (feature_type)); - c->features =malloc (sizeof (struct cache_feature ) * j); + c->features =malloc (sizeof (struct cache_feature *) * j);if (!c->features) {free (c);return NULL ; } -memcpy (c->features, feature_array,sizeof (struct cache_feature ) * j); +memcpy (c->features, feature_array,sizeof (struct cache_feature *) * j); c->num_features = j; c->extra_offset = size;
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 3/8 Fix potential buffer overrun in snprintf() calls
In reply to Phil SutterWhen consecutively printing into the same buffer at increasing offset, reduce buffer size passed to snprintf() to not defeat its size checking.
--- src/process.c | 2 +- src/queue.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
src/process.c +1 -1
@@ -84,7 +84,7 @@ void fork_process_dump(int fd)int size =0 ;list_for_each_entry (this, &process_list, head) { - size += snprintf(buf+size, sizeof(buf), + size += snprintf(buf + size, sizeof(buf) - size, "PID=%u type=%s\n", this->pid, this->type < CTD_PROC_MAX ?
src/queue.c +2 -2
@@ -69,12 +69,12 @@ void queue_stats_show(int fd)int size =0 ;char buf[512 ]; - size +=snprintf (buf+size,sizeof (buf), + size +=snprintf (buf + size,sizeof (buf) - size,"allocated queue nodes:\t\t%12u\n\n" , qobjects_num);list_for_each_entry (this, &queue_list, list) { - size +=snprintf (buf+size,sizeof (buf), + size +=snprintf (buf + size,sizeof (buf) - size,"queue %s:\n" "current elements:\t\t%12u\n" "maximum elements:\t\t%12u\n"
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 4/8 helpers: ftp: Avoid ugly casts
In reply to Phil SutterCoverity tool complains about accessing a local variable at non-zero offset. Avoid this by using a helper union. This should silence the checker, although the code is still probably not Big Endian-safe.
--- src/helpers/ftp.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
src/helpers/ftp.c +9 -11
@@ -332,23 +332,21 @@ static int nf_nat_ftp_fmt_cmd(enum nf_ct_ftp_type type, char *buffer, size_t buflen, uint32_t addr, uint16_t port) { + union { + unsigned char c[4]; +uint32_t d; + } tmp; + + tmp.d = addr;switch (type) {case NF_CT_FTP_PORT :case NF_CT_FTP_PASV :return snprintf (buffer, buflen,"%u,%u,%u,%u,%u,%u" , - ((unsigned char *)&addr)[0 ], - ((unsigned char *)&addr)[1 ], - ((unsigned char *)&addr)[2 ], - ((unsigned char *)&addr)[3 ], - port >>8 , - port & 0xFF); + tmp.c[0 ], tmp.c[1 ], tmp.c[2 ], tmp.c[3 ], + port >>8 , port &0xFF );case NF_CT_FTP_EPRT :return snprintf (buffer, buflen,"|1|%u.%u.%u.%u|%u|" , - ((unsigned char *)&addr)[0 ], - ((unsigned char *)&addr)[1 ], - ((unsigned char *)&addr)[2 ], - ((unsigned char *)&addr)[3 ], - port); + tmp.c[0 ], tmp.c[1 ], tmp.c[2 ], tmp.c[3 ], port);case NF_CT_FTP_EPSV :return snprintf (buffer, buflen,"|||%u|" , port); }
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 5/8 read_config_yy: Drop extra argument from dlog() call
In reply to Phil SutterFalse priority value was never printed.
--- src/read_config_yy.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/read_config_yy.y +1 -1
@@ -1052,7 +1052,7 @@ scheduler_line : T_PRIO T_NUMBER { conf.sched.prio = $2; if (conf.sched.prio < 0 || conf.sched.prio > 99) { - dlog(LOG_ERR, "`Priority' must be [0, 99]\n", $2); + dlog(LOG_ERR, "`Priority' must be [0, 99]\n"); exit(EXIT_FAILURE); } };
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 6/8 Don't call exit() from signal handler
In reply to Phil SutterCoverity tool complains that exit() is not signal-safe and therefore should not be called from within a signal handler. Call _exit() instead.
--- src/run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/run.c +1 -1
@@ -67,7 +67,7 @@ void killer(int signo)close_log ();sd_ct_stop (); -exit (0 ); +_exit (0 ); }static void child (int foo)
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 7/8 Drop pointless assignments
In reply to Phil SutterThese variables are not referred to after assigning within their scope (or until they're overwritten).
--- src/helpers/ssdp.c | 1 - src/main.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)
src/helpers/ssdp.c +0 -1
@@ -256,7 +256,6 @@ static int find_hdr(const char *name, const uint8_t *data, int data_len, data += i+2 ; } - data_len -= name_len; data += name_len;if (pos) *pos = data;
src/main.c +1 -1
@@ -319,7 +319,7 @@ int main(int argc, char *argv[])umask (0177 ); -if ((ret =init_config (config_file)) ==-1 ) { +if (init_config (config_file) ==-1 ) {dlog (LOG_ERR ,"can't open config file `%s'" , config_file);exit (EXIT_FAILURE ); }
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 8/8 connntrack: Fix for memleak when parsing -j arg
In reply to Phil SutterHave to free the strings allocated by split_address_and_port().
--- src/conntrack.c | 2 ++ 1 file changed, 2 insertions(+)
src/conntrack.c +2 -0
@@ -3113,6 +3113,8 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])nfct_set_nat_details (c, tmpl->ct, &ad, port_str, family); } +free (port_str); +free (nat_address); }break ;case 'w' :
Signature · 1 line
2.34.1
Pablo Neira Ayuso
maintainerLGTM
Quoted 25 lines · Phil Sutter
Phil Sutter (8):
hash: Flush tables when destroying
cache: Fix features array allocation
Fix potential buffer overrun insnprintf()calls
helpers: ftp: Avoid ugly casts
read_config_yy: Drop extra argument fromdlog()call Don't callexit()from signal handler
Drop pointless assignments
connntrack: Fix for memleak when parsing -j argsrc/cache.c | 4 ++-- src/conntrack.c | 2 ++ src/hash.c | 1 + src/helpers/ftp.c | 20 +++++++++----------- src/helpers/ssdp.c | 1 - src/main.c | 2 +- src/process.c | 2 +- src/queue.c | 4 ++-- src/read_config_yy.y | 2 +- src/run.c | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-)Signature · 1 line
2.34.1