Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gen_init_cpio: avoid duplicate strlen calls

We determine the filename length for the cpio header, so shouldn't
recalculate it when writing out the filename.

Signed-off-by: David Disseldorp <ddiss@suse.de>
Reviewed-by: Nicolas Schier <nsc@kernel.org>
Link: https://lore.kernel.org/r/20250819032607.28727-5-ddiss@suse.de
Signed-off-by: Nathan Chancellor <nathan@kernel.org>

authored by

David Disseldorp and committed by
Nathan Chancellor
348ff9e3 97169cd6

+24 -16
+24 -16
usr/gen_init_cpio.c
··· 25 25 #define str(s) xstr(s) 26 26 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 27 27 #define CPIO_HDR_LEN 110 28 + #define CPIO_TRAILER "TRAILER!!!" 28 29 #define padlen(_off, _align) (((_align) - ((_off) & ((_align) - 1))) % (_align)) 29 30 30 31 static char padding[512]; ··· 41 40 int (*handler)(const char *line); 42 41 }; 43 42 44 - static int push_string(const char *name) 43 + static int push_buf(const char *name, size_t name_len) 45 44 { 46 - unsigned int name_len = strlen(name) + 1; 47 45 ssize_t len; 48 46 49 47 len = write(outfd, name, name_len); ··· 69 69 return 0; 70 70 } 71 71 72 - static int push_rest(const char *name) 72 + static int push_rest(const char *name, size_t name_len) 73 73 { 74 - unsigned int name_len = strlen(name) + 1; 75 74 ssize_t len; 76 75 77 76 len = write(outfd, name, name_len); ··· 84 85 85 86 static int cpio_trailer(void) 86 87 { 87 - const char name[] = "TRAILER!!!"; 88 88 int len; 89 + unsigned int namesize = sizeof(CPIO_TRAILER); 89 90 90 91 len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" 91 92 "%08X%08X%08X%08X%08X%08X%08X", ··· 101 102 0, /* minor */ 102 103 0, /* rmajor */ 103 104 0, /* rminor */ 104 - (unsigned)strlen(name)+1, /* namesize */ 105 + namesize, /* namesize */ 105 106 0); /* chksum */ 106 107 offset += len; 107 108 108 109 if (len != CPIO_HDR_LEN || 109 - push_rest(name) < 0 || 110 + push_rest(CPIO_TRAILER, namesize) < 0 || 110 111 push_pad(padlen(offset, 512)) < 0) 111 112 return -1; 112 113 ··· 117 118 unsigned int mode, uid_t uid, gid_t gid) 118 119 { 119 120 int len; 121 + unsigned int namesize, targetsize = strlen(target) + 1; 120 122 121 123 if (name[0] == '/') 122 124 name++; 125 + namesize = strlen(name) + 1; 126 + 123 127 len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" 124 128 "%08X%08X%08X%08X%08X%08X%08X", 125 129 do_csum ? "070702" : "070701", /* magic */ ··· 132 130 (long) gid, /* gid */ 133 131 1, /* nlink */ 134 132 (long) default_mtime, /* mtime */ 135 - (unsigned)strlen(target)+1, /* filesize */ 133 + targetsize, /* filesize */ 136 134 3, /* major */ 137 135 1, /* minor */ 138 136 0, /* rmajor */ 139 137 0, /* rminor */ 140 - (unsigned)strlen(name) + 1,/* namesize */ 138 + namesize, /* namesize */ 141 139 0); /* chksum */ 142 140 offset += len; 143 141 144 142 if (len != CPIO_HDR_LEN || 145 - push_string(name) < 0 || 143 + push_buf(name, namesize) < 0 || 146 144 push_pad(padlen(offset, 4)) < 0 || 147 - push_string(target) < 0 || 145 + push_buf(target, targetsize) < 0 || 148 146 push_pad(padlen(offset, 4)) < 0) 149 147 return -1; 150 148 ··· 174 172 uid_t uid, gid_t gid) 175 173 { 176 174 int len; 175 + unsigned int namesize; 177 176 178 177 if (name[0] == '/') 179 178 name++; 179 + namesize = strlen(name) + 1; 180 + 180 181 len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" 181 182 "%08X%08X%08X%08X%08X%08X%08X", 182 183 do_csum ? "070702" : "070701", /* magic */ ··· 194 189 1, /* minor */ 195 190 0, /* rmajor */ 196 191 0, /* rminor */ 197 - (unsigned)strlen(name) + 1,/* namesize */ 192 + namesize, /* namesize */ 198 193 0); /* chksum */ 199 194 offset += len; 200 195 201 196 if (len != CPIO_HDR_LEN || 202 - push_rest(name) < 0) 197 + push_rest(name, namesize) < 0) 203 198 return -1; 204 199 205 200 return 0; ··· 270 265 unsigned int maj, unsigned int min) 271 266 { 272 267 int len; 268 + unsigned int namesize; 273 269 274 270 if (dev_type == 'b') 275 271 mode |= S_IFBLK; ··· 279 273 280 274 if (name[0] == '/') 281 275 name++; 276 + namesize = strlen(name) + 1; 277 + 282 278 len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" 283 279 "%08X%08X%08X%08X%08X%08X%08X", 284 280 do_csum ? "070702" : "070701", /* magic */ ··· 295 287 1, /* minor */ 296 288 maj, /* rmajor */ 297 289 min, /* rminor */ 298 - (unsigned)strlen(name) + 1,/* namesize */ 290 + namesize, /* namesize */ 299 291 0); /* chksum */ 300 292 offset += len; 301 293 302 294 if (len != CPIO_HDR_LEN || 303 - push_rest(name) < 0) 295 + push_rest(name, namesize) < 0) 304 296 return -1; 305 297 306 298 return 0; ··· 434 426 offset += len; 435 427 436 428 if (len != CPIO_HDR_LEN || 437 - push_string(name) < 0 || 429 + push_buf(name, namesize) < 0 || 438 430 push_pad(padlen(offset, 4)) < 0) 439 431 goto error; 440 432