[cpio] Fix calculation of name lengths in CPIO headers

Commit 12ea8c4 ("[cpio] Allow for construction of parent directories
as needed") introduced a regression in constructing CPIO archive
headers for relative paths (e.g. simple filenames with no leading
slash).

Fix by counting the number of path components rather than the number
of path separators, and add some test cases to cover CPIO header
construction.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-03-12 14:15:16 +00:00
parent 5f3ecbde5a
commit d6ee9a9242
3 changed files with 274 additions and 6 deletions

View File

@@ -63,14 +63,15 @@ static unsigned int cpio_max ( struct image *image ) {
const char *name = cpio_name ( image );
unsigned int max = 0;
char c;
char p;
/* Check for existence of CPIO filename */
if ( ! name )
return 0;
/* Count number of path separators */
while ( ( ( c = *(name++) ) ) && ( c != ' ' ) ) {
if ( c == '/' )
/* Count number of path components */
for ( p = '/' ; ( ( ( c = *(name++) ) ) && ( c != ' ' ) ) ; p = c ) {
if ( ( p == '/' ) && ( c != '/' ) )
max++;
}
@@ -88,13 +89,16 @@ static size_t cpio_name_len ( struct image *image, unsigned int depth ) {
const char *name = cpio_name ( image );
size_t len;
char c;
char p;
/* Sanity check */
/* Sanity checks */
assert ( name != NULL );
assert ( depth > 0 );
/* Calculate length up to specified path depth */
for ( len = 0 ; ( ( ( c = name[len] ) ) && ( c != ' ' ) ) ; len++ ) {
if ( ( c == '/' ) && ( depth-- == 0 ) )
for ( len = 0, p = '/' ; ( ( ( c = name[len] ) ) && ( c != ' ' ) ) ;
len++, p = c ) {
if ( ( c == '/' ) && ( p != '/' ) && ( --depth == 0 ) )
break;
}