Skip to content

Commit

Permalink
Fix a number of issues from the latest coverity scan.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongreig committed Nov 20, 2024
1 parent e3247c2 commit 3fb40a9
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
6 changes: 5 additions & 1 deletion source/adapters/cuda/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ thread_local char ErrorMessage[MaxMessageSize];
[[maybe_unused]] void setErrorMessage(const char *pMessage,
ur_result_t ErrorCode) {
assert(strlen(pMessage) <= MaxMessageSize);
strcpy(ErrorMessage, pMessage);
#if defined(_WIN32)
strncpy_s(ErrorMessage, MaxMessageSize, pMessage, strlen(pMessage));
#else
strncpy(ErrorMessage, pMessage, MaxMessageSize);
#endif
ErrorMessageCode = ErrorCode;
}

Expand Down
6 changes: 5 additions & 1 deletion source/adapters/native_cpu/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ thread_local char ErrorMessage[MaxMessageSize];
[[maybe_unused]] void setErrorMessage(const char *pMessage,
ur_result_t ErrorCode) {
assert(strlen(pMessage) <= MaxMessageSize);
strcpy(ErrorMessage, pMessage);
#if defined(_WIN32)
strncpy_s(ErrorMessage, MaxMessageSize, pMessage, strlen(pMessage));
#else
strncpy(ErrorMessage, pMessage, MaxMessageSize);
#endif
ErrorMessageCode = ErrorCode;
}

Expand Down
6 changes: 6 additions & 0 deletions source/adapters/opencl/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ thread_local char ErrorMessage[MaxMessageSize];
[[maybe_unused]] void setErrorMessage(const char *Message, int32_t ErrorCode) {
assert(strlen(Message) <= cl_adapter::MaxMessageSize);
strcpy(cl_adapter::ErrorMessage, Message);
#if defined(_WIN32)
strncpy_s(cl_adapter::ErrorMessage, MaxMessageSize, Message, strlen(Message));
#else
strncpy(cl_adapter::ErrorMessage, Message, MaxMessageSize);
#endif

ErrorMessageCode = ErrorCode;
}
} // namespace cl_adapter
Expand Down
7 changes: 3 additions & 4 deletions source/loader/layers/sanitizer/asan_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ void ReportBadFree(uptr Addr, const StackTrace &stack,
if (!AI) {
getContext()->logger.always("{} may be allocated on Host Memory",
(void *)Addr);
} else {
assert(!AI->IsReleased && "Chunk must be not released");
PrintAllocateInfo(Addr, AI.get());
}

assert(AI && !AI->IsReleased && "Chunk must be not released");

PrintAllocateInfo(Addr, AI.get());
}

void ReportBadContext(uptr Addr, const StackTrace &stack,
Expand Down
2 changes: 1 addition & 1 deletion source/loader/layers/sanitizer/stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool Contains(const std::string &s, const char *p) {

// Parse back trace information in the following formats:
// <module_name>([function_name]+function_offset) [offset]
void ParseBacktraceInfo(BacktraceInfo BI, std::string &ModuleName,
void ParseBacktraceInfo(const BacktraceInfo &BI, std::string &ModuleName,
uptr &Offset) {
// Parse module name
size_t End = BI.find_first_of('(');
Expand Down
4 changes: 2 additions & 2 deletions test/conformance/source/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void uur::PlatformEnvironment::selectPlatformFromOptions() {
std::stringstream errstr;
errstr << "Multiple possible platforms found; please select one of the "
"following or set --platforms_count=1:\n";
for (auto p : platforms_filtered) {
for (const auto &p : platforms_filtered) {
errstr << " --backend=" << backend_to_str(p.backend)
<< " --platform=\"" << p.name << "\"\n";
}
Expand Down Expand Up @@ -287,7 +287,7 @@ PlatformEnvironment::parsePlatformOptions(int argc, char **argv) {
} else if (std::strncmp(arg, "--backend=", sizeof("--backend=") - 1) ==
0) {
std::string backend_string{&arg[std::strlen("--backend=")]};
if (!parse_backend(backend_string)) {
if (!parse_backend(std::move(backend_string))) {
return options;
}
} else if (std::strncmp(arg, "--platforms_count=",
Expand Down
18 changes: 9 additions & 9 deletions test/conformance/usm/urUSMFree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ TEST_P(urUSMFreeTest, SuccessDeviceAlloc) {
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;

uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
EXPECT_SUCCESS(urQueueFlush(queue));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_NE(ptr, nullptr);
ASSERT_SUCCESS(urUSMFree(context, ptr));
ASSERT_SUCCESS(urEventRelease(event));
}
Expand All @@ -43,15 +43,15 @@ TEST_P(urUSMFreeTest, SuccessHostAlloc) {
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(
urUSMHostAlloc(context, nullptr, nullptr, allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
EXPECT_SUCCESS(urQueueFlush(queue));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_NE(ptr, nullptr);
ASSERT_SUCCESS(urUSMFree(context, ptr));
ASSERT_SUCCESS(urEventRelease(event));
}
Expand All @@ -73,15 +73,15 @@ TEST_P(urUSMFreeTest, SuccessSharedAlloc) {
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, nullptr,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
EXPECT_SUCCESS(urQueueFlush(queue));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_NE(ptr, nullptr);
ASSERT_SUCCESS(urUSMFree(context, ptr));
ASSERT_SUCCESS(urEventRelease(event));
}
Expand Down
25 changes: 9 additions & 16 deletions test/conformance/usm/urUSMSharedAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct urUSMSharedAllocTest

ur_usm_pool_handle_t pool = nullptr;
bool usePool = std::get<0>(getParam()).value;
void *ptr = nullptr;
};

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

TEST_P(urUSMSharedAllocTest, Success) {
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_SUCCESS(urUSMFree(context, ptr));
EXPECT_SUCCESS(urEventRelease(event));
Expand All @@ -85,17 +85,16 @@ TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) {
ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc,
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
/* alignment */ 0};
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_SUCCESS(urUSMFree(context, ptr));
EXPECT_SUCCESS(urEventRelease(event));
Expand All @@ -107,31 +106,28 @@ TEST_P(urUSMSharedAllocTest, SuccessWithMultipleAdvices) {
/* mem advice flags */ UR_USM_ADVICE_FLAG_SET_READ_MOSTLY |
UR_USM_ADVICE_FLAG_BIAS_CACHED,
/* alignment */ 0};
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_SUCCESS(urUSMFree(context, ptr));
EXPECT_SUCCESS(urEventRelease(event));
}

TEST_P(urUSMSharedAllocTest, InvalidNullHandleContext) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMSharedAlloc(nullptr, device, nullptr, pool, sizeof(int), &ptr));
}

TEST_P(urUSMSharedAllocTest, InvalidNullHandleDevice) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMSharedAlloc(context, nullptr, nullptr, pool, sizeof(int), &ptr));
Expand All @@ -144,14 +140,12 @@ TEST_P(urUSMSharedAllocTest, InvalidNullPtrMem) {
}

TEST_P(urUSMSharedAllocTest, InvalidUSMSize) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMSharedAlloc(context, device, nullptr, pool, -1, &ptr));
}

TEST_P(urUSMSharedAllocTest, InvalidValueAlignPowerOfTwo) {
void *ptr = nullptr;
ur_usm_desc_t desc = {};
desc.stype = UR_STRUCTURE_TYPE_USM_DESC;
desc.align = 5;
Expand Down Expand Up @@ -185,16 +179,15 @@ TEST_P(urUSMSharedAllocAlignmentTest, SuccessAlignedAllocations) {
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
alignment};

void *ptr = nullptr;
ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern,
allocation_size, 0, nullptr, &event));
ASSERT_SUCCESS(urEventWait(1, &event));
EXPECT_SUCCESS(urEventWait(1, &event));

ASSERT_SUCCESS(urUSMFree(context, ptr));
EXPECT_SUCCESS(urEventRelease(event));
Expand Down

0 comments on commit 3fb40a9

Please sign in to comment.