Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/lldb/source/Core/ValueObjectConstResultImpl.cpp
Warning:line 151, column 3
Potential leak of memory pointed to by 'result_cast'

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 ValueObjectConstResultImpl.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/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -isystem /usr/include/libxml2 -D HAVE_ROUND -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/lldb/source/Core -I /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/lldb/source/Core -I /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/lldb/include -I tools/lldb/include -I include -I /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/llvm/include -I /usr/include/python3.9 -I /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/clang/include -I tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/lldb/source -I tools/lldb/source -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-16/lib/clang/16.0.0/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/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/= -O3 -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-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-stringop-truncation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/= -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-2022-08-17-052721-121489-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220816100716+b5a18de65169/lldb/source/Core/ValueObjectConstResultImpl.cpp
1//===-- ValueObjectConstResultImpl.cpp ------------------------------------===//
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#include "lldb/Core/ValueObjectConstResultImpl.h"
10
11#include "lldb/Core/Value.h"
12#include "lldb/Core/ValueObject.h"
13#include "lldb/Core/ValueObjectConstResult.h"
14#include "lldb/Core/ValueObjectConstResultCast.h"
15#include "lldb/Core/ValueObjectConstResultChild.h"
16#include "lldb/Symbol/CompilerType.h"
17#include "lldb/Target/ExecutionContext.h"
18#include "lldb/Utility/DataBufferHeap.h"
19#include "lldb/Utility/Endian.h"
20#include "lldb/Utility/Scalar.h"
21
22#include <string>
23
24namespace lldb_private {
25class DataExtractor;
26}
27namespace lldb_private {
28class Status;
29}
30
31using namespace lldb;
32using namespace lldb_private;
33
34ValueObjectConstResultImpl::ValueObjectConstResultImpl(
35 ValueObject *valobj, lldb::addr_t live_address)
36 : m_impl_backend(valobj), m_live_address(live_address),
37 m_live_address_type(eAddressTypeLoad),
38 m_address_of_backend() {}
39
40lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
41 if (m_impl_backend == nullptr)
42 return lldb::ValueObjectSP();
43
44 return m_impl_backend->ValueObject::Dereference(error);
45}
46
47ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
48 size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
49 if (m_impl_backend == nullptr)
50 return nullptr;
51
52 m_impl_backend->UpdateValueIfNeeded(false);
53
54 ValueObjectConstResultChild *valobj = nullptr;
55
56 bool omit_empty_base_classes = true;
57 bool ignore_array_bounds = synthetic_array_member;
58 std::string child_name_str;
59 uint32_t child_byte_size = 0;
60 int32_t child_byte_offset = 0;
61 uint32_t child_bitfield_bit_size = 0;
62 uint32_t child_bitfield_bit_offset = 0;
63 bool child_is_base_class = false;
64 bool child_is_deref_of_parent = false;
65 uint64_t language_flags;
66
67 const bool transparent_pointers = !synthetic_array_member;
68 CompilerType compiler_type = m_impl_backend->GetCompilerType();
69 CompilerType child_compiler_type;
70
71 ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
72
73 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
74 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
75 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
76 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
77 child_is_deref_of_parent, m_impl_backend, language_flags);
78
79 // One might think we should check that the size of the children
80 // is always strictly positive, hence we could avoid creating a
81 // ValueObject if that's not the case, but it turns out there
82 // are languages out there which allow zero-size types with
83 // children (e.g. Swift).
84 if (child_compiler_type) {
85 if (synthetic_index)
86 child_byte_offset += child_byte_size * synthetic_index;
87
88 ConstString child_name;
89 if (!child_name_str.empty())
90 child_name.SetCString(child_name_str.c_str());
91
92 valobj = new ValueObjectConstResultChild(
93 *m_impl_backend, child_compiler_type, child_name, child_byte_size,
94 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
95 child_is_base_class, child_is_deref_of_parent,
96 m_live_address == LLDB_INVALID_ADDRESS(18446744073709551615UL)
97 ? m_live_address
98 : m_live_address + child_byte_offset,
99 language_flags);
100 }
101
102 return valobj;
103}
104
105lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
106 uint32_t offset, const CompilerType &type, bool can_create,
107 ConstString name_const_str) {
108 if (m_impl_backend == nullptr)
109 return lldb::ValueObjectSP();
110
111 return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
112 offset, type, can_create, name_const_str);
113}
114
115lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
116 if (m_address_of_backend.get() != nullptr)
117 return m_address_of_backend;
118
119 if (m_impl_backend == nullptr)
120 return lldb::ValueObjectSP();
121 if (m_live_address != LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
122 CompilerType compiler_type(m_impl_backend->GetCompilerType());
123
124 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
125 &m_live_address, sizeof(lldb::addr_t)));
126
127 std::string new_name("&");
128 new_name.append(m_impl_backend->GetName().AsCString(""));
129 ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
130 m_address_of_backend = ValueObjectConstResult::Create(
131 exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
132 ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
133 exe_ctx.GetAddressByteSize());
134
135 m_address_of_backend->GetValue().SetValueType(Value::ValueType::Scalar);
136 m_address_of_backend->GetValue().GetScalar() = m_live_address;
137
138 return m_address_of_backend;
139 } else
140 return m_impl_backend->ValueObject::AddressOf(error);
141}
142
143lldb::ValueObjectSP
144ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
145 if (m_impl_backend == nullptr)
1
Assuming the condition is false
2
Taking false branch
146 return lldb::ValueObjectSP();
147
148 ValueObjectConstResultCast *result_cast =
149 new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
3
Memory is allocated
150 compiler_type, m_live_address);
151 return result_cast->GetSP();
4
Potential leak of memory pointed to by 'result_cast'
152}
153
154lldb::addr_t
155ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
156 AddressType *address_type) {
157
158 if (m_impl_backend == nullptr)
159 return 0;
160
161 if (m_live_address == LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
162 return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
163 address_type);
164 }
165
166 if (address_type)
167 *address_type = m_live_address_type;
168
169 return m_live_address;
170}
171
172size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
173 uint32_t item_idx,
174 uint32_t item_count) {
175 if (m_impl_backend == nullptr)
176 return 0;
177 return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
178 item_count);
179}