[Toybox] [PATCH] tar: Fix support for long symbolic links

Daniel Mentz danielmentz at google.com
Tue Mar 19 16:38:09 PDT 2019


Similar to commit 28711d30 ("toybox: tar: Fix support for long names"),
fix the handling of symbolic links with target names longer than 100
characters.

For target names longer than 100 characters, we store the first 100
characters in the link field, and the function write_longname() takes
care of storing the complete target name. The strncpy() function is the
right tool for this copy operation. Previously, xstrncpy() has been used
which called error_exit() in cases where the target name was longer than
100 characters. However, a truncated or non-null-terminated string is
not a concern, because the tar program handles this situation correctly
while reading tar archives.

Previously, we saw the following error message:

tar: '<name of long target>' > 100 bytes
---
 toys/pending/tar.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/toys/pending/tar.c b/toys/pending/tar.c
index 97e699b4..8683fc35 100644
--- a/toys/pending/tar.c
+++ b/toys/pending/tar.c
@@ -209,11 +209,9 @@ static void add_file(char **nam, struct stat *st)
 // TODO: test preserve symlink ownership
     hdr.type = '1'+i;
     if (!(lnk = i ? xreadlink(name) : node->arg)) return perror_msg("readlink");
-// TODO: does this need NUL terminator?
     if (strlen(lnk) > sizeof(hdr.link))
       write_longname(lnk, 'K'); //write longname LINK
-// TODO: this will error_exit() if too long, not truncate.
-    xstrncpy(hdr.link, lnk, sizeof(hdr.link));
+    strncpy(hdr.link, lnk, sizeof(hdr.link));
     if (i) free(lnk);
   } else if (S_ISREG(st->st_mode)) {
     hdr.type = '0';
-- 
2.21.0.225.g810b269d1ac-goog




More information about the Toybox mailing list