tools/bootconfig: Fix errno reference after printf()
authorMasami Hiramatsu <mhiramat@kernel.org>
Thu, 19 Nov 2020 05:53:22 +0000 (14:53 +0900)
committerSteven Rostedt (VMware) <rostedt@goodmis.org>
Thu, 19 Nov 2020 13:55:44 +0000 (08:55 -0500)
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: 85c46b78da58 ("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>
tools/bootconfig/main.c

index eb92027..52eb2bb 100644 (file)
@@ -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;
@@ -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) {
@@ -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)
@@ -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);
@@ -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);
@@ -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);