From e58f6b0abb4ec0d6049654722a90f73bbdd5064f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 17 Oct 2015 17:14:19 +0200 Subject: [PATCH] Replace a couple sprintf() with snprintf() --- auth.c | 4 ++-- generic/tap_dev.c | 4 ++-- generic/tun_dev.c | 4 ++-- lib.c | 6 +++--- linkfd.c | 2 +- lock.c | 8 ++++---- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/auth.c b/auth.c index 02fda52..6352029 100644 --- a/auth.c +++ b/auth.c @@ -88,7 +88,7 @@ static int derive_key(struct vtun_host *host) static char *bf2cf(struct vtun_host *host) { - static char str[20], * ptr = str; + static char str[32], * ptr = str; *(ptr++) = '<'; @@ -145,7 +145,7 @@ static int cf2bf(char *str, struct vtun_host *host) char *ptr, *p; int s; - if (strlen(str) >= 20) { + if (strlen(str) >= 32) { return -1; } if ((ptr = strchr(str, '<'))) { diff --git a/generic/tap_dev.c b/generic/tap_dev.c index d74b08e..5b31176 100644 --- a/generic/tap_dev.c +++ b/generic/tap_dev.c @@ -42,12 +42,12 @@ int tap_open(char *dev) int i, fd; if( *dev ) { - sprintf(tapname, "/dev/%s", dev); + snprintf(tapname, sizeof tapname, "/dev/%s", dev); return open(tapname, O_RDWR); } for(i=0; i < 255; i++) { - sprintf(tapname, "/dev/tap%d", i); + snprintf(tapname, sizeof tapname, "/dev/tap%d", i); /* Open device */ if( (fd=open(tapname, O_RDWR)) > 0 ) { sprintf(dev, "tap%d",i); diff --git a/generic/tun_dev.c b/generic/tun_dev.c index 9b2b5a9..ca2b53d 100644 --- a/generic/tun_dev.c +++ b/generic/tun_dev.c @@ -42,12 +42,12 @@ int tun_open(char *dev) int i, fd; if( *dev ) { - sprintf(tunname, "/dev/%s", dev); + snprintf(tunname, sizeof tunname, "/dev/%s", dev); return open(tunname, O_RDWR); } for(i=0; i < 255; i++){ - sprintf(tunname, "/dev/tun%d", i); + snprintf(tunname, sizeof tunname, "/dev/tun%d", i); /* Open device */ if( (fd=open(tunname, O_RDWR)) > 0 ){ sprintf(dev, "tun%d", i); diff --git a/lib.c b/lib.c index 5a6a9ba..4a0843b 100644 --- a/lib.c +++ b/lib.c @@ -98,7 +98,7 @@ void set_title(const char *fmt, ...) /* print the argument string */ va_start(ap, fmt); - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof buf, fmt, ap); va_end(ap); if( strlen(buf) > title_size - 1) @@ -177,14 +177,14 @@ static char * subst_opt(char *str, struct vtun_sopt *opt) optr=opt->laddr; break; case 'P': - sprintf(buf,"%d",opt->lport); + snprintf(buf, sizeof buf, "%d",opt->lport); optr=buf; break; case 'a': optr=opt->raddr; break; case 'p': - sprintf(buf,"%d",opt->rport); + snprintf(buf, sizeof buf, "%d",opt->rport); optr=buf; break; case 'h': diff --git a/linkfd.c b/linkfd.c index 601254e..2a5f733 100644 --- a/linkfd.c +++ b/linkfd.c @@ -411,7 +411,7 @@ int linkfd(struct vtun_host *host) sa.sa_handler=sig_usr1; sigaction(SIGUSR1,&sa,NULL); - sprintf(file,"%s/%.20s", VTUN_STAT_DIR, host->host); + snprintf(file, sizeof file, "%s/%.20s", VTUN_STAT_DIR, host->host); if( (host->stat.file=fopen(file, "a")) ){ setvbuf(host->stat.file, NULL, _IOLBF, 0); } else diff --git a/lock.c b/lock.c index a8fd037..a4c8065 100644 --- a/lock.c +++ b/lock.c @@ -47,13 +47,13 @@ int create_lock(char * file) ret = 0; /* Create temp file */ - sprintf(tmp_file, "%s_%d_tmp\n", file, pid); + snprintf(tmp_file, sizeof tmp_file, "%s_%d_tmp\n", file, pid); if( (fd = open(tmp_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0 ){ vtun_syslog(LOG_ERR, "Can't create temp lock file %s", file); return -1; } - pid = sprintf(str, "%d\n", pid); + pid = snprintf(str, sizeof str, "%d\n", pid); if( write(fd, str, pid) == pid ){ /* Create lock file */ if( link(tmp_file, file) < 0 ){ @@ -115,7 +115,7 @@ int lock_host(struct vtun_host * host) if( host->multi == VTUN_MULTI_ALLOW ) return 0; - sprintf(lock_file, "%s/%s", VTUN_LOCK_DIR, host->host); + snprintf(lock_file, sizeof lock_file, "%s/%s", VTUN_LOCK_DIR, host->host); /* Check if lock already exists. */ if( (pid = read_lock(lock_file)) > 0 ){ @@ -156,7 +156,7 @@ void unlock_host(struct vtun_host *host) if( host->multi == VTUN_MULTI_ALLOW ) return; - sprintf(lock_file, "%s/%s", VTUN_LOCK_DIR, host->host); + snprintf(lock_file, sizeof lock_file, "%s/%s", VTUN_LOCK_DIR, host->host); if( unlink(lock_file) < 0 ) vtun_syslog(LOG_ERR, "Unable to remove lock %s", lock_file);