Skip to content

Commit

Permalink
tools/bootconfig: Fix errno reference after printf()
Browse files Browse the repository at this point in the history
Fix not to refer the errno variable as the result of previous libc
functions after printf() because printf() can change the errno.

Link: https://lkml.kernel.org/r/160576520243.320071.51093664672431249.stgit@devnote2

Fixes: 85c46b7 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly")
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
  • Loading branch information
Masami Hiramatsu authored and Steven Rostedt (VMware) committed Nov 19, 2020
1 parent 50b8a74 commit a61ea63
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions tools/bootconfig/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ static int load_xbc_file(const char *path, char **buf)
return ret;
}

static int pr_errno(const char *msg, int err)
{
pr_err("%s: %d\n", msg, err);
return err;
}

static int load_xbc_from_initrd(int fd, char **buf)
{
struct stat stat;
Expand All @@ -162,26 +168,24 @@ static int load_xbc_from_initrd(int fd, char **buf)
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
return 0;

if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
pr_err("Failed to lseek: %d\n", -errno);
return -errno;
}
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
return pr_errno("Failed to lseek for magic", -errno);

if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
return -errno;
return pr_errno("Failed to read", -errno);

/* Check the bootconfig magic bytes */
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
return 0;

if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
pr_err("Failed to lseek: %d\n", -errno);
return -errno;
}
if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
return pr_errno("Failed to lseek for size", -errno);

if (read(fd, &size, sizeof(u32)) < 0)
return -errno;
return pr_errno("Failed to read size", -errno);

if (read(fd, &csum, sizeof(u32)) < 0)
return -errno;
return pr_errno("Failed to read checksum", -errno);

/* Wrong size error */
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
Expand All @@ -190,10 +194,8 @@ static int load_xbc_from_initrd(int fd, char **buf)
}

if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
SEEK_SET) < 0) {
pr_err("Failed to lseek: %d\n", -errno);
return -errno;
}
SEEK_SET) < 0)
return pr_errno("Failed to lseek", -errno);

ret = load_xbc_fd(fd, buf, size);
if (ret < 0)
Expand Down Expand Up @@ -262,14 +264,16 @@ static int show_xbc(const char *path, bool list)

ret = stat(path, &st);
if (ret < 0) {
pr_err("Failed to stat %s: %d\n", path, -errno);
return -errno;
ret = -errno;
pr_err("Failed to stat %s: %d\n", path, ret);
return ret;
}

fd = open(path, O_RDONLY);
if (fd < 0) {
pr_err("Failed to open initrd %s: %d\n", path, fd);
return -errno;
ret = -errno;
pr_err("Failed to open initrd %s: %d\n", path, ret);
return ret;
}

ret = load_xbc_from_initrd(fd, &buf);
Expand Down Expand Up @@ -307,8 +311,9 @@ static int delete_xbc(const char *path)

fd = open(path, O_RDWR);
if (fd < 0) {
pr_err("Failed to open initrd %s: %d\n", path, fd);
return -errno;
ret = -errno;
pr_err("Failed to open initrd %s: %d\n", path, ret);
return ret;
}

size = load_xbc_from_initrd(fd, &buf);
Expand Down Expand Up @@ -383,9 +388,10 @@ static int apply_xbc(const char *path, const char *xbc_path)
/* Apply new one */
fd = open(path, O_RDWR | O_APPEND);
if (fd < 0) {
pr_err("Failed to open %s: %d\n", path, fd);
ret = -errno;
pr_err("Failed to open %s: %d\n", path, ret);
free(data);
return fd;
return ret;
}
/* TODO: Ensure the @path is initramfs/initrd image */
ret = write(fd, data, size + 8);
Expand Down

0 comments on commit a61ea63

Please sign in to comment.