Skip to content

Commit e9fcfa1

Browse files
Flavio Ceolincfriedt
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 1d16757 commit e9fcfa1

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

include/syscall_handler.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ extern int z_user_string_copy(char *dst, const char *src, size_t maxlen);
329329
*/
330330
#define Z_SYSCALL_VERIFY(expr) Z_SYSCALL_VERIFY_MSG(expr, #expr)
331331

332+
/**
333+
* @brief Macro to check if size is negative
334+
*
335+
* Z_SYSCALL_MEMORY can be called with signed/unsigned types
336+
* and because of that if we check if size is greater or equal to
337+
* zero, many static analyzers complain about no effect expression.
338+
*
339+
* @param ptr Memory area to examine
340+
* @param size Size of the memory area
341+
* @return true if size is valid, false otherwise
342+
* @note This is an internal API. Do not use unless you are extending
343+
* functionality in the Zephyr tree.
344+
*/
345+
#define Z_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
346+
(((uintptr_t)ptr + size) >= (uintptr_t)ptr)
347+
332348
/**
333349
* @brief Runtime check that a user thread has read and/or write permission to
334350
* a memory area
@@ -346,7 +362,8 @@ extern int z_user_string_copy(char *dst, const char *src, size_t maxlen);
346362
* @return 0 on success, nonzero on failure
347363
*/
348364
#define Z_SYSCALL_MEMORY(ptr, size, write) \
349-
Z_SYSCALL_VERIFY_MSG((size >= 0) && !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
365+
Z_SYSCALL_VERIFY_MSG(Z_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
366+
&& !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
350367
&& (arch_buffer_validate((void *)ptr, size, write) \
351368
== 0), \
352369
"Memory region %p (size %zu) %s access denied", \

0 commit comments

Comments
 (0)