Skip to content

Commit

Permalink
Use %z for size_t and avoid deprecated string functions
Browse files Browse the repository at this point in the history
This usage of strcpy and snprintf was certainly safe, but using them
sets off various deprecation and lint warnings. Easier to just write the
belt and suspenders version.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291256 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rnk committed Jan 6, 2017
1 parent ed5adf8 commit d130989
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/Support/TarWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void computeChecksum(UstarHeader &Hdr) {
unsigned Chksum = 0;
for (size_t I = 0; I < sizeof(Hdr); ++I)
Chksum += reinterpret_cast<uint8_t *>(&Hdr)[I];
sprintf(Hdr.Checksum, "%06o", Chksum);
snprintf(Hdr.Checksum, sizeof(Hdr.Checksum), "%06o", Chksum);
}

// Create a tar header and write it to a given output stream.
Expand All @@ -98,7 +98,7 @@ static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {

// Create a 512-byte header.
UstarHeader Hdr = {};
sprintf(Hdr.Size, "%011lo", PaxAttr.size());
snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", PaxAttr.size());
Hdr.TypeFlag = 'x'; // PAX magic
memcpy(Hdr.Magic, "ustar", 6); // Ustar magic
computeChecksum(Hdr);
Expand All @@ -114,8 +114,8 @@ static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) {
UstarHeader Hdr = {};
memcpy(Hdr.Name, Path.data(), Path.size());
strcpy(Hdr.Mode, "0000664");
sprintf(Hdr.Size, "%011lo", Size);
memcpy(Hdr.Mode, "0000664", 8);
snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", Size);
memcpy(Hdr.Magic, "ustar", 6);
computeChecksum(Hdr);
OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
Expand Down

0 comments on commit d130989

Please sign in to comment.