Files
TheRock/compiler/amd-llvm/clang/test/Analysis/stream-notes-missing-close.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

180 lines
6.3 KiB
C++

// RUN: %clang_analyze_cc1 -verify %s -analyzer-output=text \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=unix.Stream
#include "Inputs/system-header-simulator.h"
char *logDump();
bool coin();
[[noreturn]] void halt();
void assert(bool b) {
if (!b)
halt();
}
//===----------------------------------------------------------------------===//
// Report for which we expect NoOwnershipChangeVisitor to add a new note.
//===----------------------------------------------------------------------===//
namespace stream_opened_in_fn_call {
// TODO: AST analysis of sink would reveal that it doesn't intent to free the
// allocated memory, but in this instance, its also the only function with
// the ability to do so, we should see a note here.
void sink(FILE *f) {
}
void f() {
sink(fopen("input.txt", "w"));
// expected-note@-1{{Stream opened here}}
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace stream_opened_in_fn_call
namespace stream_passed_to_fn_call {
void expectedClose(FILE *f) {
if (char *log = logDump()) { // expected-note{{Assuming 'log' is null}}
// expected-note@-1{{Taking false branch}}
printf("%s", log);
fclose(f);
}
} // expected-note{{Returning without closing stream object or storing it for later release}}
void f() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
if (coin()) { // expected-note{{Assuming the condition is true}}
// expected-note@-1{{Taking true branch}}
expectedClose(f); // expected-note{{Calling 'expectedClose'}}
// expected-note@-1{{Returning from 'expectedClose'}}
return; // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
}
fclose(f);
}
} // namespace stream_passed_to_fn_call
namespace stream_shared_with_ptr_of_shorter_lifetime {
void sink(FILE *f) {
FILE *Q = f;
if (coin()) // expected-note {{Assuming the condition is false}}
// expected-note@-1 {{Taking false branch}}
fclose(f);
(void)Q;
} // expected-note{{Returning without closing stream object or storing it for later release}}
void foo() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
sink(f); // expected-note {{Calling 'sink'}}
// expected-note@-1 {{Returning from 'sink'}}
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace stream_shared_with_ptr_of_shorter_lifetime
//===----------------------------------------------------------------------===//
// Report for which we *do not* expect NoOwnershipChangeVisitor add a new note,
// nor do we want it to.
//===----------------------------------------------------------------------===//
namespace stream_not_passed_to_fn_call {
void expectedClose(FILE *f) {
if (char *log = logDump()) {
printf("%s", log);
fclose(f);
}
}
void f(FILE *p) {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
expectedClose(p); // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
}
} // namespace stream_not_passed_to_fn_call
namespace stream_shared_with_ptr_of_same_lifetime {
void expectedClose(FILE *f, FILE **p) {
// NOTE: Not a job of NoOwnershipChangeVisitor, but maybe this could be
// highlighted still?
*p = f;
}
void f() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
FILE *p = NULL;
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
expectedClose(f, &p);
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace stream_shared_with_ptr_of_same_lifetime
namespace stream_passed_into_fn_that_doesnt_intend_to_free {
void expectedClose(FILE *f) {
}
void f() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
expectedClose(f);
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace stream_passed_into_fn_that_doesnt_intend_to_free
namespace stream_passed_into_fn_that_doesnt_intend_to_free2 {
void bar();
void expectedClose(FILE *f) {
// Correctly realize that calling bar() doesn't mean that this function would
// like to deallocate anything.
bar();
}
void f() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
if (!f) // expected-note{{'f' is non-null}}
// expected-note@-1{{Taking false branch}}
return;
expectedClose(f);
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace stream_passed_into_fn_that_doesnt_intend_to_free2
namespace streamstate_from_closed_to_open {
// StreamState of the symbol changed from nothing to Allocated. We don't want to
// emit notes when the RefKind changes in the stack frame.
static FILE *fopenWrapper() {
FILE *f = fopen("input.txt", "w"); // expected-note{{Stream opened here}}
assert(f);
return f;
}
void use_ret() {
FILE *v;
v = fopenWrapper(); // expected-note {{Calling 'fopenWrapper'}}
// expected-note@-1{{Returning from 'fopenWrapper'}}
} // expected-warning{{Opened stream never closed. Potential resource leak [unix.Stream]}}
// expected-note@-1{{Opened stream never closed. Potential resource leak}}
} // namespace streamstate_from_closed_to_open