From 9509d0d8b8b0db0d94b6879c13d0ef72e4f642ea Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 7 Oct 2025 17:21:38 +0000 Subject: [PATCH] intel/mda: Use GTEST fixtures to manage File handles Coverity points out that if the asserts fail, then the file won't be closed, and therefore wont be deleted. We'd like to avoid littering the temp directory with useless files. This uses GTEST's `TEST_F` feature with a custom class to manager the creation and destruction of the tmpfile. CID: 1666502 CID: 1666525 CID: 1666579 Reviewed-by: Caio Oliveira Part-of: --- src/intel/mda/tar_test.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/intel/mda/tar_test.cpp b/src/intel/mda/tar_test.cpp index 5e1560e4c2a..f3058a710b2 100644 --- a/src/intel/mda/tar_test.cpp +++ b/src/intel/mda/tar_test.cpp @@ -7,9 +7,17 @@ #include #include "tar.h" -TEST(Tar, RoundtripSmallFile) +class TarTest : public testing::Test { + protected: + TarTest() : f{std::tmpfile()} {}; + ~TarTest() { std::fclose(f); } + + FILE *f; +}; + + +TEST_F(TarTest, RoundtripSmallFile) { - FILE *f = tmpfile(); const char *test = "TEST TEST TEST"; { @@ -31,7 +39,6 @@ TEST(Tar, RoundtripSmallFile) fseek(f, 0, SEEK_SET); ASSERT_EQ(fread(contents, size, 1, f), 1); - fclose(f); { tar_reader ar; @@ -55,10 +62,8 @@ TEST(Tar, RoundtripSmallFile) delete[] contents; } -TEST(Tar, RoundtripContentsWithRecordSize) +TEST_F(TarTest, RoundtripContentsWithRecordSize) { - FILE *f = tmpfile(); - uint8_t test[512]; for (unsigned i = 0; i < sizeof(test); i++) { @@ -80,7 +85,6 @@ TEST(Tar, RoundtripContentsWithRecordSize) fseek(f, 0, SEEK_SET); ASSERT_EQ(fread(contents, size, 1, f), 1); - fclose(f); { tar_reader ar; @@ -105,10 +109,8 @@ TEST(Tar, RoundtripContentsWithRecordSize) delete[] contents; } -TEST(Tar, TimestampRoundtrip) +TEST_F(TarTest, TimestampRoundtrip) { - FILE *f = tmpfile(); - const char *test = "TEST TIMESTAMP"; const time_t test_timestamp = 1234567890; // Known timestamp (February 13, 2009) @@ -129,7 +131,6 @@ TEST(Tar, TimestampRoundtrip) fseek(f, 0, SEEK_SET); ASSERT_EQ(fread(contents, size, 1, f), 1); - fclose(f); { tar_reader ar;