[OpenMP][Offloading] Fixed a crash caused by dereferencing nullptr
authorShilei Tian <i@tianshilei.me>
Thu, 6 Jan 2022 04:04:17 +0000 (23:04 -0500)
committerShilei Tian <i@tianshilei.me>
Thu, 6 Jan 2022 04:04:29 +0000 (23:04 -0500)
In function `DeviceTy::getTargetPointer`, `Entry` could be `nullptr` because of
zero length array section. We need to check if it is a valid iterator before
using it.

Reviewed By: ronlieb

Differential Revision: https://reviews.llvm.org/D116716

openmp/libomptarget/src/device.cpp

index 75935b3..738284e 100644 (file)
@@ -305,7 +305,9 @@ DeviceTy::getTargetPointer(void *HstPtrBegin, void *HstPtrBase, int64_t Size,
     DataMapMtx.unlock();
     // If not a host pointer and no present modifier, we need to wait for the
     // event if it exists.
-    if (!IsHostPtr && !HasPresentModifier) {
+    // Note: Entry might be nullptr because of zero length array section.
+    if (Entry != HostDataToTargetListTy::iterator() && !IsHostPtr &&
+        !HasPresentModifier) {
       Entry->lock();
       void *Event = Entry->getEvent();
       if (Event) {