Bug Summary

File:build/source/lldb/tools/lldb-vscode/FifoFiles.cpp
Warning:line 97, column 12
Potential leak of memory pointed to by 'future'

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 FifoFiles.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 HAVE_ROUND -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/lldb/tools/lldb-vscode -I /build/source/lldb/tools/lldb-vscode -I /build/source/lldb/include -I tools/lldb/include -I include -I /build/source/llvm/include -I /usr/include/python3.9 -I /build/source/clang/include -I tools/lldb/../clang/include -I /usr/include/libxml2 -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 -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-stringop-truncation -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/lldb/tools/lldb-vscode/FifoFiles.cpp
1//===-- FifoFiles.cpp -------------------------------------------*- 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#include "FifoFiles.h"
10
11#if !defined(_WIN32)
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15#endif
16
17#include <chrono>
18#include <fstream>
19#include <future>
20#include <optional>
21#include <thread>
22
23#include "llvm/Support/FileSystem.h"
24
25#include "lldb/lldb-defines.h"
26
27using namespace llvm;
28
29namespace lldb_vscode {
30
31FifoFile::FifoFile(StringRef path) : m_path(path) {}
32
33FifoFile::~FifoFile() {
34#if !defined(_WIN32)
35 unlink(m_path.c_str());
36#endif
37}
38
39Expected<std::shared_ptr<FifoFile>> CreateFifoFile(StringRef path) {
40#if defined(_WIN32)
41 return createStringError(inconvertibleErrorCode(), "Unimplemented");
42#else
43 if (int err = mkfifo(path.data(), 0600))
44 return createStringError(std::error_code(err, std::generic_category()),
45 "Couldn't create fifo file: %s", path.data());
46 return std::make_shared<FifoFile>(path);
47#endif
48}
49
50FifoFileIO::FifoFileIO(StringRef fifo_file, StringRef other_endpoint_name)
51 : m_fifo_file(fifo_file), m_other_endpoint_name(other_endpoint_name) {}
52
53Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
54 // We use a pointer for this future, because otherwise its normal destructor
55 // would wait for the getline to end, rendering the timeout useless.
56 std::optional<std::string> line;
57 std::future<void> *future =
58 new std::future<void>(std::async(std::launch::async, [&]() {
59 std::ifstream reader(m_fifo_file, std::ifstream::in);
60 std::string buffer;
61 std::getline(reader, buffer);
62 if (!buffer.empty())
63 line = buffer;
64 }));
65 if (future->wait_for(timeout) == std::future_status::timeout || !line)
66 // Indeed this is a leak, but it's intentional. "future" obj destructor
67 // will block on waiting for the worker thread to join. And the worker
68 // thread might be stuck in blocking I/O. Intentionally leaking the obj
69 // as a hack to avoid blocking main thread, and adding annotation to
70 // supress static code inspection warnings
71
72 // coverity[leaked_storage]
73 return createStringError(inconvertibleErrorCode(),
74 "Timed out trying to get messages from the " +
75 m_other_endpoint_name);
76 delete future;
77 return json::parse(*line);
78}
79
80Error FifoFileIO::SendJSON(const json::Value &json,
81 std::chrono::milliseconds timeout) {
82 bool done = false;
83 std::future<void> *future =
84 new std::future<void>(std::async(std::launch::async, [&]() {
1
Memory is allocated
85 std::ofstream writer(m_fifo_file, std::ofstream::out);
86 writer << JSONToString(json) << std::endl;
87 done = true;
88 }));
89 if (future->wait_for(timeout) == std::future_status::timeout || !done) {
2
Assuming 'done' is false
3
Taking true branch
90 // Indeed this is a leak, but it's intentional. "future" obj destructor will
91 // block on waiting for the worker thread to join. And the worker thread
92 // might be stuck in blocking I/O. Intentionally leaking the obj as a hack
93 // to avoid blocking main thread, and adding annotation to supress static
94 // code inspection warnings"
95
96 // coverity[leaked_storage]
97 return createStringError(inconvertibleErrorCode(),
4
Potential leak of memory pointed to by 'future'
98 "Timed out trying to send messages to the " +
99 m_other_endpoint_name);
100 }
101 delete future;
102 return Error::success();
103}
104
105} // namespace lldb_vscode