Skip to content

Commit 3d6f83d

Browse files
visitorckwpmladek
authored andcommitted
printk: Fix signed integer overflow when defining LOG_BUF_LEN_MAX
Shifting 1 << 31 on a 32-bit int causes signed integer overflow, which leads to undefined behavior. To prevent this, cast 1 to u32 before performing the shift, ensuring well-defined behavior. This change explicitly avoids any potential overflow by ensuring that the shift occurs on an unsigned 32-bit integer. Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Acked-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20240928113608.1438087-1-visitorckw@gmail.com Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 7d66d3a commit 3d6f83d

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

kernel/printk/printk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static struct latched_seq clear_seq = {
523523
/* record buffer */
524524
#define LOG_ALIGN __alignof__(unsigned long)
525525
#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
526-
#define LOG_BUF_LEN_MAX (u32)(1 << 31)
526+
#define LOG_BUF_LEN_MAX ((u32)1 << 31)
527527
static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
528528
static char *log_buf = __log_buf;
529529
static u32 log_buf_len = __LOG_BUF_LEN;

0 commit comments

Comments
 (0)