Skip to content

Commit aa72577

Browse files
authored
Merge pull request #2352 from aarongreig/aaron/fixCoverity15-11
Fix a number of issues from the latest coverity scan.
2 parents 89def64 + 3d2b721 commit aa72577

File tree

10 files changed

+48
-45
lines changed

10 files changed

+48
-45
lines changed

source/adapters/cuda/common.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ void detail::ur::assertion(bool Condition, const char *Message) {
104104

105105
// Global variables for ZER_EXT_RESULT_ADAPTER_SPECIFIC_ERROR
106106
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
107-
thread_local char ErrorMessage[MaxMessageSize];
107+
thread_local char ErrorMessage[MaxMessageSize]{};
108108

109109
// Utility function for setting a message and warning
110110
[[maybe_unused]] void setErrorMessage(const char *pMessage,
111111
ur_result_t ErrorCode) {
112-
assert(strlen(pMessage) <= MaxMessageSize);
113-
strcpy(ErrorMessage, pMessage);
112+
assert(strlen(pMessage) < MaxMessageSize);
113+
// Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is
114+
// always null terminated.
115+
strncpy(ErrorMessage, pMessage, MaxMessageSize - 1);
114116
ErrorMessageCode = ErrorCode;
115117
}
116118

source/adapters/hip/common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ void detail::ur::assertion(bool Condition, const char *pMessage) {
164164

165165
// Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR
166166
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
167-
thread_local char ErrorMessage[MaxMessageSize];
167+
thread_local char ErrorMessage[MaxMessageSize]{};
168168

169169
// Utility function for setting a message and warning
170170
[[maybe_unused]] void setErrorMessage(const char *pMessage,
171171
ur_result_t ErrorCode) {
172172
assert(strlen(pMessage) < MaxMessageSize);
173+
// Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is
174+
// always null terminated.
173175
strncpy(ErrorMessage, pMessage, MaxMessageSize - 1);
174176
ErrorMessageCode = ErrorCode;
175177
}

source/adapters/level_zero/common.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,17 @@ getZeStructureType<ze_intel_device_block_array_exp_properties_t>() {
341341

342342
// Global variables for ZER_EXT_RESULT_ADAPTER_SPECIFIC_ERROR
343343
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
344-
thread_local char ErrorMessage[MaxMessageSize];
344+
thread_local char ErrorMessage[MaxMessageSize]{};
345345
thread_local int32_t ErrorAdapterNativeCode;
346346

347347
// Utility function for setting a message and warning
348348
[[maybe_unused]] void setErrorMessage(const char *pMessage,
349349
ur_result_t ErrorCode,
350350
int32_t AdapterErrorCode) {
351-
assert(strlen(pMessage) <= MaxMessageSize);
352-
strcpy(ErrorMessage, pMessage);
351+
assert(strlen(pMessage) < MaxMessageSize);
352+
// Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is
353+
// always null terminated.
354+
strncpy(ErrorMessage, pMessage, MaxMessageSize - 1);
353355
ErrorMessageCode = ErrorCode;
354356
ErrorAdapterNativeCode = AdapterErrorCode;
355357
}

source/adapters/native_cpu/common.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
// Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR
1414
// See urGetLastResult
1515
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
16-
thread_local char ErrorMessage[MaxMessageSize];
16+
thread_local char ErrorMessage[MaxMessageSize]{};
1717

1818
// Utility function for setting a message and warning
1919
[[maybe_unused]] void setErrorMessage(const char *pMessage,
2020
ur_result_t ErrorCode) {
21-
assert(strlen(pMessage) <= MaxMessageSize);
22-
strcpy(ErrorMessage, pMessage);
21+
assert(strlen(pMessage) < MaxMessageSize);
22+
// Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is
23+
// always null terminated.
24+
strncpy(ErrorMessage, pMessage, MaxMessageSize - 1);
2325
ErrorMessageCode = ErrorCode;
2426
}
2527

source/adapters/opencl/common.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ namespace cl_adapter {
1414

1515
/* Global variables for urAdapterGetLastError() */
1616
thread_local int32_t ErrorMessageCode = 0;
17-
thread_local char ErrorMessage[MaxMessageSize];
17+
thread_local char ErrorMessage[MaxMessageSize]{};
1818

1919
[[maybe_unused]] void setErrorMessage(const char *Message, int32_t ErrorCode) {
20-
assert(strlen(Message) <= cl_adapter::MaxMessageSize);
21-
strcpy(cl_adapter::ErrorMessage, Message);
20+
assert(strlen(Message) < cl_adapter::MaxMessageSize);
21+
// Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is
22+
// always null terminated.
23+
strncpy(cl_adapter::ErrorMessage, Message, MaxMessageSize - 1);
24+
2225
ErrorMessageCode = ErrorCode;
2326
}
2427
} // namespace cl_adapter

source/loader/layers/sanitizer/asan/asan_report.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ void ReportBadFree(uptr Addr, const StackTrace &stack,
4747
if (!AI) {
4848
getContext()->logger.always("{} may be allocated on Host Memory",
4949
(void *)Addr);
50+
} else {
51+
assert(!AI->IsReleased && "Chunk must be not released");
52+
PrintAllocateInfo(Addr, AI.get());
5053
}
51-
52-
assert(AI && !AI->IsReleased && "Chunk must be not released");
53-
54-
PrintAllocateInfo(Addr, AI.get());
5554
}
5655

5756
void ReportBadContext(uptr Addr, const StackTrace &stack,

source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bool Contains(const std::string &s, const char *p) {
3131

3232
// Parse back trace information in the following formats:
3333
// <module_name>([function_name]+function_offset) [offset]
34-
void ParseBacktraceInfo(BacktraceInfo BI, std::string &ModuleName,
34+
void ParseBacktraceInfo(const BacktraceInfo &BI, std::string &ModuleName,
3535
uptr &Offset) {
3636
// Parse module name
3737
size_t End = BI.find_first_of('(');

test/conformance/source/environment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void uur::PlatformEnvironment::selectPlatformFromOptions() {
222222
std::stringstream errstr;
223223
errstr << "Multiple possible platforms found; please select one of the "
224224
"following or set --platforms_count=1:\n";
225-
for (auto p : platforms_filtered) {
225+
for (const auto &p : platforms_filtered) {
226226
errstr << " --backend=" << backend_to_str(p.backend)
227227
<< " --platform=\"" << p.name << "\"\n";
228228
}
@@ -287,7 +287,7 @@ PlatformEnvironment::parsePlatformOptions(int argc, char **argv) {
287287
} else if (std::strncmp(arg, "--backend=", sizeof("--backend=") - 1) ==
288288
0) {
289289
std::string backend_string{&arg[std::strlen("--backend=")]};
290-
if (!parse_backend(backend_string)) {
290+
if (!parse_backend(std::move(backend_string))) {
291291
return options;
292292
}
293293
} else if (std::strncmp(arg, "--platforms_count=",

test/conformance/usm/urUSMFree.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ TEST_P(urUSMFreeTest, SuccessDeviceAlloc) {
1919
size_t allocation_size = sizeof(int);
2020
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr,
2121
allocation_size, &ptr));
22+
ASSERT_NE(ptr, nullptr);
2223

2324
ur_event_handle_t event = nullptr;
2425

2526
uint8_t pattern = 0;
26-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
27+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
2728
allocation_size, 0, nullptr, &event));
2829
EXPECT_SUCCESS(urQueueFlush(queue));
29-
ASSERT_SUCCESS(urEventWait(1, &event));
30+
EXPECT_SUCCESS(urEventWait(1, &event));
3031

31-
ASSERT_NE(ptr, nullptr);
3232
ASSERT_SUCCESS(urUSMFree(context, ptr));
3333
ASSERT_SUCCESS(urEventRelease(event));
3434
}
@@ -43,15 +43,15 @@ TEST_P(urUSMFreeTest, SuccessHostAlloc) {
4343
size_t allocation_size = sizeof(int);
4444
ASSERT_SUCCESS(
4545
urUSMHostAlloc(context, nullptr, nullptr, allocation_size, &ptr));
46+
ASSERT_NE(ptr, nullptr);
4647

4748
ur_event_handle_t event = nullptr;
4849
uint8_t pattern = 0;
49-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
50+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
5051
allocation_size, 0, nullptr, &event));
5152
EXPECT_SUCCESS(urQueueFlush(queue));
52-
ASSERT_SUCCESS(urEventWait(1, &event));
53+
EXPECT_SUCCESS(urEventWait(1, &event));
5354

54-
ASSERT_NE(ptr, nullptr);
5555
ASSERT_SUCCESS(urUSMFree(context, ptr));
5656
ASSERT_SUCCESS(urEventRelease(event));
5757
}
@@ -73,15 +73,15 @@ TEST_P(urUSMFreeTest, SuccessSharedAlloc) {
7373
size_t allocation_size = sizeof(int);
7474
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, nullptr,
7575
allocation_size, &ptr));
76+
ASSERT_NE(ptr, nullptr);
7677

7778
ur_event_handle_t event = nullptr;
7879
uint8_t pattern = 0;
79-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
80+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
8081
allocation_size, 0, nullptr, &event));
8182
EXPECT_SUCCESS(urQueueFlush(queue));
82-
ASSERT_SUCCESS(urEventWait(1, &event));
83+
EXPECT_SUCCESS(urEventWait(1, &event));
8384

84-
ASSERT_NE(ptr, nullptr);
8585
ASSERT_SUCCESS(urUSMFree(context, ptr));
8686
ASSERT_SUCCESS(urEventRelease(event));
8787
}

test/conformance/usm/urUSMSharedAlloc.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct urUSMSharedAllocTest
4444

4545
ur_usm_pool_handle_t pool = nullptr;
4646
bool usePool = std::get<0>(getParam()).value;
47+
void *ptr = nullptr;
4748
};
4849

4950
// The 0 value parameters are not relevant for urUSMSharedAllocTest tests, they
@@ -57,17 +58,16 @@ UUR_TEST_SUITE_P(
5758
uur::printUSMAllocTestString<urUSMSharedAllocTest>);
5859

5960
TEST_P(urUSMSharedAllocTest, Success) {
60-
void *ptr = nullptr;
6161
size_t allocation_size = sizeof(int);
6262
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, pool,
6363
allocation_size, &ptr));
6464
ASSERT_NE(ptr, nullptr);
6565

6666
ur_event_handle_t event = nullptr;
6767
uint8_t pattern = 0;
68-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
68+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
6969
allocation_size, 0, nullptr, &event));
70-
ASSERT_SUCCESS(urEventWait(1, &event));
70+
EXPECT_SUCCESS(urEventWait(1, &event));
7171

7272
ASSERT_SUCCESS(urUSMFree(context, ptr));
7373
EXPECT_SUCCESS(urEventRelease(event));
@@ -85,17 +85,16 @@ TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) {
8585
ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc,
8686
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
8787
/* alignment */ 0};
88-
void *ptr = nullptr;
8988
size_t allocation_size = sizeof(int);
9089
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
9190
allocation_size, &ptr));
9291
ASSERT_NE(ptr, nullptr);
9392

9493
ur_event_handle_t event = nullptr;
9594
uint8_t pattern = 0;
96-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
95+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
9796
allocation_size, 0, nullptr, &event));
98-
ASSERT_SUCCESS(urEventWait(1, &event));
97+
EXPECT_SUCCESS(urEventWait(1, &event));
9998

10099
ASSERT_SUCCESS(urUSMFree(context, ptr));
101100
EXPECT_SUCCESS(urEventRelease(event));
@@ -107,31 +106,28 @@ TEST_P(urUSMSharedAllocTest, SuccessWithMultipleAdvices) {
107106
/* mem advice flags */ UR_USM_ADVICE_FLAG_SET_READ_MOSTLY |
108107
UR_USM_ADVICE_FLAG_BIAS_CACHED,
109108
/* alignment */ 0};
110-
void *ptr = nullptr;
111109
size_t allocation_size = sizeof(int);
112110
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
113111
allocation_size, &ptr));
114112
ASSERT_NE(ptr, nullptr);
115113

116114
ur_event_handle_t event = nullptr;
117115
uint8_t pattern = 0;
118-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
116+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
119117
allocation_size, 0, nullptr, &event));
120-
ASSERT_SUCCESS(urEventWait(1, &event));
118+
EXPECT_SUCCESS(urEventWait(1, &event));
121119

122120
ASSERT_SUCCESS(urUSMFree(context, ptr));
123121
EXPECT_SUCCESS(urEventRelease(event));
124122
}
125123

126124
TEST_P(urUSMSharedAllocTest, InvalidNullHandleContext) {
127-
void *ptr = nullptr;
128125
ASSERT_EQ_RESULT(
129126
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
130127
urUSMSharedAlloc(nullptr, device, nullptr, pool, sizeof(int), &ptr));
131128
}
132129

133130
TEST_P(urUSMSharedAllocTest, InvalidNullHandleDevice) {
134-
void *ptr = nullptr;
135131
ASSERT_EQ_RESULT(
136132
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
137133
urUSMSharedAlloc(context, nullptr, nullptr, pool, sizeof(int), &ptr));
@@ -144,14 +140,12 @@ TEST_P(urUSMSharedAllocTest, InvalidNullPtrMem) {
144140
}
145141

146142
TEST_P(urUSMSharedAllocTest, InvalidUSMSize) {
147-
void *ptr = nullptr;
148143
ASSERT_EQ_RESULT(
149144
UR_RESULT_ERROR_INVALID_USM_SIZE,
150145
urUSMSharedAlloc(context, device, nullptr, pool, -1, &ptr));
151146
}
152147

153148
TEST_P(urUSMSharedAllocTest, InvalidValueAlignPowerOfTwo) {
154-
void *ptr = nullptr;
155149
ur_usm_desc_t desc = {};
156150
desc.stype = UR_STRUCTURE_TYPE_USM_DESC;
157151
desc.align = 5;
@@ -185,16 +179,15 @@ TEST_P(urUSMSharedAllocAlignmentTest, SuccessAlignedAllocations) {
185179
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
186180
alignment};
187181

188-
void *ptr = nullptr;
189182
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
190183
allocation_size, &ptr));
191184
ASSERT_NE(ptr, nullptr);
192185

193186
ur_event_handle_t event = nullptr;
194187
uint8_t pattern = 0;
195-
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
188+
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
196189
allocation_size, 0, nullptr, &event));
197-
ASSERT_SUCCESS(urEventWait(1, &event));
190+
EXPECT_SUCCESS(urEventWait(1, &event));
198191

199192
ASSERT_SUCCESS(urUSMFree(context, ptr));
200193
EXPECT_SUCCESS(urEventRelease(event));

0 commit comments

Comments
 (0)