Explorar o código

[Pal] Add assert in case zero is fed to IS_POWER_OF_2()

Jia Zhang %!s(int64=6) %!d(string=hai) anos
pai
achega
1f46a8a60c
Modificáronse 2 ficheiros con 4 adicións e 2 borrados
  1. 3 1
      Pal/lib/api.h
  2. 1 1
      Pal/src/host/Linux-SGX/sgx_main.c

+ 3 - 1
Pal/lib/api.h

@@ -68,7 +68,9 @@ typedef ptrdiff_t ssize_t;
 #define SATURATED_P_SUB(ptr_a, b, limit) \
    ((__typeof__(ptr_a))SATURATED_SUB((uintptr_t)(ptr_a), (uintptr_t)(b), (uintptr_t)(limit)))
 
-#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
+#define IS_POWER_OF_2(x) \
+    ({ assert((x) != 0); \
+       (((x) & ((x) - 1)) == 0); })
 
 #define IS_ALIGNED(val, alignment) ((val) % (alignment) == 0)
 #define ALIGN_DOWN(val, alignment) ((val) - (val) % (alignment))

+ 1 - 1
Pal/src/host/Linux-SGX/sgx_main.c

@@ -254,7 +254,7 @@ int initialize_enclave (struct pal_enclave * enclave)
     }
 
     enclave->size = parse_int(cfgbuf);
-    if (!IS_POWER_OF_2(enclave->size)) {
+    if (!enclave->size || !IS_POWER_OF_2(enclave->size)) {
         SGX_DBG(DBG_E, "Enclave size not a power of two (an SGX-imposed requirement)\n");
         ret = -EINVAL;
         goto out;