Skip to content

Commit eb80530

Browse files
danmarkartben
authored andcommitted
sca: Fix undefined behavior during preprocessing
According to the C standard it is undefined behavior to use preprocessor directives inside macro invocations. Cppcheck stops when it see this UB with an error message, and so this change will improve Cppcheck analysis This is a refactoring to fix UB, no logical change is intended. Signed-off-by: Daniel Marjamäki <daniel.marjamaki@cppchecksolutions.com>
1 parent caeda69 commit eb80530

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

drivers/console/uart_console.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,8 @@ static int uart_console_init(void)
615615
}
616616

617617
/* UART console initializes after the UART device itself */
618-
SYS_INIT(uart_console_init,
619618
#if defined(CONFIG_EARLY_CONSOLE)
620-
PRE_KERNEL_1,
619+
SYS_INIT(uart_console_init, PRE_KERNEL_1, CONFIG_CONSOLE_INIT_PRIORITY);
621620
#else
622-
POST_KERNEL,
623-
#endif
624-
CONFIG_CONSOLE_INIT_PRIORITY);
621+
SYS_INIT(uart_console_init, POST_KERNEL, CONFIG_CONSOLE_INIT_PRIORITY);
622+
#endif /* CONFIG_EARLY_CONSOLE */

include/zephyr/drivers/gpio.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,16 +902,17 @@ static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
902902
"Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
903903
"enabled for a level interrupt.");
904904

905-
__ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
906905
#ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
907-
((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
908-
(flags & GPIO_INT_ENABLE_DISABLE_ONLY) != 0,
906+
#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE GPIO_INT_ENABLE_DISABLE_ONLY
909907
#else
910-
((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
908+
#define GPIO_INT_ENABLE_DISABLE_ONLY_VALUE 0
911909
#endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
912-
"At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
913-
"enabled.");
914910

911+
__ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
912+
((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
913+
(flags & GPIO_INT_ENABLE_DISABLE_ONLY_VALUE) != 0,
914+
"At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be enabled.");
915+
#undef GPIO_INT_ENABLE_DISABLE_ONLY_VALUE
915916
__ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
916917
"Unsupported pin");
917918

include/zephyr/kernel/internal/mm.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@
4444
#define K_MEM_VIRT_OFFSET 0
4545
#endif /* CONFIG_MMU */
4646

47+
#if CONFIG_SRAM_BASE_ADDRESS != 0
48+
#define IS_SRAM_ADDRESS_LOWER(ADDR) ((ADDR) >= CONFIG_SRAM_BASE_ADDRESS)
49+
#else
50+
#define IS_SRAM_ADDRESS_LOWER(ADDR) true
51+
#endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */
52+
53+
54+
#if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0
55+
#define IS_SRAM_ADDRESS_UPPER(ADDR) \
56+
((ADDR) < (CONFIG_SRAM_BASE_ADDRESS + \
57+
(CONFIG_SRAM_SIZE * 1024UL)))
58+
#else
59+
#define IS_SRAM_ADDRESS_UPPER(ADDR) false
60+
#endif
61+
62+
#define IS_SRAM_ADDRESS(ADDR) \
63+
(IS_SRAM_ADDRESS_LOWER(ADDR) && \
64+
IS_SRAM_ADDRESS_UPPER(ADDR))
65+
4766
/**
4867
* @brief Get physical address from virtual address.
4968
*
@@ -115,16 +134,7 @@ static inline uintptr_t k_mem_phys_addr(void *virt)
115134
"address %p not in permanent mappings", virt);
116135
#else
117136
/* Should be identity-mapped */
118-
__ASSERT(
119-
#if CONFIG_SRAM_BASE_ADDRESS != 0
120-
(addr >= CONFIG_SRAM_BASE_ADDRESS) &&
121-
#endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */
122-
#if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0
123-
(addr < (CONFIG_SRAM_BASE_ADDRESS +
124-
(CONFIG_SRAM_SIZE * 1024UL))),
125-
#else
126-
false,
127-
#endif /* (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 */
137+
__ASSERT(IS_SRAM_ADDRESS(addr),
128138
"physical address 0x%lx not in RAM",
129139
(unsigned long)addr);
130140
#endif /* CONFIG_MMU */
@@ -153,16 +163,7 @@ static inline void *k_mem_virt_addr(uintptr_t phys)
153163
__ASSERT(sys_mm_is_phys_addr_in_range(phys),
154164
"physical address 0x%lx not in RAM", (unsigned long)phys);
155165
#else
156-
__ASSERT(
157-
#if CONFIG_SRAM_BASE_ADDRESS != 0
158-
(phys >= CONFIG_SRAM_BASE_ADDRESS) &&
159-
#endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */
160-
#if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0
161-
(phys < (CONFIG_SRAM_BASE_ADDRESS +
162-
(CONFIG_SRAM_SIZE * 1024UL))),
163-
#else
164-
false,
165-
#endif /* (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 */
166+
__ASSERT(IS_SRAM_ADDRESS(phys),
166167
"physical address 0x%lx not in RAM", (unsigned long)phys);
167168
#endif /* CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK */
168169

0 commit comments

Comments
 (0)