Skip to content

Commit 91160d8

Browse files
committed
Fix an unconditional break in checkMachOAndArchFlags
Found by PVS-Studio. llvm-svn: 285598
1 parent f5c0689 commit 91160d8

File tree

3 files changed

+44
-52
lines changed

3 files changed

+44
-52
lines changed

llvm/tools/llvm-nm/llvm-nm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,9 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
10511051
// architectures was specificed. If not then an error is generated and this
10521052
// routine returns false. Else it returns true.
10531053
static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
1054-
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1054+
auto *MachO = dyn_cast<MachOObjectFile>(O);
10551055

1056-
if (!MachO || ArchAll || ArchFlags.size() == 0)
1056+
if (!MachO || ArchAll || ArchFlags.empty())
10571057
return true;
10581058

10591059
MachO::mach_header H;

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,30 +1186,26 @@ static void DumpInfoPlistSectionContents(StringRef Filename,
11861186
// architectures were specified. If not then an error is generated and this
11871187
// routine returns false. Else it returns true.
11881188
static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1189-
if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1190-
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1191-
bool ArchFound = false;
1192-
MachO::mach_header H;
1193-
MachO::mach_header_64 H_64;
1194-
Triple T;
1195-
if (MachO->is64Bit()) {
1196-
H_64 = MachO->MachOObjectFile::getHeader64();
1197-
T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
1198-
} else {
1199-
H = MachO->MachOObjectFile::getHeader();
1200-
T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
1201-
}
1202-
unsigned i;
1203-
for (i = 0; i < ArchFlags.size(); ++i) {
1204-
if (ArchFlags[i] == T.getArchName())
1205-
ArchFound = true;
1206-
break;
1207-
}
1208-
if (!ArchFound) {
1209-
errs() << "llvm-objdump: file: " + Filename + " does not contain "
1210-
<< "architecture: " + ArchFlags[i] + "\n";
1211-
return false;
1212-
}
1189+
auto *MachO = dyn_cast<MachOObjectFile>(O);
1190+
1191+
if (!MachO || ArchAll || ArchFlags.empty())
1192+
return true;
1193+
1194+
MachO::mach_header H;
1195+
MachO::mach_header_64 H_64;
1196+
Triple T;
1197+
if (MachO->is64Bit()) {
1198+
H_64 = MachO->MachOObjectFile::getHeader64();
1199+
T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
1200+
} else {
1201+
H = MachO->MachOObjectFile::getHeader();
1202+
T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
1203+
}
1204+
if (none_of(ArchFlags, [&](const std::string &Name) {
1205+
return Name == T.getArchName();
1206+
})) {
1207+
errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1208+
return false;
12131209
}
12141210
return true;
12151211
}

llvm/tools/llvm-size/llvm-size.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -498,36 +498,32 @@ static void printObjectSectionSizes(ObjectFile *Obj) {
498498
}
499499
}
500500

501-
/// Checks to see if the @p o ObjectFile is a Mach-O file and if it is and there
501+
/// Checks to see if the @p O ObjectFile is a Mach-O file and if it is and there
502502
/// is a list of architecture flags specified then check to make sure this
503503
/// Mach-O file is one of those architectures or all architectures was
504504
/// specificed. If not then an error is generated and this routine returns
505505
/// false. Else it returns true.
506-
static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
507-
if (isa<MachOObjectFile>(o) && !ArchAll && ArchFlags.size() != 0) {
508-
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
509-
bool ArchFound = false;
510-
MachO::mach_header H;
511-
MachO::mach_header_64 H_64;
512-
Triple T;
513-
if (MachO->is64Bit()) {
514-
H_64 = MachO->MachOObjectFile::getHeader64();
515-
T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
516-
} else {
517-
H = MachO->MachOObjectFile::getHeader();
518-
T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
519-
}
520-
unsigned i;
521-
for (i = 0; i < ArchFlags.size(); ++i) {
522-
if (ArchFlags[i] == T.getArchName())
523-
ArchFound = true;
524-
break;
525-
}
526-
if (!ArchFound) {
527-
errs() << ToolName << ": file: " << file
528-
<< " does not contain architecture: " << ArchFlags[i] << ".\n";
529-
return false;
530-
}
506+
static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
507+
auto *MachO = dyn_cast<MachOObjectFile>(O);
508+
509+
if (!MachO || ArchAll || ArchFlags.empty())
510+
return true;
511+
512+
MachO::mach_header H;
513+
MachO::mach_header_64 H_64;
514+
Triple T;
515+
if (MachO->is64Bit()) {
516+
H_64 = MachO->MachOObjectFile::getHeader64();
517+
T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
518+
} else {
519+
H = MachO->MachOObjectFile::getHeader();
520+
T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
521+
}
522+
if (none_of(ArchFlags, [&](const std::string &Name) {
523+
return Name == T.getArchName();
524+
})) {
525+
error(Filename + ": No architecture specified");
526+
return false;
531527
}
532528
return true;
533529
}

0 commit comments

Comments
 (0)