utils: convert to strnprintf()

parent 3fcae2e4
...@@ -84,8 +84,8 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev, ...@@ -84,8 +84,8 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
!strcmp(direntp->d_name, "..")) !strcmp(direntp->d_name, ".."))
continue; continue;
rc = snprintf(pathname, PATH_MAX, "%s/%s", dirname, direntp->d_name); rc = strnprintf(pathname, sizeof(pathname), "%s/%s", dirname, direntp->d_name);
if (rc < 0 || rc >= PATH_MAX) { if (rc < 0) {
ERROR("The name of path is too long"); ERROR("The name of path is too long");
failed = 1; failed = 1;
continue; continue;
...@@ -267,8 +267,8 @@ char *get_rundir(void) ...@@ -267,8 +267,8 @@ char *get_rundir(void)
if (!rundir) if (!rundir)
return NULL; return NULL;
ret = snprintf(rundir, len, "%s/.cache/lxc/run/", homedir); ret = strnprintf(rundir, len, "%s/.cache/lxc/run/", homedir);
if (ret < 0 || (size_t)ret >= len) if (ret < 0)
return ret_set_errno(NULL, EIO); return ret_set_errno(NULL, EIO);
return move_ptr(rundir); return move_ptr(rundir);
...@@ -660,8 +660,8 @@ bool switch_to_ns(pid_t pid, const char *ns) ...@@ -660,8 +660,8 @@ bool switch_to_ns(pid_t pid, const char *ns)
+ LXC_NAMESPACE_NAME_MAX]; + LXC_NAMESPACE_NAME_MAX];
/* Switch to new ns */ /* Switch to new ns */
ret = snprintf(nspath, sizeof(nspath), "/proc/%d/ns/%s", pid, ns); ret = strnprintf(nspath, sizeof(nspath), "/proc/%d/ns/%s", pid, ns);
if (ret < 0 || ret >= sizeof(nspath)) if (ret < 0)
return false; return false;
fd = open(nspath, O_RDONLY | O_CLOEXEC); fd = open(nspath, O_RDONLY | O_CLOEXEC);
...@@ -734,11 +734,10 @@ char *on_path(const char *cmd, const char *rootfs) ...@@ -734,11 +734,10 @@ char *on_path(const char *cmd, const char *rootfs)
lxc_iterate_parts(entry, path, ":") { lxc_iterate_parts(entry, path, ":") {
if (rootfs) if (rootfs)
ret = snprintf(cmdpath, PATH_MAX, "%s/%s/%s", rootfs, ret = strnprintf(cmdpath, sizeof(cmdpath), "%s/%s/%s", rootfs, entry, cmd);
entry, cmd);
else else
ret = snprintf(cmdpath, PATH_MAX, "%s/%s", entry, cmd); ret = strnprintf(cmdpath, sizeof(cmdpath), "%s/%s", entry, cmd);
if (ret < 0 || ret >= PATH_MAX) if (ret < 0)
continue; continue;
if (access(cmdpath, X_OK) == 0) if (access(cmdpath, X_OK) == 0)
...@@ -788,8 +787,8 @@ char *choose_init(const char *rootfs) ...@@ -788,8 +787,8 @@ char *choose_init(const char *rootfs)
else else
tmp = empty; tmp = empty;
ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc"); ret = strnprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc");
if (ret < 0 || ret >= PATH_MAX) { if (ret < 0) {
ERROR("The name of path is too long"); ERROR("The name of path is too long");
goto out1; goto out1;
} }
...@@ -797,8 +796,8 @@ char *choose_init(const char *rootfs) ...@@ -797,8 +796,8 @@ char *choose_init(const char *rootfs)
if (access(retv, X_OK) == 0) if (access(retv, X_OK) == 0)
return retv; return retv;
ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init"); ret = strnprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init");
if (ret < 0 || ret >= PATH_MAX) { if (ret < 0) {
ERROR("The name of path is too long"); ERROR("The name of path is too long");
goto out1; goto out1;
} }
...@@ -806,8 +805,8 @@ char *choose_init(const char *rootfs) ...@@ -806,8 +805,8 @@ char *choose_init(const char *rootfs)
if (access(retv, X_OK) == 0) if (access(retv, X_OK) == 0)
return retv; return retv;
ret = snprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp); ret = strnprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp);
if (ret < 0 || ret >= PATH_MAX) { if (ret < 0) {
ERROR("The name of path is too long"); ERROR("The name of path is too long");
goto out1; goto out1;
} }
...@@ -815,8 +814,8 @@ char *choose_init(const char *rootfs) ...@@ -815,8 +814,8 @@ char *choose_init(const char *rootfs)
if (access(retv, X_OK) == 0) if (access(retv, X_OK) == 0)
return retv; return retv;
ret = snprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp); ret = strnprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp);
if (ret < 0 || ret >= PATH_MAX) { if (ret < 0) {
ERROR("The name of path is too long"); ERROR("The name of path is too long");
goto out1; goto out1;
} }
...@@ -834,8 +833,8 @@ char *choose_init(const char *rootfs) ...@@ -834,8 +833,8 @@ char *choose_init(const char *rootfs)
if (rootfs) if (rootfs)
goto out1; goto out1;
ret = snprintf(retv, PATH_MAX, "/init.lxc.static"); ret = strnprintf(retv, PATH_MAX, "/init.lxc.static");
if (ret < 0 || ret >= PATH_MAX) { if (ret < 0) {
WARN("Nonsense - name /lxc.init.static too long"); WARN("Nonsense - name /lxc.init.static too long");
goto out1; goto out1;
} }
...@@ -874,8 +873,8 @@ char *get_template_path(const char *t) ...@@ -874,8 +873,8 @@ char *get_template_path(const char *t)
if (!tpath) if (!tpath)
return NULL; return NULL;
ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); ret = strnprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
if (ret < 0 || ret >= len) { if (ret < 0) {
free(tpath); free(tpath);
return NULL; return NULL;
} }
...@@ -1094,8 +1093,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co ...@@ -1094,8 +1093,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
source_fd = openat2(beneath_fd, src, &how, sizeof(how)); source_fd = openat2(beneath_fd, src, &how, sizeof(how));
if (source_fd < 0) if (source_fd < 0)
return -errno; return -errno;
ret = snprintf(src_buf, sizeof(src_buf), "/proc/self/fd/%d", source_fd); ret = strnprintf(src_buf, sizeof(src_buf), "/proc/self/fd/%d", source_fd);
if (ret < 0 || ret >= sizeof(src_buf)) if (ret < 0)
return -EIO; return -EIO;
} else { } else {
src_buf[0] = '\0'; src_buf[0] = '\0';
...@@ -1104,8 +1103,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co ...@@ -1104,8 +1103,8 @@ int __safe_mount_beneath_at(int beneath_fd, const char *src, const char *dst, co
target_fd = openat2(beneath_fd, dst, &how, sizeof(how)); target_fd = openat2(beneath_fd, dst, &how, sizeof(how));
if (target_fd < 0) if (target_fd < 0)
return log_error_errno(-errno, errno, "Failed to open %d(%s)", beneath_fd, dst); return log_error_errno(-errno, errno, "Failed to open %d(%s)", beneath_fd, dst);
ret = snprintf(tgt_buf, sizeof(tgt_buf), "/proc/self/fd/%d", target_fd); ret = strnprintf(tgt_buf, sizeof(tgt_buf), "/proc/self/fd/%d", target_fd);
if (ret < 0 || ret >= sizeof(tgt_buf)) if (ret < 0)
return -EIO; return -EIO;
if (!is_empty_string(src_buf)) if (!is_empty_string(src_buf))
...@@ -1163,8 +1162,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype, ...@@ -1163,8 +1162,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
if (srcfd < 0) if (srcfd < 0)
return srcfd; return srcfd;
ret = snprintf(srcbuf, sizeof(srcbuf), "/proc/self/fd/%d", srcfd); ret = strnprintf(srcbuf, sizeof(srcbuf), "/proc/self/fd/%d", srcfd);
if (ret < 0 || ret >= (int)sizeof(srcbuf)) { if (ret < 0) {
close(srcfd); close(srcfd);
ERROR("Out of memory"); ERROR("Out of memory");
return -EINVAL; return -EINVAL;
...@@ -1183,8 +1182,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype, ...@@ -1183,8 +1182,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
return destfd; return destfd;
} }
ret = snprintf(destbuf, sizeof(destbuf), "/proc/self/fd/%d", destfd); ret = strnprintf(destbuf, sizeof(destbuf), "/proc/self/fd/%d", destfd);
if (ret < 0 || ret >= (int)sizeof(destbuf)) { if (ret < 0) {
if (srcfd != -1) if (srcfd != -1)
close(srcfd); close(srcfd);
...@@ -1266,8 +1265,8 @@ bool task_blocks_signal(pid_t pid, int signal) ...@@ -1266,8 +1265,8 @@ bool task_blocks_signal(pid_t pid, int signal)
size_t n = 0; size_t n = 0;
bool bret = false; bool bret = false;
ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid); ret = strnprintf(status, sizeof(status), "/proc/%d/status", pid);
if (ret < 0 || ret >= __PROC_STATUS_LEN) if (ret < 0)
return bret; return bret;
f = fopen(status, "re"); f = fopen(status, "re");
...@@ -1305,10 +1304,10 @@ int lxc_preserve_ns(const int pid, const char *ns) ...@@ -1305,10 +1304,10 @@ int lxc_preserve_ns(const int pid, const char *ns)
* are supported by the kernel by passing in the NULL or the empty * are supported by the kernel by passing in the NULL or the empty
* string. * string.
*/ */
ret = snprintf(path, __NS_PATH_LEN, "/proc/%d/ns%s%s", pid, ret = strnprintf(path, sizeof(path), "/proc/%d/ns%s%s", pid,
!ns || strcmp(ns, "") == 0 ? "" : "/", !ns || strcmp(ns, "") == 0 ? "" : "/",
!ns || strcmp(ns, "") == 0 ? "" : ns); !ns || strcmp(ns, "") == 0 ? "" : ns);
if (ret < 0 || (size_t)ret >= __NS_PATH_LEN) if (ret < 0)
return ret_errno(EIO); return ret_errno(EIO);
return open(path, O_RDONLY | O_CLOEXEC); return open(path, O_RDONLY | O_CLOEXEC);
...@@ -1404,8 +1403,8 @@ static int lxc_get_unused_loop_dev_legacy(char *loop_name) ...@@ -1404,8 +1403,8 @@ static int lxc_get_unused_loop_dev_legacy(char *loop_name)
} }
} }
ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name); ret = strnprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name);
if (ret < 0 || ret >= LO_NAME_SIZE) { if (ret < 0) {
close(fd); close(fd);
fd = -1; fd = -1;
continue; continue;
...@@ -1439,15 +1438,15 @@ static int lxc_get_unused_loop_dev(char *name_loop) ...@@ -1439,15 +1438,15 @@ static int lxc_get_unused_loop_dev(char *name_loop)
goto on_error; goto on_error;
} }
ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr); ret = strnprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr);
if (ret < 0 || ret >= LO_NAME_SIZE) if (ret < 0)
goto on_error; goto on_error;
fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC); fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC);
if (fd_tmp < 0) { if (fd_tmp < 0) {
/* on Android loop devices are moved under /dev/block, give it a shot */ /* on Android loop devices are moved under /dev/block, give it a shot */
ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/block/loop%d", loop_nr); ret = strnprintf(name_loop, LO_NAME_SIZE, "/dev/block/loop%d", loop_nr);
if (ret < 0 || ret >= LO_NAME_SIZE) if (ret < 0)
goto on_error; goto on_error;
fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC); fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC);
...@@ -1633,8 +1632,8 @@ bool lxc_nic_exists(char *nic) ...@@ -1633,8 +1632,8 @@ bool lxc_nic_exists(char *nic)
if (!strcmp(nic, "none")) if (!strcmp(nic, "none"))
return true; return true;
ret = snprintf(path, __LXC_SYS_CLASS_NET_LEN, "/sys/class/net/%s", nic); ret = strnprintf(path, sizeof(path), "/sys/class/net/%s", nic);
if (ret < 0 || (size_t)ret >= __LXC_SYS_CLASS_NET_LEN) if (ret < 0)
return false; return false;
ret = stat(path, &sb); ret = stat(path, &sb);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment