Commit af3616d0 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by Cyrill Gorcunov

util: Sanitize get_image_path

Make it shorter and simpler and use one in open_image_ro_nocheck.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 8c9c575a
...@@ -160,26 +160,16 @@ int move_img_fd(int *img_fd, int want_fd) ...@@ -160,26 +160,16 @@ int move_img_fd(int *img_fd, int want_fd)
int get_image_path(char *path, int size, const char *fmt, int pid) int get_image_path(char *path, int size, const char *fmt, int pid)
{ {
int image_dir_size = strlen(image_dir); int len;
int ret;
strncpy(path, image_dir, size); len = snprintf(path, size, "%s/", image_dir);
len += snprintf(path + len, size - len, fmt, pid);
if (size <= image_dir_size) if (len > size) {
goto err; pr_err("Image path buffer overflow %d/%d\n", size, len);
return -1;
path[image_dir_size] = '/'; }
size -= image_dir_size + 1;
ret = snprintf(path + image_dir_size + 1, size, fmt, pid);
if (ret == -1 || ret >= size)
goto err;
return 0; return 0;
err:
pr_err("can't get image path\n");
return -1;
} }
int open_image_ro_nocheck(const char *fmt, int pid) int open_image_ro_nocheck(const char *fmt, int pid)
...@@ -187,10 +177,9 @@ int open_image_ro_nocheck(const char *fmt, int pid) ...@@ -187,10 +177,9 @@ int open_image_ro_nocheck(const char *fmt, int pid)
char path[PATH_MAX]; char path[PATH_MAX];
int tmp; int tmp;
tmp = snprintf(path, sizeof(path), "%s/", image_dir); tmp = get_image_path(path, sizeof(path), fmt, pid);
snprintf(path + tmp, sizeof(path) - tmp, fmt, pid); if (tmp == 0)
tmp = open(path, O_RDONLY);
tmp = open(path, O_RDONLY);
if (tmp < 0) if (tmp < 0)
pr_perror("Can't open image %s for %d\n", fmt, pid); pr_perror("Can't open image %s for %d\n", fmt, pid);
......
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