Skip to content

Commit

Permalink
Fix Coverity wrapper escape issue
Browse files Browse the repository at this point in the history
Issue was introduced in #1220 where a new allocation is created in a
`std::unique_ptr` to enable buffer mapping. The `.get()` member function
is called before the `std::unique_ptr` is passed into the `BufferMem`
constructor. However, move semantics were not being explicitly followed.
The `BufferMem` constructor was taking the `std::unique_ptr` by l-value
refernece, then calling `std::move()` on it. This triggered a
wrapper-escape, use-after-free defect in Coverity. This fix is to
explicitly `std::move()` the `std::unique_ptr` into the `BufferMem`
constructor, and also update the `BufferMem` constructor to take an
r-value reference instead of an l-value reference.
  • Loading branch information
kbenzie committed Apr 11, 2024
1 parent 38e9478 commit 6027c6b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/adapters/cuda/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct BufferMem {
MapMem(nullptr) {}

BufferMap(size_t MapSize, size_t MapOffset, ur_map_flags_t MapFlags,
std::unique_ptr<unsigned char[]> &MapMem)
std::unique_ptr<unsigned char[]> &&MapMem)
: MapSize(MapSize), MapOffset(MapOffset), MapFlags(MapFlags),
MapMem(std::move(MapMem)) {}

Expand Down Expand Up @@ -105,7 +105,7 @@ struct BufferMem {
auto MapMem = std::make_unique<unsigned char[]>(MapSize);
MapPtr = MapMem.get();
PtrToBufferMap.insert(
{MapPtr, BufferMap(MapSize, MapOffset, MapFlags, MapMem)});
{MapPtr, BufferMap(MapSize, MapOffset, MapFlags, std::move(MapMem))});
} else {
/// However, if HostPtr already has valid memory (e.g. pinned allocation),
/// we can just use that memory for the mapping.
Expand Down

0 comments on commit 6027c6b

Please sign in to comment.