Skip to content

Commit 255760e

Browse files
authored
[UR] Fix various defects from static analysis (#17299)
- Don't use iterators that may be at the end - Use references for param tuples - Avoid integer overflow
1 parent b0f9bed commit 255760e

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

unified-runtime/source/common/ur_singleton.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#ifndef UR_SINGLETON_H
1313
#define UR_SINGLETON_H 1
1414

15-
#include <cassert>
1615
#include <memory>
1716
#include <mutex>
1817
#include <unordered_map>
@@ -77,20 +76,22 @@ template <typename singleton_tn, typename key_tn> class singleton_factory_t {
7776
void retain(key_tn key) {
7877
std::lock_guard<std::mutex> lk(mut);
7978
auto iter = map.find(getKey(key));
80-
assert(iter != map.end());
81-
iter->second.ref_count++;
79+
if (iter != map.end()) {
80+
iter->second.ref_count++;
81+
}
8282
}
8383

8484
//////////////////////////////////////////////////////////////////////////
8585
/// once the key is no longer valid, release the singleton
8686
void release(key_tn key) {
8787
std::lock_guard<std::mutex> lk(mut);
8888
auto iter = map.find(getKey(key));
89-
assert(iter != map.end());
90-
if (iter->second.ref_count == 0) {
91-
map.erase(iter);
92-
} else {
93-
iter->second.ref_count--;
89+
if (iter != map.end()) {
90+
if (iter->second.ref_count == 0) {
91+
map.erase(iter);
92+
} else {
93+
iter->second.ref_count--;
94+
}
9495
}
9596
}
9697

unified-runtime/test/conformance/device/urDevicePartition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ TEST_P(urDevicePartitionTest, PartitionByCountsSuccess) {
6161

6262
uint32_t n_cu_in_device = 0;
6363
ASSERT_NO_FATAL_FAILURE(getNumberComputeUnits(device, n_cu_in_device));
64+
ASSERT_NE(n_cu_in_device, 0);
6465

6566
enum class Combination { ONE, HALF, ALL_MINUS_ONE, ALL };
6667

unified-runtime/test/conformance/testing/source/fixtures.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ std::string deviceTestWithParamPrinter<BoolTestParam>(
1212
const ::testing::TestParamInfo<std::tuple<DeviceTuple, BoolTestParam>>
1313
&info) {
1414
auto device = std::get<0>(info.param).device;
15-
auto param = std::get<1>(info.param);
15+
auto &param = std::get<1>(info.param);
1616

1717
std::stringstream ss;
1818
ss << param.name << (param.value ? "Enabled" : "Disabled");
@@ -24,7 +24,7 @@ std::string platformTestWithParamPrinter<BoolTestParam>(
2424
const ::testing::TestParamInfo<
2525
std::tuple<ur_platform_handle_t, BoolTestParam>> &info) {
2626
auto platform = std::get<0>(info.param);
27-
auto param = std::get<1>(info.param);
27+
auto &param = std::get<1>(info.param);
2828

2929
std::stringstream ss;
3030
ss << param.name << (param.value ? "Enabled" : "Disabled");
@@ -36,7 +36,7 @@ std::string deviceTestWithParamPrinter<SamplerCreateParamT>(
3636
const ::testing::TestParamInfo<
3737
std::tuple<DeviceTuple, uur::SamplerCreateParamT>> &info) {
3838
auto device = std::get<0>(info.param).device;
39-
auto param = std::get<1>(info.param);
39+
auto &param = std::get<1>(info.param);
4040

4141
const auto normalized = std::get<0>(param);
4242
const auto addr_mode = std::get<1>(param);
@@ -58,7 +58,7 @@ std::string deviceTestWithParamPrinter<ur_image_format_t>(
5858
const ::testing::TestParamInfo<std::tuple<DeviceTuple, ur_image_format_t>>
5959
&info) {
6060
auto device = std::get<0>(info.param).device;
61-
auto param = std::get<1>(info.param);
61+
auto &param = std::get<1>(info.param);
6262
auto ChannelOrder = param.channelOrder;
6363
auto ChannelType = param.channelType;
6464

0 commit comments

Comments
 (0)