Skip to content

Commit e6c6a4d

Browse files
committed
[fix][tests] Coverity: UNCAUGHT_EXCEPT checker, "Uncaught exception" type (#1231)
* An `qpl::tests::exception_handler()` function has been added to `tools/utils/common`. This function is similar to the exception handler previously added to `example_utils`. It is able to catch exceptions of the type `std::exception` and uncaught exceptions. At first glance, there are no other exception types that can be raised in QPL tests and benchmarks. * `qpl::tests::exception_handler()` function has been used in `main()` functions of all QPL tests and benchmarks: * `functional` tests * `cross_tests` * `benchmarks`
1 parent d1bb5cb commit e6c6a4d

File tree

4 files changed

+99
-60
lines changed

4 files changed

+99
-60
lines changed

tools/benchmarks/src/main.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "test_hw_dispatcher.hpp"
1717

1818
// tool_common
19+
#include "exception_handler.hpp"
1920
#include "system_info.hpp"
2021

2122
#if defined(__linux__)
@@ -277,35 +278,36 @@ std::string format(const char* format, ...) noexcept {
277278
//
278279
// Main
279280
//
280-
int main(int argc, char** argv) //NOLINT(bugprone-exception-escape)
281-
{
282-
// Parse command line arguments
283-
bench::cmd::parse_cmd_line(&argc, argv);
281+
int main(int argc, char** argv) {
282+
try {
283+
// Parse command line arguments
284+
bench::cmd::parse_cmd_line(&argc, argv);
284285

285-
// Initialize the benchmark framework
286-
::benchmark::Initialize(&argc, argv);
287-
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
286+
// Initialize the benchmark framework
287+
::benchmark::Initialize(&argc, argv);
288+
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
288289

289-
// Retrieve system information
290-
auto& sys_info = qpl::test::get_sys_info();
291-
std::cout << sys_info;
290+
// Retrieve system information
291+
auto& sys_info = qpl::test::get_sys_info();
292+
std::cout << sys_info;
292293

293-
// Initialize accelerator hardware if enabled
294-
if (!bench::cmd::FLAGS_no_hw) bench::details::init_hw();
294+
// Initialize accelerator hardware if enabled
295+
if (!bench::cmd::FLAGS_no_hw) bench::details::init_hw();
295296

296-
// Parse the benchmark filter
297-
bench::parse_benchmark_filter(benchmark::GetBenchmarkFilter());
297+
// Parse the benchmark filter
298+
bench::parse_benchmark_filter(benchmark::GetBenchmarkFilter());
298299

299-
// Register benchmarks
300-
auto& registry = bench::details::get_registry();
301-
for (auto& reg : registry)
302-
reg();
300+
// Register benchmarks
301+
auto& registry = bench::details::get_registry();
302+
for (auto& reg : registry)
303+
reg();
303304

304-
// Run benchmarks
305-
::benchmark::RunSpecifiedBenchmarks();
305+
// Run benchmarks
306+
::benchmark::RunSpecifiedBenchmarks();
306307

307-
// Shutdown the benchmark framework
308-
::benchmark::Shutdown();
308+
// Shutdown the benchmark framework
309+
::benchmark::Shutdown();
309310

310-
return 0;
311+
return 0;
312+
} catch (...) { return qpl::test::exception_handler(); }
311313
}

tools/tests/cross_tests/main.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "gtest/gtest.h"
1010
#include "qpl_test_environment.hpp"
1111

12+
// tool_common
13+
#include "exception_handler.hpp"
14+
1215
namespace qpl::test {
1316

1417
static inline void show_help() {
@@ -58,20 +61,22 @@ static inline util::arguments_list_t get_testing_settings(int argc, char* argv[]
5861

5962
} // namespace qpl::test
6063

61-
int main(int argc, char* argv[]) { //NOLINT(bugprone-exception-escape)
62-
testing::InitGoogleTest(&argc, argv);
64+
int main(int argc, char* argv[]) {
65+
try {
66+
testing::InitGoogleTest(&argc, argv);
6367

64-
auto arguments_list = qpl::test::get_testing_settings(argc, argv);
68+
auto arguments_list = qpl::test::get_testing_settings(argc, argv);
6569

66-
using environment = qpl::test::util::TestEnvironment;
70+
using environment = qpl::test::util::TestEnvironment;
6771

68-
environment::GetInstance().Initialize(arguments_list);
72+
environment::GetInstance().Initialize(arguments_list);
6973

70-
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
74+
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
7175

72-
const int status = RUN_ALL_TESTS();
76+
const int status = RUN_ALL_TESTS();
7377

74-
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
78+
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
7579

76-
return status;
80+
return status;
81+
} catch (...) { return qpl::test::exception_handler(); }
7782
}

tools/tests/functional/main.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <algorithm>
2121

2222
// tool_common
23+
#include "exception_handler.hpp"
2324
#include "system_info.hpp"
2425

2526
namespace qpl::test {
@@ -174,46 +175,47 @@ int test_init_with_fork() {
174175
}
175176
#endif
176177

177-
int main(int argc, char* argv[]) { //NOLINT(bugprone-exception-escape)
178-
179-
std::vector<std::string> arguments(argv + 1, argv + argc);
180-
if (std::find(begin(arguments), end(arguments), QPL_ARG_HELP) != end(arguments)) {
181-
qpl::test::show_help();
182-
return 0;
183-
}
178+
int main(int argc, char* argv[]) {
179+
try {
180+
std::vector<std::string> arguments(argv + 1, argv + argc);
181+
if (std::find(begin(arguments), end(arguments), QPL_ARG_HELP) != end(arguments)) {
182+
qpl::test::show_help();
183+
return 0;
184+
}
184185

185-
testing::InitGoogleTest(&argc, argv);
186+
testing::InitGoogleTest(&argc, argv);
186187

187-
auto arguments_list = qpl::test::get_testing_settings(argc, argv);
188+
auto arguments_list = qpl::test::get_testing_settings(argc, argv);
188189

189-
using environment = qpl::test::util::TestEnvironment;
190+
using environment = qpl::test::util::TestEnvironment;
190191

191-
environment::GetInstance().Initialize(arguments_list);
192+
environment::GetInstance().Initialize(arguments_list);
192193

193-
const qpl::test::extended_info_t& info = qpl::test::get_sys_info();
194-
std::cout << info;
194+
const qpl::test::extended_info_t& info = qpl::test::get_sys_info();
195+
std::cout << info;
195196

196197
#if defined(__linux__)
197-
int init_with_fork_status = 0;
198-
auto execution_path = environment::GetInstance().GetExecutionPath();
199-
if (execution_path == qpl_path_hardware) {
200-
std::cout << "Running HW dispatcher initialization check with multiprocessing\n";
201-
init_with_fork_status = test_init_with_fork();
202-
EXPECT_TRUE(init_with_fork_status == 0) << "Hardware dispatcher initialization with fork() failed. ";
203-
std::cout << "Finished running HW dispatcher initialization check.\n";
204-
}
198+
int init_with_fork_status = 0;
199+
auto execution_path = environment::GetInstance().GetExecutionPath();
200+
if (execution_path == qpl_path_hardware) {
201+
std::cout << "Running HW dispatcher initialization check with multiprocessing\n";
202+
init_with_fork_status = test_init_with_fork();
203+
EXPECT_TRUE(init_with_fork_status == 0) << "Hardware dispatcher initialization with fork() failed. ";
204+
std::cout << "Finished running HW dispatcher initialization check.\n";
205+
}
205206
#endif
206207

207-
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
208+
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
208209

209-
int status = RUN_ALL_TESTS();
210+
int status = RUN_ALL_TESTS();
210211

211-
// Duplicate diagnostics at the end of the log
212+
// Duplicate diagnostics at the end of the log
212213
#if defined(__linux__)
213-
if (init_with_fork_status) std::cout << "Hardware dispatcher initialization with fork() failed.\n";
214-
status |= init_with_fork_status;
214+
if (init_with_fork_status) std::cout << "Hardware dispatcher initialization with fork() failed.\n";
215+
status |= init_with_fork_status;
215216
#endif
216-
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
217+
std::cout << "Tests seed = " << environment::GetInstance().GetSeed() << '\n';
217218

218-
return status;
219+
return status;
220+
} catch (...) { return qpl::test::exception_handler(); }
219221
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
******************************************************************************/
6+
7+
#ifndef QPL_TOOLS_UTILS_COMMON_EXCEPTION_HANDLER_HPP
8+
#define QPL_TOOLS_UTILS_COMMON_EXCEPTION_HANDLER_HPP
9+
10+
#include <exception>
11+
#include <iostream>
12+
13+
namespace qpl::test {
14+
15+
inline int exception_handler() {
16+
try {
17+
throw;
18+
} catch (const std::exception& std_exception) {
19+
std::cout << "std::exception was caught: " << std_exception.what() << "\n";
20+
return -2;
21+
} catch (...) {
22+
std::cout << "An unrecognized exception was caught!\n";
23+
return -1;
24+
}
25+
return 0;
26+
}
27+
28+
} // namespace qpl::test
29+
30+
#endif //QPL_TOOLS_UTILS_COMMON_EXCEPTION_HANDLER_HPP

0 commit comments

Comments
 (0)