Commit e9d17545 authored by Rui Ueyama's avatar Rui Ueyama
Browse files

TarWriter: Fix a bug in Ustar header.

If we split a filename into `Name` and `Prefix`, `Prefix` is at most
145 bytes. We had a bug that didn't split a path correctly. This bug
was pointed out by Rafael in the post commit review.

This patch adds a unit test for TarWriter to verify the fix.

llvm-svn: 291494
parent be92d5e9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -122,7 +122,7 @@ static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
static std::pair<StringRef, StringRef> splitPath(StringRef Path) {
  if (Path.size() <= sizeof(UstarHeader::Name))
    return {"", Path};
  size_t Sep = Path.rfind('/', sizeof(UstarHeader::Name) + 1);
  size_t Sep = Path.rfind('/', sizeof(UstarHeader::Prefix) + 1);
  if (Sep == StringRef::npos)
    return {"", Path};
  return {Path.substr(0, Sep), Path.substr(Sep + 1)};
+2 −1
Original line number Diff line number Diff line
@@ -43,10 +43,11 @@ add_llvm_unittest(SupportTests
  SpecialCaseListTest.cpp
  StringPool.cpp
  SwapByteOrderTest.cpp
  TarWriterTest.cpp
  TargetParserTest.cpp
  Threading.cpp
  ThreadLocalTest.cpp
  ThreadPool.cpp
  Threading.cpp
  TimerTest.cpp
  TypeNameTest.cpp
  TrailingObjectsTest.cpp
+88 −0
Original line number Diff line number Diff line
//===- llvm/unittest/Support/TarWriterTest.cpp ----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TarWriter.h"
#include "gtest/gtest.h"

using namespace llvm;
namespace {

struct UstarHeader {
  char Name[100];
  char Mode[8];
  char Uid[8];
  char Gid[8];
  char Size[12];
  char Mtime[12];
  char Checksum[8];
  char TypeFlag;
  char Linkname[100];
  char Magic[6];
  char Version[2];
  char Uname[32];
  char Gname[32];
  char DevMajor[8];
  char DevMinor[8];
  char Prefix[155];
  char Pad[12];
};

class TarWriterTest : public ::testing::Test {};

static UstarHeader create(StringRef Base, StringRef Filename) {
  // Create a temporary file.
  SmallString<128> Path;
  std::error_code EC =
      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
  EXPECT_FALSE((bool)EC);

  // Create a tar file.
  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, Base);
  EXPECT_TRUE((bool)TarOrErr);
  std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
  Tar->append(Filename, "contents");
  Tar.release();

  // Read the tar file.
  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
  EXPECT_TRUE((bool)MBOrErr);
  std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
  sys::fs::remove(Path);
  return *reinterpret_cast<const UstarHeader *>(MB->getBufferStart());
}

TEST_F(TarWriterTest, Basics) {
  UstarHeader Hdr = create("base", "file");
  ASSERT_EQ("ustar", StringRef(Hdr.Magic));
  ASSERT_EQ("00", StringRef(Hdr.Version, 2));
  ASSERT_EQ("base/file", StringRef(Hdr.Name));
  ASSERT_EQ("00000000010", StringRef(Hdr.Size));
}

TEST_F(TarWriterTest, LongFilename) {
  UstarHeader Hdr1 = create(
      "012345678", std::string(99, 'x') + "/" + std::string(44, 'x') + "/foo");
  ASSERT_EQ("foo", StringRef(Hdr1.Name));
  ASSERT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(44, 'x'),
            StringRef(Hdr1.Prefix));

  UstarHeader Hdr2 = create(
      "012345678", std::string(99, 'x') + "/" + std::string(45, 'x') + "/foo");
  ASSERT_EQ("foo", StringRef(Hdr2.Name));
  ASSERT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(45, 'x'),
            StringRef(Hdr2.Prefix));

  UstarHeader Hdr3 = create(
      "012345678", std::string(99, 'x') + "/" + std::string(46, 'x') + "/foo");
  ASSERT_EQ(std::string(46, 'x') + "/foo", StringRef(Hdr3.Name));
  ASSERT_EQ("012345678/" + std::string(99, 'x'), StringRef(Hdr3.Prefix));
}
}