Browse Source

[Linux/SGX PAL] Check NULL handles in DkObjectsWaitAny()

Chia-Che Tsai 5 years ago
parent
commit
e8f334b220
3 changed files with 17 additions and 5 deletions
  1. 1 1
      Pal/src/db_object.c
  2. 10 3
      Pal/src/host/Linux-SGX/db_object.c
  3. 6 1
      Pal/src/host/Linux/db_object.c

+ 1 - 1
Pal/src/db_object.c

@@ -89,7 +89,7 @@ DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout)
 
     for (int i = 0 ; i < count ; i++)
         // We modify the caller's handleArray?
-        if (UNKNOWN_HANDLE(handleArray[i]))
+        if (handleArray[i] && UNKNOWN_HANDLE(handleArray[i]))
             handleArray[i] = NULL;
 
     PAL_HANDLE polled = NULL;

+ 10 - 3
Pal/src/host/Linux-SGX/db_object.c

@@ -95,7 +95,7 @@ static int _DkObjectWaitOne (PAL_HANDLE handle, uint64_t timeout)
 
     const struct handle_ops * ops = HANDLE_OPS(handle);
 
-    if (!ops->wait)
+    if (!ops || !ops->wait)
         return -PAL_ERROR_NOTSUPPORT;
 
     return ops->wait(handle, timeout);
@@ -110,8 +110,15 @@ int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
         return 0;
 
     if (count == 1) {
-        *polled = handleArray[0];
-        return _DkObjectWaitOne(handleArray[0], timeout);
+        // It is possible to have NULL pointers in the handle array.
+        // In this case, assume nothing is polled.
+        if (!handleArray[0])
+            return -PAL_ERROR_TRYAGAIN;
+
+        int rv = _DkObjectWaitOne(handleArray[0], timeout);
+        if (rv == 0)
+            *polled = handleArray[0];
+        return rv;
     }
 
     int i, j, ret, maxfds = 0, nfds = 0;

+ 6 - 1
Pal/src/host/Linux/db_object.c

@@ -117,7 +117,7 @@ static int _DkObjectWaitOne (PAL_HANDLE handle, uint64_t timeout)
 
     const struct handle_ops * ops = HANDLE_OPS(handle);
 
-    if (!ops->wait)
+    if (!ops || !ops->wait)
         return -PAL_ERROR_NOTSUPPORT;
 
     return ops->wait(handle, timeout);
@@ -132,6 +132,11 @@ int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
         return 0;
 
     if (count == 1) {
+        // It is possible to have NULL pointers in the handle array.
+        // In this case, assume nothing is polled.
+        if (!handleArray[0])
+            return -PAL_ERROR_TRYAGAIN;
+
         int rv = _DkObjectWaitOne(handleArray[0], timeout);
         if (rv == 0)
             *polled = handleArray[0];