diff options
author | Jose M. Guisado Gomez <guigom@riseup.net> | 2019-08-16 11:25:11 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2019-08-17 11:53:57 +0200 |
commit | f196de88cdd9764ddc2e4de737a960972d82fe9d (patch) | |
tree | 7fd6779ee6dbcf9cda6e09078b393cae5736b3f5 /src/main.c | |
parent | de12e29bf35b1da51944c826beb34acf48d90289 (diff) |
src: fix strncpy -Wstringop-truncation warnings
-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 <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 7 |
1 files changed, 3 insertions, 4 deletions
@@ -120,8 +120,8 @@ do_chdir(const char *d) int main(int argc, char *argv[]) { + char config_file[PATH_MAX + 1] = {}; int ret, i, action = -1; - char config_file[PATH_MAX] = {}; int type = 0; struct utsname u; int version, major, minor; @@ -165,13 +165,12 @@ int main(int argc, char *argv[]) break; case 'C': if (++i < argc) { - strncpy(config_file, argv[i], PATH_MAX); - if (strlen(argv[i]) >= PATH_MAX){ - config_file[PATH_MAX-1]='\0'; + if (strlen(argv[i]) > PATH_MAX) { dlog(LOG_WARNING, "Path to config file" " to long. Cutting it down to %d" " characters", PATH_MAX); } + snprintf(config_file, PATH_MAX, "%s", argv[i]); break; } show_usage(argv[0]); |