Bug Summary

File:build/source/llvm/tools/llvm-readobj/ObjDumper.cpp
Warning:line 202, column 13
The left operand of '<' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ObjDumper.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/llvm-readobj -I /build/source/llvm/tools/llvm-readobj -I include -I /build/source/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1683717183 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-05-10-133810-16478-1 -x c++ /build/source/llvm/tools/llvm-readobj/ObjDumper.cpp
1//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements ObjDumper.
11///
12//===----------------------------------------------------------------------===//
13
14#include "ObjDumper.h"
15#include "llvm-readobj.h"
16#include "llvm/Object/Archive.h"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/FormatVariadic.h"
20#include "llvm/Support/ScopedPrinter.h"
21#include "llvm/Support/raw_ostream.h"
22#include <map>
23
24namespace llvm {
25
26static inline Error createError(const Twine &Msg) {
27 return createStringError(object::object_error::parse_failed, Msg);
28}
29
30ObjDumper::ObjDumper(ScopedPrinter &Writer, StringRef ObjName) : W(Writer) {
31 // Dumper reports all non-critical errors as warnings.
32 // It does not print the same warning more than once.
33 WarningHandler = [=](const Twine &Msg) {
34 if (Warnings.insert(Msg.str()).second)
35 reportWarning(createError(Msg), ObjName);
36 return Error::success();
37 };
38}
39
40ObjDumper::~ObjDumper() {}
41
42void ObjDumper::reportUniqueWarning(Error Err) const {
43 reportUniqueWarning(toString(std::move(Err)));
44}
45
46void ObjDumper::reportUniqueWarning(const Twine &Msg) const {
47 cantFail(WarningHandler(Msg),
48 "WarningHandler should always return ErrorSuccess");
49}
50
51static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
52 for (size_t i = 0; i < Len; i++)
53 W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
54}
55
56void ObjDumper::printAsStringList(StringRef StringContent,
57 size_t StringDataOffset) {
58 size_t StrSize = StringContent.size();
59 if (StrSize == 0)
60 return;
61 if (StrSize < StringDataOffset) {
62 reportUniqueWarning("offset (0x" + Twine::utohexstr(StringDataOffset) +
63 ") is past the end of the contents (size 0x" +
64 Twine::utohexstr(StrSize) + ")");
65 return;
66 }
67
68 const uint8_t *StrContent = StringContent.bytes_begin();
69 // Some formats contain additional metadata at the start which should not be
70 // interpreted as strings. Skip these bytes, but account for them in the
71 // string offsets.
72 const uint8_t *CurrentWord = StrContent + StringDataOffset;
73 const uint8_t *StrEnd = StringContent.bytes_end();
74
75 while (CurrentWord <= StrEnd) {
76 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
77 StrEnd - CurrentWord);
78 if (!WordSize) {
79 CurrentWord++;
80 continue;
81 }
82 W.startLine() << format("[%6tx] ", CurrentWord - StrContent);
83 printAsPrintable(W.startLine(), CurrentWord, WordSize);
84 W.startLine() << '\n';
85 CurrentWord += WordSize + 1;
86 }
87}
88
89void ObjDumper::printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
90 ArrayRef<std::string> InputFilenames,
91 const object::Archive *A) {
92 W.startLine() << "\n";
93 W.printString("File", FileStr);
94 W.printString("Format", Obj.getFileFormatName());
95 W.printString("Arch", Triple::getArchTypeName(Obj.getArch()));
96 W.printString("AddressSize",
97 std::string(formatv("{0}bit", 8 * Obj.getBytesInAddress())));
98 this->printLoadName();
99}
100
101static std::vector<object::SectionRef>
102getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
103 ArrayRef<std::string> Sections) {
104 std::vector<object::SectionRef> Ret;
105 std::map<std::string, bool> SecNames;
106 std::map<unsigned, bool> SecIndices;
107 unsigned SecIndex;
108 for (StringRef Section : Sections) {
109 if (!Section.getAsInteger(0, SecIndex))
110 SecIndices.emplace(SecIndex, false);
111 else
112 SecNames.emplace(std::string(Section), false);
113 }
114
115 SecIndex = Obj.isELF() ? 0 : 1;
116 for (object::SectionRef SecRef : Obj.sections()) {
117 StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());
118 auto NameIt = SecNames.find(std::string(SecName));
119 if (NameIt != SecNames.end())
120 NameIt->second = true;
121 auto IndexIt = SecIndices.find(SecIndex);
122 if (IndexIt != SecIndices.end())
123 IndexIt->second = true;
124 if (NameIt != SecNames.end() || IndexIt != SecIndices.end())
125 Ret.push_back(SecRef);
126 SecIndex++;
127 }
128
129 for (const std::pair<const std::string, bool> &S : SecNames)
130 if (!S.second)
131 reportWarning(
132 createError(formatv("could not find section '{0}'", S.first).str()),
133 Obj.getFileName());
134
135 for (std::pair<unsigned, bool> S : SecIndices)
136 if (!S.second)
137 reportWarning(
138 createError(formatv("could not find section {0}", S.first).str()),
139 Obj.getFileName());
140
141 return Ret;
142}
143
144void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
145 ArrayRef<std::string> Sections) {
146 bool First = true;
147 for (object::SectionRef Section :
148 getSectionRefsByNameOrIndex(Obj, Sections)) {
149 StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
150
151 if (!First)
152 W.startLine() << '\n';
153 First = false;
154 W.startLine() << "String dump of section '" << SectionName << "':\n";
155
156 StringRef SectionContent =
157 unwrapOrError(Obj.getFileName(), Section.getContents());
158 printAsStringList(SectionContent);
159 }
160}
161
162void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
163 ArrayRef<std::string> Sections) {
164 bool First = true;
165 for (object::SectionRef Section :
166 getSectionRefsByNameOrIndex(Obj, Sections)) {
167 StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
168
169 if (!First
0.1
'First' is true
)
1
Taking false branch
170 W.startLine() << '\n';
171 First = false;
172 W.startLine() << "Hex dump of section '" << SectionName << "':\n";
173
174 StringRef SectionContent =
175 unwrapOrError(Obj.getFileName(), Section.getContents());
176 const uint8_t *SecContent = SectionContent.bytes_begin();
177 const uint8_t *SecEnd = SecContent + SectionContent.size();
178
179 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
2
Loop condition is true. Entering loop body
180 const uint8_t *TmpSecPtr = SecPtr;
181 uint8_t i;
182 uint8_t k;
3
'k' declared without an initial value
183
184 W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent),
185 10);
186 W.startLine() << ' ';
187 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
4
Assuming 'TmpSecPtr' is >= 'SecEnd'
188 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
189 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
190 W.startLine() << format_hex_no_prefix(Val, 2);
191 }
192 W.startLine() << ' ';
193 }
194
195 // We need to print the correct amount of spaces to match the format.
196 // We are adding the (4 - i) last rows that are 8 characters each.
197 // Then, the (4 - i) spaces that are in between the rows.
198 // Least, if we cut in a middle of a row, we add the remaining characters,
199 // which is (8 - (k * 2)).
200 if (i
4.1
'i' is < 4
< 4)
5
Taking true branch
201 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i), ' ');
202 if (k < 4)
6
The left operand of '<' is a garbage value
203 W.startLine() << format("%*c", 8 - k * 2, ' ');
204
205 TmpSecPtr = SecPtr;
206 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
207 W.startLine() << (isPrint(TmpSecPtr[i])
208 ? static_cast<char>(TmpSecPtr[i])
209 : '.');
210
211 W.startLine() << '\n';
212 }
213 }
214}
215
216} // namespace llvm