Bug Summary

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