[NFC][libObject] clang-format Archive{.h,.cpp}

In preparation for D100651
This commit is contained in:
Jordan Rupprecht 2021-05-27 16:48:40 -07:00
parent 57646d38d5
commit e41aaea262
2 changed files with 89 additions and 93 deletions

View File

@ -54,17 +54,15 @@ public:
Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const; Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
StringRef getRawLastModified() const { StringRef getRawLastModified() const {
return StringRef(ArMemHdr->LastModified, return StringRef(ArMemHdr->LastModified, sizeof(ArMemHdr->LastModified))
sizeof(ArMemHdr->LastModified)).rtrim(' '); .rtrim(' ');
} }
Expected<unsigned> getUID() const; Expected<unsigned> getUID() const;
Expected<unsigned> getGID() const; Expected<unsigned> getGID() const;
// This returns the size of the private struct ArMemHdrType // This returns the size of the private struct ArMemHdrType
uint64_t getSizeOf() const { uint64_t getSizeOf() const { return sizeof(ArMemHdrType); }
return sizeof(ArMemHdrType);
}
private: private:
struct ArMemHdrType { struct ArMemHdrType {
@ -117,9 +115,7 @@ public:
return Header.getLastModified(); return Header.getLastModified();
} }
StringRef getRawLastModified() const { StringRef getRawLastModified() const { return Header.getRawLastModified(); }
return Header.getRawLastModified();
}
Expected<unsigned> getUID() const { return Header.getUID(); } Expected<unsigned> getUID() const { return Header.getUID(); }
Expected<unsigned> getGID() const { return Header.getGID(); } Expected<unsigned> getGID() const { return Header.getGID(); }
@ -182,9 +178,7 @@ public:
public: public:
Symbol(const Archive *p, uint32_t symi, uint32_t stri) Symbol(const Archive *p, uint32_t symi, uint32_t stri)
: Parent(p) : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
, SymbolIndex(symi)
, StringIndex(stri) {}
bool operator==(const Symbol &other) const { bool operator==(const Symbol &other) const {
return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex); return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
@ -224,14 +218,7 @@ public:
/// Size field is 10 decimal digits long /// Size field is 10 decimal digits long
static const uint64_t MaxMemberSize = 9999999999; static const uint64_t MaxMemberSize = 9999999999;
enum Kind { enum Kind { K_GNU, K_GNU64, K_BSD, K_DARWIN, K_DARWIN64, K_COFF };
K_GNU,
K_GNU64,
K_BSD,
K_DARWIN,
K_DARWIN64,
K_COFF
};
Kind kind() const { return (Kind)Format; } Kind kind() const { return (Kind)Format; }
bool isThin() const { return IsThin; } bool isThin() const { return IsThin; }
@ -250,9 +237,7 @@ public:
} }
// Cast methods. // Cast methods.
static bool classof(Binary const *v) { static bool classof(Binary const *v) { return v->isArchive(); }
return v->isArchive();
}
// check if a symbol is in the archive // check if a symbol is in the archive
Expected<Optional<Child>> findSym(StringRef name) const; Expected<Optional<Child>> findSym(StringRef name) const;

View File

@ -43,8 +43,7 @@ const char ThinMagic[] = "!<thin>\n";
void Archive::anchor() {} void Archive::anchor() {}
static Error static Error malformedError(Twine Msg) {
malformedError(Twine Msg) {
std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")"; std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
return make_error<GenericBinaryError>(std::move(StringMsg), return make_error<GenericBinaryError>(std::move(StringMsg),
object_error::parse_failed); object_error::parse_failed);
@ -77,8 +76,8 @@ ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
if (Err) { if (Err) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(StringRef(ArMemHdr->Terminator, OS.write_escaped(
sizeof(ArMemHdr->Terminator))); StringRef(ArMemHdr->Terminator, sizeof(ArMemHdr->Terminator)));
OS.flush(); OS.flush();
std::string Msg("terminator characters in archive member \"" + Buf + std::string Msg("terminator characters in archive member \"" + Buf +
"\" not the correct \"`\\n\" values for the archive " "\" not the correct \"`\\n\" values for the archive "
@ -102,14 +101,14 @@ Expected<StringRef> ArchiveMemberHeader::getRawName() const {
auto Kind = Parent->kind(); auto Kind = Parent->kind();
if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) { if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
if (ArMemHdr->Name[0] == ' ') { if (ArMemHdr->Name[0] == ' ') {
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("name contains a leading space for archive member " return malformedError("name contains a leading space for archive member "
"header at offset " + Twine(Offset)); "header at offset " +
Twine(Offset));
} }
EndCond = ' '; EndCond = ' ';
} } else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
EndCond = ' '; EndCond = ' ';
else else
EndCond = '/'; EndCond = '/';
@ -131,8 +130,8 @@ Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
// archive header is truncated to produce an error message with the name. // archive header is truncated to produce an error message with the name.
// Make sure the name field is not truncated. // Make sure the name field is not truncated.
if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) { if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t ArchiveOffset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("archive header truncated before the name field " return malformedError("archive header truncated before the name field "
"for archive member header at offset " + "for archive member header at offset " +
Twine(ArchiveOffset)); Twine(ArchiveOffset));
@ -158,21 +157,22 @@ Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(Name.substr(1).rtrim(' ')); OS.write_escaped(Name.substr(1).rtrim(' '));
OS.flush(); OS.flush();
uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t ArchiveOffset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("long name offset characters after the '/' are " return malformedError("long name offset characters after the '/' are "
"not all decimal numbers: '" + Buf + "' for " "not all decimal numbers: '" +
"archive member header at offset " + Buf + "' for archive member header at offset " +
Twine(ArchiveOffset)); Twine(ArchiveOffset));
} }
// Verify it. // Verify it.
if (StringOffset >= Parent->getStringTable().size()) { if (StringOffset >= Parent->getStringTable().size()) {
uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t ArchiveOffset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("long name offset " + Twine(StringOffset) + " past " return malformedError("long name offset " + Twine(StringOffset) +
"the end of the string table for archive member " " past the end of the string table for archive "
"header at offset " + Twine(ArchiveOffset)); "member header at offset " +
Twine(ArchiveOffset));
} }
// GNU long file names end with a "/\n". // GNU long file names end with a "/\n".
@ -196,23 +196,24 @@ Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(Name.substr(3).rtrim(' ')); OS.write_escaped(Name.substr(3).rtrim(' '));
OS.flush(); OS.flush();
uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t ArchiveOffset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("long name length characters after the #1/ are " return malformedError("long name length characters after the #1/ are "
"not all decimal numbers: '" + Buf + "' for " "not all decimal numbers: '" +
"archive member header at offset " + Buf + "' for archive member header at offset " +
Twine(ArchiveOffset)); Twine(ArchiveOffset));
} }
if (getSizeOf() + NameLength > Size) { if (getSizeOf() + NameLength > Size) {
uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t ArchiveOffset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("long name length: " + Twine(NameLength) + return malformedError("long name length: " + Twine(NameLength) +
" extends past the end of the member or archive " " extends past the end of the member or archive "
"for archive member header at offset " + "for archive member header at offset " +
Twine(ArchiveOffset)); Twine(ArchiveOffset));
} }
return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(), return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
NameLength).rtrim('\0'); NameLength)
.rtrim('\0');
} }
// It is not a long name so trim the blanks at the end of the name. // It is not a long name so trim the blanks at the end of the name.
@ -225,36 +226,43 @@ Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
Expected<uint64_t> ArchiveMemberHeader::getSize() const { Expected<uint64_t> ArchiveMemberHeader::getSize() const {
uint64_t Ret; uint64_t Ret;
if (StringRef(ArMemHdr->Size, if (StringRef(ArMemHdr->Size, sizeof(ArMemHdr->Size))
sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) { .rtrim(" ")
.getAsInteger(10, Ret)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(StringRef(ArMemHdr->Size, OS.write_escaped(
sizeof(ArMemHdr->Size)).rtrim(" ")); StringRef(ArMemHdr->Size, sizeof(ArMemHdr->Size)).rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("characters in size field in archive header are not " return malformedError("characters in size field in archive header are not "
"all decimal numbers: '" + Buf + "' for archive " "all decimal numbers: '" +
"member header at offset " + Twine(Offset)); Buf +
"' for archive "
"member header at offset " +
Twine(Offset));
} }
return Ret; return Ret;
} }
Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const { Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
unsigned Ret; unsigned Ret;
if (StringRef(ArMemHdr->AccessMode, if (StringRef(ArMemHdr->AccessMode, sizeof(ArMemHdr->AccessMode))
sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) { .rtrim(' ')
.getAsInteger(8, Ret)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(StringRef(ArMemHdr->AccessMode, OS.write_escaped(
sizeof(ArMemHdr->AccessMode)).rtrim(" ")); StringRef(ArMemHdr->AccessMode, sizeof(ArMemHdr->AccessMode))
.rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("characters in AccessMode field in archive header " return malformedError("characters in AccessMode field in archive header "
"are not all decimal numbers: '" + Buf + "' for the " "are not all decimal numbers: '" +
"archive member header at offset " + Twine(Offset)); Buf + "' for the archive member header at offset " +
Twine(Offset));
} }
return static_cast<sys::fs::perms>(Ret); return static_cast<sys::fs::perms>(Ret);
} }
@ -262,19 +270,21 @@ Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
Expected<sys::TimePoint<std::chrono::seconds>> Expected<sys::TimePoint<std::chrono::seconds>>
ArchiveMemberHeader::getLastModified() const { ArchiveMemberHeader::getLastModified() const {
unsigned Seconds; unsigned Seconds;
if (StringRef(ArMemHdr->LastModified, if (StringRef(ArMemHdr->LastModified, sizeof(ArMemHdr->LastModified))
sizeof(ArMemHdr->LastModified)).rtrim(' ') .rtrim(' ')
.getAsInteger(10, Seconds)) { .getAsInteger(10, Seconds)) {
std::string Buf; std::string Buf;
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(StringRef(ArMemHdr->LastModified, OS.write_escaped(
sizeof(ArMemHdr->LastModified)).rtrim(" ")); StringRef(ArMemHdr->LastModified, sizeof(ArMemHdr->LastModified))
.rtrim(" "));
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("characters in LastModified field in archive header " return malformedError("characters in LastModified field in archive header "
"are not all decimal numbers: '" + Buf + "' for the " "are not all decimal numbers: '" +
"archive member header at offset " + Twine(Offset)); Buf + "' for the archive member header at offset " +
Twine(Offset));
} }
return sys::toTimePoint(Seconds); return sys::toTimePoint(Seconds);
@ -290,11 +300,12 @@ Expected<unsigned> ArchiveMemberHeader::getUID() const {
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(User); OS.write_escaped(User);
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("characters in UID field in archive header " return malformedError("characters in UID field in archive header "
"are not all decimal numbers: '" + Buf + "' for the " "are not all decimal numbers: '" +
"archive member header at offset " + Twine(Offset)); Buf + "' for the archive member header at offset " +
Twine(Offset));
} }
return Ret; return Ret;
} }
@ -309,11 +320,12 @@ Expected<unsigned> ArchiveMemberHeader::getGID() const {
raw_string_ostream OS(Buf); raw_string_ostream OS(Buf);
OS.write_escaped(Group); OS.write_escaped(Group);
OS.flush(); OS.flush();
uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - uint64_t Offset =
Parent->getData().data(); reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
return malformedError("characters in GID field in archive header " return malformedError("characters in GID field in archive header "
"are not all decimal numbers: '" + Buf + "' for the " "are not all decimal numbers: '" +
"archive member header at offset " + Twine(Offset)); Buf + "' for the archive member header at offset " +
Twine(Offset));
} }
return Ret; return Ret;
} }
@ -321,15 +333,15 @@ Expected<unsigned> ArchiveMemberHeader::getGID() const {
Archive::Child::Child(const Archive *Parent, StringRef Data, Archive::Child::Child(const Archive *Parent, StringRef Data,
uint16_t StartOfFile) uint16_t StartOfFile)
: Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr), : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
Data(Data), StartOfFile(StartOfFile) { Data(Data), StartOfFile(StartOfFile) {}
}
Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err) Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
: Parent(Parent), : Parent(Parent),
Header(Parent, Start, Header(Parent, Start,
Parent Parent
? Parent->getData().size() - (Start - Parent->getData().data()) ? Parent->getData().size() - (Start - Parent->getData().data())
: 0, Err) { : 0,
Err) {
if (!Start) if (!Start)
return; return;
@ -382,8 +394,8 @@ Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
OS.flush(); OS.flush();
uint64_t Offset = Start - Parent->getData().data(); uint64_t Offset = Start - Parent->getData().data();
*Err = malformedError("long name length characters after the #1/ are " *Err = malformedError("long name length characters after the #1/ are "
"not all decimal numbers: '" + Buf + "' for " "not all decimal numbers: '" +
"archive member header at offset " + Buf + "' for archive member header at offset " +
Twine(Offset)); Twine(Offset));
return; return;
} }
@ -646,8 +658,7 @@ Archive::Archive(MemoryBufferRef Source, Error &Err)
SymbolTable = BufOrErr.get(); SymbolTable = BufOrErr.get();
if (Increment()) if (Increment())
return; return;
} } else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
Format = K_DARWIN64; Format = K_DARWIN64;
// We know that the symbol table is not an external file, but we still // We know that the symbol table is not an external file, but we still
// must check any Expected<> return value. // must check any Expected<> return value.