Skip to content

Commit

Permalink
toybox: tar: Fix support for long names
Browse files Browse the repository at this point in the history
Pathnames may be longer than the name field in the header, so use
strncpy() instead of xstrncpy() to avoid bailing out.

Also add unit tests to ensure proper handling of short and long
pathnames.

Change-Id: Id025891993746889564b479e5185cf9721b54a55
  • Loading branch information
tdmcyngn authored and landley committed Feb 11, 2016
1 parent 5b61086 commit 28711d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/tar.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ rm -rf temp_dir
mkdir dir/dir1 -p
echo "Inside dir/dir1" > dir/dir1/file ; echo "Hello Inside dir" > dir/file
testing "Extraction on STDOUT : -O" " tar -czf dir.tgz dir/ ; rm -rf dir ; tar -xf dir.tgz -O ; [ -e 'Inside dir/dir1/\nHello Inside dir\n' ] && echo 'yes'; rm -rf dir.tgz " "" "" ""

#Creating short filename
f="filename_with_100_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
echo "This is testdata" > $f
testing "tar shortname filename" "tar -cf testFile.tar $f && [ -e testFile.tar ] && echo 'yes'; rm -f $f; tar -xf testFile.tar && [ -f $f ] && cat $f && strings testFile.tar | grep -o LongLink; rm -f testFile.tar; rm -f $f" "yes\nThis is testdata\n" "" ""

#Creating long filename
f="filename_with_101_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
echo "This is testdata" > $f
testing "tar longname filename" "tar -cf testFile.tar $f && [ -e testFile.tar ] && echo 'yes'; rm -f $f; tar -xf testFile.tar && [ -f $f ] && cat $f && strings testFile.tar | grep -o LongLink; rm -f testFile.tar; rm -f $f" "yes\nThis is testdata\nLongLink\n" "" ""

#Creating long pathname
d="dirname_with_50_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
f="filename_with_50_chars_xxxxxxxxxxxxxxxxxxxxxxxxxxx"
mkdir $d
echo "This is testdata" > $d/$f
testing "tar longname pathname" "tar -cf testFile.tar $d/$f && [ -e testFile.tar ] && echo 'yes'; rm -rf $d; tar -xf testFile.tar && [ -f $d/$f ] && cat $d/$f && strings testFile.tar | grep -o LongLink; rm -f testFile.tar; rm -rf $d" "yes\nThis is testdata\nLongLink\n" "" ""
2 changes: 1 addition & 1 deletion toys/pending/tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static void add_file(struct archive_handler *tar, char **nam, struct stat *st)
}

memset(&hdr, 0, sizeof(hdr));
xstrncpy(hdr.name, hname, sizeof(hdr.name));
strncpy(hdr.name, hname, sizeof(hdr.name));
itoo(hdr.mode, sizeof(hdr.mode), st->st_mode &07777);
itoo(hdr.uid, sizeof(hdr.uid), st->st_uid);
itoo(hdr.gid, sizeof(hdr.gid), st->st_gid);
Expand Down

0 comments on commit 28711d3

Please sign in to comment.