From f196de88cdd9764ddc2e4de737a960972d82fe9d Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado Gomez" Date: Fri, 16 Aug 2019 11:25:11 +0200 Subject: src: fix strncpy -Wstringop-truncation warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -Wstringop-truncation warning was introduced in GCC-8 as truncation checker for strncpy and strncat. Systems using gcc version >= 8 would receive the following warnings: read_config_yy.c: In function ‘yyparse’: read_config_yy.y:1594:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation] 1594 | strncpy(policy->name, $2, CTD_HELPER_NAME_LEN); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ read_config_yy.y:1384:2: warning: ‘strncpy’ specified bound 256 equals destination size [-Wstringop-truncation] 1384 | strncpy(conf.stats.logfile, $2, FILENAME_MAXLEN); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ read_config_yy.y:692:2: warning: ‘strncpy’ specified bound 108 equals destination size [-Wstringop-truncation] 692 | strncpy(conf.local.path, $2, UNIX_PATH_MAX); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ read_config_yy.y:169:2: warning: ‘strncpy’ specified bound 256 equals destination size [-Wstringop-truncation] 169 | strncpy(conf.lockfile, $2, FILENAME_MAXLEN); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ read_config_yy.y:119:2: warning: ‘strncpy’ specified bound 256 equals destination size [-Wstringop-truncation] 119 | strncpy(conf.logfile, $2, FILENAME_MAXLEN); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.c: In function ‘main’: main.c:168:5: warning: ‘strncpy’ specified bound 4096 equals destination size [-Wstringop-truncation] 168 | strncpy(config_file, argv[i], PATH_MAX); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fix the issue by checking for string length first. Also using snprintf instead. In addition, correct an off-by-one when warning about maximum config file path length. Signed-off-by: Jose M. Guisado Gomez Signed-off-by: Pablo Neira Ayuso --- include/conntrackd.h | 6 +++--- include/helper.h | 2 +- include/local.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/conntrackd.h b/include/conntrackd.h index 81dff22..fe9ec18 100644 --- a/include/conntrackd.h +++ b/include/conntrackd.h @@ -85,9 +85,9 @@ union inet_address { #define CONFIG(x) conf.x struct ct_conf { - char logfile[FILENAME_MAXLEN]; + char logfile[FILENAME_MAXLEN + 1]; int syslog_facility; - char lockfile[FILENAME_MAXLEN]; + char lockfile[FILENAME_MAXLEN + 1]; int hashsize; /* hashtable size */ int channel_num; int channel_default; @@ -132,7 +132,7 @@ struct ct_conf { int prio; } sched; struct { - char logfile[FILENAME_MAXLEN]; + char logfile[FILENAME_MAXLEN + 1]; int syslog_facility; size_t buffer_size; } stats; diff --git a/include/helper.h b/include/helper.h index d15c1c6..d540667 100644 --- a/include/helper.h +++ b/include/helper.h @@ -13,7 +13,7 @@ struct pkt_buff; #define CTD_HELPER_POLICY_MAX 4 struct ctd_helper_policy { - char name[CTD_HELPER_NAME_LEN]; + char name[CTD_HELPER_NAME_LEN + 1]; uint32_t expect_timeout; uint32_t expect_max; }; diff --git a/include/local.h b/include/local.h index 22859d7..9379446 100644 --- a/include/local.h +++ b/include/local.h @@ -7,12 +7,12 @@ struct local_conf { int reuseaddr; - char path[UNIX_PATH_MAX]; + char path[UNIX_PATH_MAX + 1]; }; struct local_server { int fd; - char path[UNIX_PATH_MAX]; + char path[UNIX_PATH_MAX + 1]; }; /* callback return values */ -- cgit v1.2.3