aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/TarWriter.cpp
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2017-09-27 21:38:02 +0000
committerRui Ueyama <ruiu@google.com>2017-09-27 21:38:02 +0000
commit283f56ac03cfa4168cdd45045539e28e8d9fc92e (patch)
treed290996d08d0d7812153fa3a0c0569a19ef5dac9 /llvm/lib/Support/TarWriter.cpp
parent88f2aa12d9c8677934329799026d866e2a1cfd1b (diff)
downloadllvm-283f56ac03cfa4168cdd45045539e28e8d9fc92e.zip
llvm-283f56ac03cfa4168cdd45045539e28e8d9fc92e.tar.gz
llvm-283f56ac03cfa4168cdd45045539e28e8d9fc92e.tar.bz2
Fix off-by-one error in TarWriter.
The tar format originally supported up to 99 byte filename. The two extensions are proposed later: Ustar or PAX. In the UStar extension, a pathanme is split at a '/' and its "prefix" and "suffix" are stored in different locations in the tar header. Since "prefix" can be up to 155 byte, it can represent up to 254 byte filename (but exact limit depends on the location of '/' character in a pathname.) Our TarWriter first attempt to use UStar extension and then fallback to PAX extension. But there's a bug in UStar header creation. "Suffix" part must be a NUL- terminated string, but we didn't handle it correctly. As a result, if your filename just 100 characters long, the last character was droppped. This patch fixes the issue. Differential Revision: https://reviews.llvm.org/D38149 llvm-svn: 314349
Diffstat (limited to 'llvm/lib/Support/TarWriter.cpp')
-rw-r--r--llvm/lib/Support/TarWriter.cpp57
1 files changed, 31 insertions, 26 deletions
diff --git a/llvm/lib/Support/TarWriter.cpp b/llvm/lib/Support/TarWriter.cpp
index f06abf4..5ae6474 100644
--- a/llvm/lib/Support/TarWriter.cpp
+++ b/llvm/lib/Support/TarWriter.cpp
@@ -116,34 +116,36 @@ static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
pad(OS);
}
-// In the Ustar header, a path can be split at any '/' to store
-// a path into UstarHeader::Name and UstarHeader::Prefix. This
-// function splits a given path for that purpose.
-static std::pair<StringRef, StringRef> splitPath(StringRef Path) {
- if (Path.size() <= sizeof(UstarHeader::Name))
- return {"", Path};
+// Path fits in a Ustar header if
+//
+// - Path is less than 100 characters long, or
+// - Path is in the form of "<prefix>/<name>" where <prefix> is less
+// than or equal to 155 characters long and <name> is less than 100
+// characters long. Both <prefix> and <name> can contain extra '/'.
+//
+// If Path fits in a Ustar header, updates Prefix and Name and returns true.
+// Otherwise, returns false.
+static bool splitUstar(StringRef Path, StringRef &Prefix, StringRef &Name) {
+ if (Path.size() < sizeof(UstarHeader::Name)) {
+ Name = Path;
+ return true;
+ }
+
size_t Sep = Path.rfind('/', sizeof(UstarHeader::Prefix) + 1);
if (Sep == StringRef::npos)
- return {"", Path};
- return {Path.substr(0, Sep), Path.substr(Sep + 1)};
-}
+ return false;
+ if (Path.size() - Sep - 1 >= sizeof(UstarHeader::Name))
+ return false;
-// Returns true if a given path can be stored to a Ustar header
-// without the PAX extension.
-static bool fitsInUstar(StringRef Path) {
- StringRef Prefix;
- StringRef Name;
- std::tie(Prefix, Name) = splitPath(Path);
- return Name.size() <= sizeof(UstarHeader::Name);
+ Prefix = Path.substr(0, Sep);
+ Name = Path.substr(Sep + 1);
+ return true;
}
// The PAX header is an extended format, so a PAX header needs
// to be followed by a "real" header.
-static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) {
- StringRef Prefix;
- StringRef Name;
- std::tie(Prefix, Name) = splitPath(Path);
-
+static void writeUstarHeader(raw_fd_ostream &OS, StringRef Prefix,
+ StringRef Name, size_t Size) {
UstarHeader Hdr = makeUstarHeader();
memcpy(Hdr.Name, Name.data(), Name.size());
memcpy(Hdr.Mode, "0000664", 8);
@@ -168,12 +170,15 @@ TarWriter::TarWriter(int FD, StringRef BaseDir)
// Append a given file to an archive.
void TarWriter::append(StringRef Path, StringRef Data) {
// Write Path and Data.
- std::string S = BaseDir + "/" + sys::path::convert_to_slash(Path) + "\0";
- if (fitsInUstar(S)) {
- writeUstarHeader(OS, S, Data.size());
+ std::string Fullpath = BaseDir + "/" + sys::path::convert_to_slash(Path);
+
+ StringRef Prefix;
+ StringRef Name;
+ if (splitUstar(Fullpath, Prefix, Name)) {
+ writeUstarHeader(OS, Prefix, Name, Data.size());
} else {
- writePaxHeader(OS, S);
- writeUstarHeader(OS, "", Data.size());
+ writePaxHeader(OS, Fullpath);
+ writeUstarHeader(OS, "", "", Data.size());
}
OS << Data;