Just enjoy snprintf()

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2019-06-14 18:42:29 +00:00
parent db718d5942
commit 5976434285
5 changed files with 23 additions and 67 deletions

View File

@@ -165,7 +165,7 @@ gt_bind(int argc, char **argv)
}
char tun_name[64];
const int tun_fd = tun_create(tun_name, sizeof(tun_name) - 1, dev);
const int tun_fd = tun_create(tun_name, sizeof(tun_name), dev);
if (tun_fd == -1) {
gt_log("couldn't create tun device\n");

View File

@@ -2,6 +2,7 @@
#include "ctl.h"
#include "str.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/socket.h>
@@ -41,16 +42,14 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file)
.sun_family = AF_UNIX,
};
const char *path[] = {dir, "/", file};
const size_t len = sizeof(sun.sun_path) - 1;
int ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dir, file);
if (str_cat(sun.sun_path, len, path, COUNT(path)) == len) {
if (str_cat(NULL, len + 1, path, COUNT(path)) > len) {
if (ret <= 0 || (size_t)ret >= sizeof(sun.sun_path)) {
errno = EINVAL;
return -1;
}
}
if (dst)
*dst = sun;
return 0;

View File

@@ -1,7 +1,7 @@
#include "common.h"
#include "iface.h"
#include "str.h"
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
@@ -17,21 +17,19 @@ iface_set_mtu(const char *dev_name, size_t mtu)
.ifr_mtu = (int)mtu,
};
const size_t len = sizeof(ifr.ifr_name) - 1;
int ret = snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", dev_name);
if (str_cpy(ifr.ifr_name, len, dev_name) == len) {
if (str_len(dev_name, len + 1) > len) {
if (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) {
errno = EINTR;
return -1;
}
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
return -1;
int ret = ioctl(fd, SIOCSIFMTU, &ifr);
ret = ioctl(fd, SIOCSIFMTU, &ifr);
int err = errno;
close(fd);

View File

@@ -31,32 +31,3 @@ str_len(const char *restrict str, size_t len)
return strnlen(str, len);
}
static inline size_t
str_cat(char *dst, size_t dst_len, const char **src, size_t count)
{
if (count && !src)
return 0;
size_t len = 0;
for (size_t i = 0; i < count && dst_len > len; i++) {
size_t n = str_len(src[i], dst_len - len);
if (dst && n)
memmove(&dst[len], src[i], n);
len += n;
}
if (dst)
dst[len] = 0;
return len;
}
static inline size_t
str_cpy(char *dst, size_t dst_len, const char *src)
{
return str_cat(dst, dst_len, &src, 1);
}

View File

@@ -32,9 +32,9 @@
static int
tun_create_by_id(char *name, size_t len, unsigned id)
{
int ret = snprintf(name, len + 1, "utun%u", id);
int ret = snprintf(name, len, "utun%u", id);
if (ret <= 0 || ret > len) {
if (ret <= 0 || (size_t)ret >= len) {
errno = EINVAL;
return -1;
}
@@ -44,8 +44,9 @@ tun_create_by_id(char *name, size_t len, unsigned id)
if (fd == -1)
return -1;
struct ctl_info ci = {0};
str_cpy(ci.ctl_name, sizeof(ci.ctl_name) - 1, UTUN_CONTROL_NAME);
struct ctl_info ci = {
.ctl_name = UTUN_CONTROL_NAME,
};
if (ioctl(fd, CTLIOCGINFO, &ci)) {
int err = errno;
@@ -96,10 +97,9 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
.ifr_flags = IFF_TUN | IFF_NO_PI,
};
const size_t ifr_len = sizeof(ifr.ifr_name) - 1;
int ret = snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", dev_name);
if ((len < ifr_len) ||
(str_len(dev_name, ifr_len + 1) > ifr_len)) {
if (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) {
errno = EINVAL;
return -1;
}
@@ -109,8 +109,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
if (fd == -1)
return -1;
str_cpy(ifr.ifr_name, ifr_len, dev_name);
if (ioctl(fd, TUNSETIFF, &ifr)) {
int err = errno;
close(fd);
@@ -118,8 +116,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
return -1;
}
str_cpy(name, len, ifr.ifr_name);
return fd;
}
@@ -128,21 +124,13 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
static int
tun_create_by_name(char *name, size_t len, const char *dev_name)
{
char tmp[128];
int ret = snprintf(tmp, sizeof(tmp), "/dev/%s", dev_name);
int ret = snprintf(name, len, "/dev/%s", dev_name);
if (ret <= 0 || (size_t)ret >= sizeof(tmp)) {
if (ret <= 0 || (size_t)ret >= len) {
errno = EINVAL;
return -1;
}
if (str_cpy(name, len, dev_name) == len) {
if (str_len(dev_name, len + 1) > len) {
errno = EINVAL;
return -1;
}
}
return open(tmp, O_RDWR);
}