From ae82759d004fa62c60b7ec36041f7f242e6bf22d Mon Sep 17 00:00:00 2001
From: Martin Anderseck <martin.anderseck@ni.com>
Date: Wed, 4 Aug 2021 09:45:39 +0200
Subject:  test: Fix potential resource leak

Fixed issue where variable tmp_file going out of scope could have
leaked the storage it points to. Replaced the declaration of tmp_file
by using a unique pointer with custom deleter that takes care of
closing the tmpfile when it is not needed anymore.
---
 host/tests/isatty_test.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

(limited to 'host')

diff --git a/host/tests/isatty_test.cpp b/host/tests/isatty_test.cpp
index 786023dc3..990366896 100644
--- a/host/tests/isatty_test.cpp
+++ b/host/tests/isatty_test.cpp
@@ -5,7 +5,7 @@
 //
 
 #include <uhdlib/utils/isatty.hpp>
-#include <stdio.h>
+#include <cstdio>
 #include <boost/test/unit_test.hpp>
 #include <iostream>
 
@@ -18,12 +18,11 @@ BOOST_AUTO_TEST_CASE(test_isatty)
     } else {
         std::cout << "stderr is not a TTY" << std::endl;
     }
-
-    FILE* tmp_file = std::tmpfile();
+    auto tmp_file = std::unique_ptr<std::FILE, decltype(&std::fclose)>(std::tmpfile(), &std::fclose);
 #ifdef UHD_PLATFORM_WIN32
-    BOOST_REQUIRE(!uhd::is_a_tty(_fileno(tmp_file)));
+    BOOST_REQUIRE(!uhd::is_a_tty(_fileno(tmp_file.get())));
 #elif _POSIX_C_SOURCE >= _200112L
-    BOOST_REQUIRE(!uhd::is_a_tty(fileno(tmp_file)));
+    BOOST_REQUIRE(!uhd::is_a_tty(fileno(tmp_file.get())));
 #else
     // I got 99 problems but dealing with portability ain't one
     BOOST_REQUIRE(!uhd::is_a_tty(99));
-- 
cgit v1.2.3