[PATCH] printk: Do not accept an empty name or devname as a preferred console

From: Petr Mladek

Date: Tue Nov 03 2026 - 12:02:20 EST


The current code for storing preferred consoles does not expect
the case when an empty string is passed via @name or @devname.

Empty console driver name does not make any sense. Avoid handling
such entries completely.

Suggested-by: Chris Down <chris@xxxxxxxxxxxxxx>
Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index b7543fea865b..6bba67e142e1 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2558,7 +2558,9 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
*
* The function adds or updates an entry in preferred_consoles[] array. Also
* it updates some global variables which are used to make the right decisions
- * in register_console().
+ * in register_console(). Finally, it enforces some rules:
+ *
+ * 1. Do not accept empty @name or @devname.
*/
static int add_or_update_preferred_console(unsigned int i,
const char *name, const short idx,
@@ -2576,6 +2578,11 @@ static int add_or_update_preferred_console(unsigned int i,
if (!name && !devname)
return -EINVAL;

+ if ((name && !name[0]) || (devname && !devname[0])) {
+ pr_err("Adding a preferred console with an empty name\n");
+ return -EINVAL;
+ }
+
/*
* We use a signed short index for struct console for device drivers to
* indicate a not yet assigned index or port. However, a negative index
--
2.56.0

Best Regards,
Petr