Replace a couple sprintf() with snprintf()

This commit is contained in:
Frank Denis 2015-10-17 17:14:19 +02:00
parent 11dab6288d
commit e58f6b0abb
6 changed files with 14 additions and 14 deletions

4
auth.c
View File

@ -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, '<'))) {

View File

@ -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);

View File

@ -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);

6
lib.c
View File

@ -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':

View File

@ -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

8
lock.c
View File

@ -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);