Browse Source

[Pal/Linux-SGX] Simplify check for uint32_t overflow of size/len

Dmitrii Kuvaiskii 4 years ago
parent
commit
8e660a1edc

+ 2 - 2
Pal/src/host/Linux-SGX/db_devices.c

@@ -191,7 +191,7 @@ static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void
     if (fd == PAL_IDX_POISON)
         return -PAL_ERROR_DENIED;
 
-    if (size >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (size != (uint32_t)size)
         return -PAL_ERROR_INVAL;
 
     int bytes = ocall_read(fd, buffer, size);
@@ -208,7 +208,7 @@ static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, con
     if (fd == PAL_IDX_POISON)
         return -PAL_ERROR_DENIED;
 
-    if (size >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (size != (uint32_t)size)
         return -PAL_ERROR_INVAL;
 
     int bytes = ocall_write(fd, buffer, size);

+ 2 - 2
Pal/src/host/Linux-SGX/db_pipes.c

@@ -184,7 +184,7 @@ static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void*
         !IS_HANDLE_TYPE(handle, pipe))
         return -PAL_ERROR_NOTCONNECTION;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int fd    = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd;
@@ -208,7 +208,7 @@ static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, cons
         !IS_HANDLE_TYPE(handle, pipe))
         return -PAL_ERROR_NOTCONNECTION;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd;

+ 2 - 2
Pal/src/host/Linux-SGX/db_process.c

@@ -373,7 +373,7 @@ static int64_t proc_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
     if (offset)
         return -PAL_ERROR_INVAL;
 
-    if (count >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (count != (uint32_t)count)
         return -PAL_ERROR_INVAL;
 
     int bytes;
@@ -393,7 +393,7 @@ static int64_t proc_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
     if (offset)
         return -PAL_ERROR_INVAL;
 
-    if (count >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (count != (uint32_t)count)
         return -PAL_ERROR_INVAL;
 
     int bytes;

+ 6 - 6
Pal/src/host/Linux-SGX/db_sockets.c

@@ -467,7 +467,7 @@ static int64_t tcp_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void*
     if (handle->sock.fd == PAL_IDX_POISON)
         return -PAL_ERROR_ENDOFSTREAM;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int bytes = ocall_recv(handle->sock.fd, buf, len, NULL, NULL, NULL, NULL);
@@ -492,7 +492,7 @@ static int64_t tcp_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, const
     if (handle->sock.fd == PAL_IDX_POISON)
         return -PAL_ERROR_CONNFAILED;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int bytes = ocall_send(handle->sock.fd, buf, len, NULL, 0, NULL, 0);
@@ -617,7 +617,7 @@ static int64_t udp_receive(PAL_HANDLE handle, uint64_t offset, uint64_t len, voi
     if (handle->sock.fd == PAL_IDX_POISON)
         return -PAL_ERROR_BADHANDLE;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int ret = ocall_recv(handle->sock.fd, buf, len, NULL, NULL, NULL, NULL);
@@ -635,7 +635,7 @@ static int64_t udp_receivebyaddr(PAL_HANDLE handle, uint64_t offset, uint64_t le
     if (handle->sock.fd == PAL_IDX_POISON)
         return -PAL_ERROR_BADHANDLE;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     struct sockaddr conn_addr;
@@ -667,7 +667,7 @@ static int64_t udp_send(PAL_HANDLE handle, uint64_t offset, uint64_t len, const
     if (handle->sock.fd == PAL_IDX_POISON)
         return -PAL_ERROR_BADHANDLE;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     int bytes = ocall_send(handle->sock.fd, buf, len, NULL, 0, NULL, 0);
@@ -691,7 +691,7 @@ static int64_t udp_sendbyaddr(PAL_HANDLE handle, uint64_t offset, uint64_t len,
     if (!strstartswith_static(addr, URI_PREFIX_UDP))
         return -PAL_ERROR_INVAL;
 
-    if (len >= (1ULL << (sizeof(unsigned int) * 8)))
+    if (len != (uint32_t)len)
         return -PAL_ERROR_INVAL;
 
     addr += static_strlen(URI_PREFIX_UDP);