Skip to content

Commit 87d056b

Browse files
Flavio Ceolinhenrikbrixandersen
authored andcommitted
syscall: Fix static analysis compalins
Since K_SYSCALL_MEMORY can be called with signed/unsigned size types, if we check if size >= 0, static anlysis will complain about it when size in unsigned. Signed-off-by: Flavio Ceolin <[email protected]>
1 parent ff89703 commit 87d056b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

include/zephyr/internal/syscall_handler.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,22 @@ int k_usermode_string_copy(char *dst, const char *src, size_t maxlen);
394394
*/
395395
#define K_SYSCALL_VERIFY(expr) K_SYSCALL_VERIFY_MSG(expr, #expr)
396396

397+
/**
398+
* @brief Macro to check if size is negative
399+
*
400+
* K_SYSCALL_MEMORY can be called with signed/unsigned types
401+
* and because of that if we check if size is greater or equal to
402+
* zero, many static analyzers complain about no effect expression.
403+
*
404+
* @param ptr Memory area to examine
405+
* @param size Size of the memory area
406+
* @return true if size is valid, false otherwise
407+
* @note This is an internal API. Do not use unless you are extending
408+
* functionality in the Zephyr tree.
409+
*/
410+
#define K_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
411+
(((uintptr_t)ptr + size) >= (uintptr_t)ptr)
412+
397413
/**
398414
* @brief Runtime check that a user thread has read and/or write permission to
399415
* a memory area
@@ -413,7 +429,8 @@ int k_usermode_string_copy(char *dst, const char *src, size_t maxlen);
413429
* functionality in the Zephyr tree.
414430
*/
415431
#define K_SYSCALL_MEMORY(ptr, size, write) \
416-
K_SYSCALL_VERIFY_MSG((size >= 0) && !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
432+
K_SYSCALL_VERIFY_MSG(K_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
433+
&& !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
417434
&& (arch_buffer_validate((void *)ptr, size, write) \
418435
== 0), \
419436
"Memory region %p (size %zu) %s access denied", \

0 commit comments

Comments
 (0)