Skip to content

Commit ea841a4

Browse files
Addressed klocwork_dpctl_issues (#465)
1 parent 83e497d commit ea841a4

6 files changed

+46
-9
lines changed

dpctl-capi/source/dpctl_sycl_context_interface.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ DPCTLContext_GetDevices(__dpctl_keep const DPCTLSyclContextRef CRef)
141141
}
142142
return wrap(DevicesVectorPtr);
143143
} catch (std::bad_alloc const &ba) {
144+
delete DevicesVectorPtr;
144145
// \todo log error
145146
std::cerr << ba.what() << '\n';
146147
return nullptr;
147148
} catch (const runtime_error &re) {
149+
delete DevicesVectorPtr;
148150
// \todo log error
149151
std::cerr << re.what() << '\n';
150152
return nullptr;

dpctl-capi/source/dpctl_sycl_device_interface.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,18 @@ DPCTLDevice_CreateSubDevicesEqually(__dpctl_keep const DPCTLSyclDeviceRef DRef,
587587
Devices->emplace_back(wrap(new device(sd)));
588588
}
589589
} catch (std::bad_alloc const &ba) {
590+
delete Devices;
590591
std::cerr << ba.what() << '\n';
591592
return nullptr;
592593
} catch (feature_not_supported const &fnse) {
594+
delete Devices;
593595
std::cerr << fnse.what() << '\n';
596+
return nullptr;
594597
} catch (runtime_error const &re) {
598+
delete Devices;
595599
// \todo log error
596600
std::cerr << re.what() << '\n';
601+
return nullptr;
597602
}
598603
}
599604
return wrap(Devices);
@@ -617,13 +622,18 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef,
617622
Devices->emplace_back(wrap(new device(sd)));
618623
}
619624
} catch (std::bad_alloc const &ba) {
625+
delete Devices;
620626
std::cerr << ba.what() << '\n';
621627
return nullptr;
622628
} catch (feature_not_supported const &fnse) {
629+
delete Devices;
623630
std::cerr << fnse.what() << '\n';
631+
return nullptr;
624632
} catch (runtime_error const &re) {
633+
delete Devices;
625634
// \todo log error
626635
std::cerr << re.what() << '\n';
636+
return nullptr;
627637
}
628638
}
629639
return wrap(Devices);
@@ -646,13 +656,18 @@ __dpctl_give DPCTLDeviceVectorRef DPCTLDevice_CreateSubDevicesByAffinity(
646656
Devices->emplace_back(wrap(new device(sd)));
647657
}
648658
} catch (std::bad_alloc const &ba) {
659+
delete Devices;
649660
std::cerr << ba.what() << '\n';
650661
return nullptr;
651662
} catch (feature_not_supported const &fnse) {
663+
delete Devices;
652664
std::cerr << fnse.what() << '\n';
665+
return nullptr;
653666
} catch (runtime_error const &re) {
667+
delete Devices;
654668
// \todo log error
655669
std::cerr << re.what() << '\n';
670+
return nullptr;
656671
}
657672
}
658673
return wrap(Devices);

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef)
123123
auto &cache = DeviceCacheBuilder::getDeviceCache();
124124
auto entry = cache.find(*Device);
125125
if (entry != cache.end()) {
126+
context *ContextPtr = nullptr;
126127
try {
127-
CRef = wrap(new context(entry->second));
128+
ContextPtr = new context(entry->second);
129+
CRef = wrap(ContextPtr);
128130
} catch (std::bad_alloc const &ba) {
129131
std::cerr << ba.what() << std::endl;
132+
delete ContextPtr;
130133
CRef = nullptr;
131134
}
132135
}
@@ -144,6 +147,7 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
144147
try {
145148
Devices = new vector_class<DPCTLSyclDeviceRef>();
146149
} catch (std::bad_alloc const &ba) {
150+
delete Devices;
147151
return nullptr;
148152
}
149153
const auto &root_devices = device::get_devices();

dpctl-capi/source/dpctl_sycl_platform_interface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ __dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector(
7979
DPCTLSyclPlatformRef PRef = nullptr;
8080
auto DS = unwrap(DSRef);
8181
if (DS) {
82+
platform *P = nullptr;
8283
try {
83-
auto P = new platform(*DS);
84+
P = new platform(*DS);
8485
PRef = wrap(P);
8586
} catch (std::bad_alloc const &ba) {
87+
delete P;
8688
std::cerr << ba.what() << '\n';
8789
} catch (runtime_error const &re) {
90+
delete P;
8891
std::cerr << re.what() << '\n';
89-
return nullptr;
9092
}
9193
}
9294
else {

dpctl-capi/source/dpctl_sycl_queue_interface.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,13 @@ DPCTLQueue_CreateForDevice(__dpctl_keep const DPCTLSyclDeviceRef DRef,
234234
// will be the case for non-root devices, i.e., sub-devices, a new context
235235
// will be allocated. Note that any newly allocated context is not cached.
236236
if (!CRef) {
237+
context *ContextPtr = nullptr;
237238
try {
238-
CRef = wrap(new context(*Device));
239+
ContextPtr = new context(*Device);
240+
CRef = wrap(ContextPtr);
239241
} catch (std::bad_alloc const &ba) {
240242
std::cerr << ba.what() << std::endl;
243+
delete ContextPtr;
241244
return QRef;
242245
}
243246
}

dpctl-capi/source/dpctl_vector_templ.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class<SYCLREF(EL)>, VECTOR(EL))
3939
*/
4040
__dpctl_give VECTOR(EL) FN(EL, Create)()
4141
{
42+
vector_class<SYCLREF(EL)> *Vec = nullptr;
4243
try {
43-
auto Vec = new vector_class<SYCLREF(EL)>();
44+
Vec = new vector_class<SYCLREF(EL)>();
4445
return wrap(Vec);
4546
} catch (std::bad_alloc const &ba) {
47+
delete Vec;
4648
return nullptr;
4749
}
4850
}
@@ -56,15 +58,17 @@ __dpctl_give VECTOR(EL) FN(EL, Create)()
5658
__dpctl_give VECTOR(EL)
5759
FN(EL, CreateFromArray)(size_t n, __dpctl_keep SYCLREF(EL) * elems)
5860
{
61+
vector_class<SYCLREF(EL)> *Vec = nullptr;
5962
try {
60-
auto Vec = new vector_class<SYCLREF(EL)>();
63+
Vec = new vector_class<SYCLREF(EL)>();
6164
for (size_t i = 0; i < n; ++i) {
6265
auto Ref = unwrap(elems[i]);
6366
Vec->emplace_back(
6467
wrap(new std::remove_pointer<decltype(Ref)>::type(*Ref)));
6568
}
6669
return wrap(Vec);
6770
} catch (std::bad_alloc const &ba) {
71+
delete Vec;
6872
return nullptr;
6973
}
7074
}
@@ -125,13 +129,20 @@ SYCLREF(EL) FN(EL, GetAt)(__dpctl_keep VECTOR(EL) VRef, size_t index)
125129
auto Vec = unwrap(VRef);
126130
SYCLREF(EL) copy = nullptr;
127131
if (Vec) {
132+
SYCLREF(EL) ret;
128133
try {
129-
auto ret = Vec->at(index);
130-
auto Ref = unwrap(ret);
131-
copy = wrap(new std::remove_pointer<decltype(Ref)>::type(*Ref));
134+
ret = Vec->at(index);
132135
} catch (std::out_of_range const &oor) {
133136
std::cerr << oor.what() << '\n';
137+
return nullptr;
138+
}
139+
auto Ref = unwrap(ret);
140+
std::remove_pointer<decltype(Ref)>::type *elPtr = nullptr;
141+
try {
142+
elPtr = new std::remove_pointer<decltype(Ref)>::type(*Ref);
143+
copy = wrap(elPtr);
134144
} catch (std::bad_alloc const &ba) {
145+
delete elPtr;
135146
// \todo log error
136147
std::cerr << ba.what() << '\n';
137148
return nullptr;

0 commit comments

Comments
 (0)