Skip to content

Commit 5661dd9

Browse files
nathanchancepmladek
authored andcommitted
printk: Convert a use of sprintf to snprintf in console_unlock
When CONFIG_PRINTK is disabled (e.g. when building allnoconfig), clang warns: ../kernel/printk/printk.c:2416:10: warning: 'sprintf' will always overflow; destination buffer has size 0, but format string expands to at least 33 [-Wfortify-source] len = sprintf(text, ^ 1 warning generated. It is not wrong; text has a zero size when CONFIG_PRINTK is disabled because LOG_LINE_MAX and PREFIX_MAX are both zero. Change to snprintf so that this case is explicitly handled without any risk of overflow. Link: ClangBuiltLinux#846 Link: llvm/llvm-project@6d485ff Link: http://lkml.kernel.org/r/[email protected] Cc: Steven Rostedt <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Nathan Chancellor <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]>
1 parent a4fe2b4 commit 5661dd9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kernel/printk/printk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,9 +2413,9 @@ void console_unlock(void)
24132413
printk_safe_enter_irqsave(flags);
24142414
raw_spin_lock(&logbuf_lock);
24152415
if (console_seq < log_first_seq) {
2416-
len = sprintf(text,
2417-
"** %llu printk messages dropped **\n",
2418-
log_first_seq - console_seq);
2416+
len = snprintf(text, sizeof(text),
2417+
"** %llu printk messages dropped **\n",
2418+
log_first_seq - console_seq);
24192419

24202420
/* messages are gone, move to first one */
24212421
console_seq = log_first_seq;

0 commit comments

Comments
 (0)