Skip to content

Commit a61ea63

Browse files
mhiramatrostedt
authored andcommitted
tools/bootconfig: Fix errno reference after printf()
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>
1 parent 50b8a74 commit a61ea63

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

‎tools/bootconfig/main.c‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ static int load_xbc_file(const char *path, char **buf)
147147
return ret;
148148
}
149149

150+
static int pr_errno(const char *msg, int err)
151+
{
152+
pr_err("%s: %d\n", msg, err);
153+
return err;
154+
}
155+
150156
static int load_xbc_from_initrd(int fd, char **buf)
151157
{
152158
struct stat stat;
@@ -162,26 +168,24 @@ static int load_xbc_from_initrd(int fd, char **buf)
162168
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
163169
return 0;
164170

165-
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
166-
pr_err("Failed to lseek: %d\n", -errno);
167-
return -errno;
168-
}
171+
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
172+
return pr_errno("Failed to lseek for magic", -errno);
173+
169174
if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
170-
return -errno;
175+
return pr_errno("Failed to read", -errno);
176+
171177
/* Check the bootconfig magic bytes */
172178
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
173179
return 0;
174180

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

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

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

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

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

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

263265
ret = stat(path, &st);
264266
if (ret < 0) {
265-
pr_err("Failed to stat %s: %d\n", path, -errno);
266-
return -errno;
267+
ret = -errno;
268+
pr_err("Failed to stat %s: %d\n", path, ret);
269+
return ret;
267270
}
268271

269272
fd = open(path, O_RDONLY);
270273
if (fd < 0) {
271-
pr_err("Failed to open initrd %s: %d\n", path, fd);
272-
return -errno;
274+
ret = -errno;
275+
pr_err("Failed to open initrd %s: %d\n", path, ret);
276+
return ret;
273277
}
274278

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

308312
fd = open(path, O_RDWR);
309313
if (fd < 0) {
310-
pr_err("Failed to open initrd %s: %d\n", path, fd);
311-
return -errno;
314+
ret = -errno;
315+
pr_err("Failed to open initrd %s: %d\n", path, ret);
316+
return ret;
312317
}
313318

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

0 commit comments

Comments
 (0)