tar: prevent empty file to be treated as valid tarball

function                                             old     new   delta
tar_main                                             879     895     +16

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-11-19 17:17:48 +01:00
parent dd1d8ab506
commit 0545e3b69a
2 changed files with 35 additions and 3 deletions

View File

@ -1196,8 +1196,14 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
/*tar_handle->offset = 0; - already is */ /*tar_handle->offset = 0; - already is */
} }
/* Zero processed headers (== empty file) is not a valid tarball.
* We (ab)use bb_got_signal as exitcode here,
* because check_errors_in_children() uses _it_ as error indicator.
*/
bb_got_signal = EXIT_FAILURE;
while (get_header_tar(tar_handle) == EXIT_SUCCESS) while (get_header_tar(tar_handle) == EXIT_SUCCESS)
continue; bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */
/* Check that every file that should have been extracted was */ /* Check that every file that should have been extracted was */
while (tar_handle->accept) { while (tar_handle->accept) {
@ -1213,8 +1219,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
close(tar_handle->src_fd); close(tar_handle->src_fd);
if (SEAMLESS_COMPRESSION || OPT_COMPRESS) { if (SEAMLESS_COMPRESSION || OPT_COMPRESS) {
/* Set bb_got_signal to 1 if a child died with !0 exitcode */
check_errors_in_children(0); check_errors_in_children(0);
}
return bb_got_signal; return bb_got_signal;
} }
return EXIT_SUCCESS;
}

View File

@ -15,6 +15,15 @@ mkdir tar.tempdir && cd tar.tempdir || exit 1
# testing "test name" "script" "expected result" "file input" "stdin" # testing "test name" "script" "expected result" "file input" "stdin"
testing "Empty file is not a tarball" '\
tar xvf - 2>&1; echo $?
' "\
tar: short read
1
" \
"" ""
SKIP=
optional FEATURE_SEAMLESS_GZ optional FEATURE_SEAMLESS_GZ
testing "Empty file is not a tarball.tar.gz" '\ testing "Empty file is not a tarball.tar.gz" '\
tar xvzf - 2>&1; echo $? tar xvzf - 2>&1; echo $?
@ -26,6 +35,22 @@ tar: short read
"" "" "" ""
SKIP= SKIP=
testing "Two zeroed blocks is a ('truncated') empty tarball" '\
dd if=/dev/zero bs=512 count=2 2>/dev/null | strace -o /tmp/zzz -tt -s99 tar xvf - 2>&1; echo $?
' "\
0
" \
"" ""
SKIP=
testing "Twenty zeroed blocks is an empty tarball" '\
dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
' "\
0
" \
"" ""
SKIP=
optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
testing "tar hardlinks and repeated files" '\ testing "tar hardlinks and repeated files" '\
rm -rf input_* test.tar 2>/dev/null rm -rf input_* test.tar 2>/dev/null