Skip to content

Commit 348ff9e

Browse files
ddissnathanchance
authored andcommitted
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>
1 parent 97169cd commit 348ff9e

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

usr/gen_init_cpio.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define str(s) xstr(s)
2626
#define MIN(a, b) ((a) < (b) ? (a) : (b))
2727
#define CPIO_HDR_LEN 110
28+
#define CPIO_TRAILER "TRAILER!!!"
2829
#define padlen(_off, _align) (((_align) - ((_off) & ((_align) - 1))) % (_align))
2930

3031
static char padding[512];
@@ -40,9 +41,8 @@ struct file_handler {
4041
int (*handler)(const char *line);
4142
};
4243

43-
static int push_string(const char *name)
44+
static int push_buf(const char *name, size_t name_len)
4445
{
45-
unsigned int name_len = strlen(name) + 1;
4646
ssize_t len;
4747

4848
len = write(outfd, name, name_len);
@@ -69,9 +69,8 @@ static int push_pad(size_t padlen)
6969
return 0;
7070
}
7171

72-
static int push_rest(const char *name)
72+
static int push_rest(const char *name, size_t name_len)
7373
{
74-
unsigned int name_len = strlen(name) + 1;
7574
ssize_t len;
7675

7776
len = write(outfd, name, name_len);
@@ -85,8 +84,8 @@ static int push_rest(const char *name)
8584

8685
static int cpio_trailer(void)
8786
{
88-
const char name[] = "TRAILER!!!";
8987
int len;
88+
unsigned int namesize = sizeof(CPIO_TRAILER);
9089

9190
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
9291
"%08X%08X%08X%08X%08X%08X%08X",
@@ -102,12 +101,12 @@ static int cpio_trailer(void)
102101
0, /* minor */
103102
0, /* rmajor */
104103
0, /* rminor */
105-
(unsigned)strlen(name)+1, /* namesize */
104+
namesize, /* namesize */
106105
0); /* chksum */
107106
offset += len;
108107

109108
if (len != CPIO_HDR_LEN ||
110-
push_rest(name) < 0 ||
109+
push_rest(CPIO_TRAILER, namesize) < 0 ||
111110
push_pad(padlen(offset, 512)) < 0)
112111
return -1;
113112

@@ -118,9 +117,12 @@ static int cpio_mkslink(const char *name, const char *target,
118117
unsigned int mode, uid_t uid, gid_t gid)
119118
{
120119
int len;
120+
unsigned int namesize, targetsize = strlen(target) + 1;
121121

122122
if (name[0] == '/')
123123
name++;
124+
namesize = strlen(name) + 1;
125+
124126
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
125127
"%08X%08X%08X%08X%08X%08X%08X",
126128
do_csum ? "070702" : "070701", /* magic */
@@ -130,19 +132,19 @@ static int cpio_mkslink(const char *name, const char *target,
130132
(long) gid, /* gid */
131133
1, /* nlink */
132134
(long) default_mtime, /* mtime */
133-
(unsigned)strlen(target)+1, /* filesize */
135+
targetsize, /* filesize */
134136
3, /* major */
135137
1, /* minor */
136138
0, /* rmajor */
137139
0, /* rminor */
138-
(unsigned)strlen(name) + 1,/* namesize */
140+
namesize, /* namesize */
139141
0); /* chksum */
140142
offset += len;
141143

142144
if (len != CPIO_HDR_LEN ||
143-
push_string(name) < 0 ||
145+
push_buf(name, namesize) < 0 ||
144146
push_pad(padlen(offset, 4)) < 0 ||
145-
push_string(target) < 0 ||
147+
push_buf(target, targetsize) < 0 ||
146148
push_pad(padlen(offset, 4)) < 0)
147149
return -1;
148150

@@ -172,9 +174,12 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
172174
uid_t uid, gid_t gid)
173175
{
174176
int len;
177+
unsigned int namesize;
175178

176179
if (name[0] == '/')
177180
name++;
181+
namesize = strlen(name) + 1;
182+
178183
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
179184
"%08X%08X%08X%08X%08X%08X%08X",
180185
do_csum ? "070702" : "070701", /* magic */
@@ -189,12 +194,12 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
189194
1, /* minor */
190195
0, /* rmajor */
191196
0, /* rminor */
192-
(unsigned)strlen(name) + 1,/* namesize */
197+
namesize, /* namesize */
193198
0); /* chksum */
194199
offset += len;
195200

196201
if (len != CPIO_HDR_LEN ||
197-
push_rest(name) < 0)
202+
push_rest(name, namesize) < 0)
198203
return -1;
199204

200205
return 0;
@@ -265,6 +270,7 @@ static int cpio_mknod(const char *name, unsigned int mode,
265270
unsigned int maj, unsigned int min)
266271
{
267272
int len;
273+
unsigned int namesize;
268274

269275
if (dev_type == 'b')
270276
mode |= S_IFBLK;
@@ -273,6 +279,8 @@ static int cpio_mknod(const char *name, unsigned int mode,
273279

274280
if (name[0] == '/')
275281
name++;
282+
namesize = strlen(name) + 1;
283+
276284
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
277285
"%08X%08X%08X%08X%08X%08X%08X",
278286
do_csum ? "070702" : "070701", /* magic */
@@ -287,12 +295,12 @@ static int cpio_mknod(const char *name, unsigned int mode,
287295
1, /* minor */
288296
maj, /* rmajor */
289297
min, /* rminor */
290-
(unsigned)strlen(name) + 1,/* namesize */
298+
namesize, /* namesize */
291299
0); /* chksum */
292300
offset += len;
293301

294302
if (len != CPIO_HDR_LEN ||
295-
push_rest(name) < 0)
303+
push_rest(name, namesize) < 0)
296304
return -1;
297305

298306
return 0;
@@ -426,7 +434,7 @@ static int cpio_mkfile(const char *name, const char *location,
426434
offset += len;
427435

428436
if (len != CPIO_HDR_LEN ||
429-
push_string(name) < 0 ||
437+
push_buf(name, namesize) < 0 ||
430438
push_pad(padlen(offset, 4)) < 0)
431439
goto error;
432440

0 commit comments

Comments
 (0)