Bug Summary

File:tools/llvm-readobj/ObjDumper.cpp
Warning:line 120, column 70
The left operand of '*' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ObjDumper.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337657/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn337657/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/tools/llvm-readobj -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-23-043044-26795-1 -x c++ /build/llvm-toolchain-snapshot-7~svn337657/tools/llvm-readobj/ObjDumper.cpp -faddrsig
1//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements ObjDumper.
12///
13//===----------------------------------------------------------------------===//
14
15#include "ObjDumper.h"
16#include "Error.h"
17#include "llvm-readobj.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/ScopedPrinter.h"
21#include "llvm/Support/raw_ostream.h"
22
23namespace llvm {
24
25ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
26
27ObjDumper::~ObjDumper() {
28}
29
30static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
31 for (size_t i = 0; i < Len; i++)
32 W << (isprint(Start[i]) ? static_cast<char>(Start[i]) : '.');
33}
34
35static Expected<object::SectionRef>
36getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
37 char *StrPtr;
38 long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
39 object::SectionRef Section;
40 long SecIndex = 0;
41 for (object::SectionRef SecRef : Obj->sections()) {
42 if (*StrPtr) {
43 StringRef SectionName;
44
45 if (std::error_code E = SecRef.getName(SectionName))
46 return errorCodeToError(E);
47
48 if (SectionName == SecName)
49 return SecRef;
50 } else if (SecIndex == SectionIndex)
51 return SecRef;
52
53 SecIndex++;
54 }
55 return make_error<StringError>("invalid section reference",
56 object::object_error::parse_failed);
57}
58
59void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
60 StringRef SecName) {
61 Expected<object::SectionRef> SectionRefOrError =
62 getSecNameOrIndexAsSecRef(Obj, SecName);
63 if (!SectionRefOrError)
64 error(std::move(SectionRefOrError));
65 object::SectionRef Section = *SectionRefOrError;
66 StringRef SectionName;
67
68 if (std::error_code E = Section.getName(SectionName))
69 error(E);
70 W.startLine() << "String dump of section '" << SectionName << "':\n";
71
72 StringRef SectionContent;
73 Section.getContents(SectionContent);
74
75 const uint8_t *SecContent = SectionContent.bytes_begin();
76 const uint8_t *CurrentWord = SecContent;
77 const uint8_t *SecEnd = SectionContent.bytes_end();
78
79 while (CurrentWord <= SecEnd) {
80 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
81 SecEnd - CurrentWord);
82 if (!WordSize) {
83 CurrentWord++;
84 continue;
85 }
86 W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
87 printAsPrintable(W.startLine(), CurrentWord, WordSize);
88 W.startLine() << '\n';
89 CurrentWord += WordSize + 1;
90 }
91}
92
93void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section,
94 size_t Size) {
95 const uint8_t *SecContent = Section;
96 const uint8_t *SecEnd = Section + Size;
97 W.startLine() << "Hex dump of section '" << SecName << "':\n";
98
99 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
1
Loop condition is true. Entering loop body
100 const uint8_t *TmpSecPtr = SecPtr;
101 uint8_t i;
102 uint8_t k;
2
'k' declared without an initial value
103
104 W.startLine() << format_hex(SecPtr - SecContent, 10);
105 W.startLine() << ' ';
106 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
107 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
108 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
109 W.startLine() << format_hex_no_prefix(Val, 2);
110 }
111 W.startLine() << ' ';
112 }
113
114 // We need to print the correct amount of spaces to match the format.
115 // We are adding the (4 - i) last rows that are 8 characters each.
116 // Then, the (4 - i) spaces that are in between the rows.
117 // Least, if we cut in a middle of a row, we add the remaining characters,
118 // which is (8 - (k * 2))
119 if (i < 4)
3
Taking true branch
120 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
4
The left operand of '*' is a garbage value
121 ' ');
122
123 TmpSecPtr = SecPtr;
124 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) {
125 if (isprint(TmpSecPtr[i]))
126 W.startLine() << TmpSecPtr[i];
127 else
128 W.startLine() << '.';
129 }
130
131 W.startLine() << '\n';
132 }
133}
134
135} // namespace llvm