@@ -128,11 +128,22 @@ insert_dir (const char *name, struct dir **root,
128
128
n -> name = grub_strndup (cb , ce - cb );
129
129
if (ptr )
130
130
{
131
+ /*
132
+ * Create the substring with the trailing NUL byte
133
+ * to be included in the cpio header.
134
+ */
135
+ char * tmp_name = grub_strndup (name , ce - name );
136
+ if (!tmp_name ) {
137
+ grub_free (n -> name );
138
+ grub_free (n );
139
+ return grub_errno ;
140
+ }
131
141
grub_dprintf ("linux" , "Creating directory %s, %s\n" , name , ce );
132
- ptr = make_header (ptr , name , ce - name ,
142
+ ptr = make_header (ptr , tmp_name , ce - name + 1 ,
133
143
040777 , 0 );
144
+ grub_free (tmp_name );
134
145
}
135
- size += ALIGN_UP ((ce - (char * ) name )
146
+ size += ALIGN_UP ((ce - (char * ) name + 1 )
136
147
+ sizeof (struct newc_head ), 4 );
137
148
* head = n ;
138
149
cur = n ;
@@ -183,7 +194,7 @@ grub_initrd_init (int argc, char *argv[],
183
194
}
184
195
initrd_ctx -> size
185
196
+= ALIGN_UP (sizeof (struct newc_head )
186
- + grub_strlen (initrd_ctx -> components [i ].newc_name ),
197
+ + grub_strlen (initrd_ctx -> components [i ].newc_name ) + 1 ,
187
198
4 );
188
199
initrd_ctx -> size += insert_dir (initrd_ctx -> components [i ].newc_name ,
189
200
& root , 0 );
@@ -194,7 +205,7 @@ grub_initrd_init (int argc, char *argv[],
194
205
else if (newc )
195
206
{
196
207
initrd_ctx -> size += ALIGN_UP (sizeof (struct newc_head )
197
- + sizeof ("TRAILER!!!" ) - 1 , 4 );
208
+ + sizeof ("TRAILER!!!" ), 4 );
198
209
free_dir (root );
199
210
root = 0 ;
200
211
newc = 0 ;
@@ -217,7 +228,7 @@ grub_initrd_init (int argc, char *argv[],
217
228
{
218
229
initrd_ctx -> size = ALIGN_UP (initrd_ctx -> size , 4 );
219
230
initrd_ctx -> size += ALIGN_UP (sizeof (struct newc_head )
220
- + sizeof ("TRAILER!!!" ) - 1 , 4 );
231
+ + sizeof ("TRAILER!!!" ), 4 );
221
232
free_dir (root );
222
233
root = 0 ;
223
234
}
@@ -269,14 +280,14 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
269
280
ptr += insert_dir (initrd_ctx -> components [i ].newc_name ,
270
281
& root , ptr );
271
282
ptr = make_header (ptr , initrd_ctx -> components [i ].newc_name ,
272
- grub_strlen (initrd_ctx -> components [i ].newc_name ),
283
+ grub_strlen (initrd_ctx -> components [i ].newc_name ) + 1 ,
273
284
0100777 ,
274
285
initrd_ctx -> components [i ].size );
275
286
newc = 1 ;
276
287
}
277
288
else if (newc )
278
289
{
279
- ptr = make_header (ptr , "TRAILER!!!" , sizeof ("TRAILER!!!" ) - 1 ,
290
+ ptr = make_header (ptr , "TRAILER!!!" , sizeof ("TRAILER!!!" ),
280
291
0 , 0 );
281
292
free_dir (root );
282
293
root = 0 ;
@@ -308,7 +319,7 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
308
319
{
309
320
grub_memset (ptr , 0 , ALIGN_UP_OVERHEAD (cursize , 4 ));
310
321
ptr += ALIGN_UP_OVERHEAD (cursize , 4 );
311
- ptr = make_header (ptr , "TRAILER!!!" , sizeof ("TRAILER!!!" ) - 1 , 0 , 0 );
322
+ ptr = make_header (ptr , "TRAILER!!!" , sizeof ("TRAILER!!!" ), 0 , 0 );
312
323
}
313
324
free_dir (root );
314
325
root = 0 ;
0 commit comments