@@ -209,14 +209,15 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi
209
209
file_name_override_len = 0 ;
210
210
switch (header .fileType ()) {
211
211
.directory = > {
212
- const file_name = try stripComponents (unstripped_file_name , options .strip_components );
212
+ const file_name = stripComponents (unstripped_file_name , options .strip_components );
213
213
if (file_name .len != 0 and ! options .exclude_empty_directories ) {
214
214
try dir .makePath (file_name );
215
215
}
216
216
},
217
217
.normal = > {
218
218
if (file_size == 0 and unstripped_file_name .len == 0 ) return ;
219
- const file_name = try stripComponents (unstripped_file_name , options .strip_components );
219
+ const file_name = stripComponents (unstripped_file_name , options .strip_components );
220
+ if (file_name .len == 0 ) return error .BadFileName ;
220
221
221
222
const file = dir .createFile (file_name , .{}) catch | err | switch (err ) {
222
223
error .FileNotFound = > again : {
@@ -299,7 +300,8 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi
299
300
.hard_link = > return error .TarUnsupportedFileType ,
300
301
.symbolic_link = > {
301
302
// The file system path of the symbolic link.
302
- const file_name = try stripComponents (unstripped_file_name , options .strip_components );
303
+ const file_name = stripComponents (unstripped_file_name , options .strip_components );
304
+ if (file_name .len == 0 ) return error .BadFileName ;
303
305
// The data inside the symbolic link.
304
306
const link_name = header .linkName ();
305
307
@@ -334,24 +336,27 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi
334
336
}
335
337
}
336
338
337
- fn stripComponents (path : []const u8 , count : u32 ) ! []const u8 {
339
+ fn stripComponents (path : []const u8 , count : u32 ) []const u8 {
338
340
var i : usize = 0 ;
339
341
var c = count ;
340
342
while (c > 0 ) : (c -= 1 ) {
341
343
if (std .mem .indexOfScalarPos (u8 , path , i , '/' )) | pos | {
342
344
i = pos + 1 ;
343
345
} else {
344
- return error .TarComponentsOutsideStrippedPrefix ;
346
+ i = path .len ;
347
+ break ;
345
348
}
346
349
}
347
350
return path [i .. ];
348
351
}
349
352
350
353
test stripComponents {
351
354
const expectEqualStrings = std .testing .expectEqualStrings ;
352
- try expectEqualStrings ("a/b/c" , try stripComponents ("a/b/c" , 0 ));
353
- try expectEqualStrings ("b/c" , try stripComponents ("a/b/c" , 1 ));
354
- try expectEqualStrings ("c" , try stripComponents ("a/b/c" , 2 ));
355
+ try expectEqualStrings ("a/b/c" , stripComponents ("a/b/c" , 0 ));
356
+ try expectEqualStrings ("b/c" , stripComponents ("a/b/c" , 1 ));
357
+ try expectEqualStrings ("c" , stripComponents ("a/b/c" , 2 ));
358
+ try expectEqualStrings ("" , stripComponents ("a/b/c" , 3 ));
359
+ try expectEqualStrings ("" , stripComponents ("a/b/c" , 4 ));
355
360
}
356
361
357
362
const PaxAttributeInfo = struct {
0 commit comments