Skip to contentSkip to conversation

init: Remove strlcat() and sprintf() from do_one_initcall()

PATCH In review Thorsten Blum 2 participants 2 messages Nov 14 – Nov 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 init: Remove strlcat() and sprintf() from do_one_initcall()

In preparation for removing strlcat() from the kernel [1], format the warning directly in WARN(). This also removes the temporary 64-byte buffer and the sprintf() call while preserving the warning text.

---
 init/main.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
init/main.c +11 -10
index 93a2b12976f0..430b403e0880 100644
@@ -1397,7 +1397,8 @@ static inline void do_trace_initcall_level(const char *level)
 int __init_or_module do_one_initcall(initcall_t fn)
 {
 	int count = preempt_count();
-	char msgbuf[64];
+	bool preempt_imbalanced;
+	bool irqs_left_disabled;
 	int ret;
 
 	if (initcall_blacklisted(fn))
@@ -1407,17 +1408,17 @@ int __init_or_module do_one_initcall(initcall_t fn)
 	ret = fn();
 	do_trace_initcall_finish(fn, ret);
 
-	msgbuf[0] = 0;
-
-	if (preempt_count() != count) {
-		sprintf(msgbuf, "preemption imbalance ");
+	preempt_imbalanced = preempt_count() != count;
+	if (preempt_imbalanced)
 		preempt_count_set(count);
-	}
-	if (irqs_disabled()) {
-		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
+
+	irqs_left_disabled = irqs_disabled();
+	if (irqs_left_disabled)
 		local_irq_enable();
-	}
-	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
+
+	WARN(preempt_imbalanced || irqs_left_disabled, "initcall %pS returned with %s%s\n", fn,
+	     preempt_imbalanced ? "preemption imbalance " : "",
+	     irqs_left_disabled ? "disabled interrupts " : "");
 
 	add_latent_entropy();
 	return ret;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · On Sat, 14 Nov 2026 12:05:31 +0100 Thorsten Blum <[email protected]> wrote:

In preparation for removing strlcat() from the kernel [1], format the warning directly in WARN(). This also removes the temporary 64-byte buffer and the sprintf() call while preserving the warning text.

This one actually makes sense!
I might have used 'const char *' variables and removed the ' ' before the '\n'. But:

Quoted 46 lines
---
 init/main.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
init/main.c +11 -10
index 93a2b12976f0..430b403e0880 100644
@@ -1397,7 +1397,8 @@ static inline void do_trace_initcall_level(const char *level)
 int __init_or_module do_one_initcall(initcall_t fn)
 {
 	int count = preempt_count();
-	char msgbuf[64];
+	bool preempt_imbalanced;
+	bool irqs_left_disabled;
 	int ret;
 
 	if (initcall_blacklisted(fn))
@@ -1407,17 +1408,17 @@ int __init_or_module do_one_initcall(initcall_t fn)
 	ret = fn();
 	do_trace_initcall_finish(fn, ret);
 
-	msgbuf[0] = 0;
-
-	if (preempt_count() != count) {
-		sprintf(msgbuf, "preemption imbalance ");
+	preempt_imbalanced = preempt_count() != count;
+	if (preempt_imbalanced)
 		preempt_count_set(count);
-	}
-	if (irqs_disabled()) {
-		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
+
+	irqs_left_disabled = irqs_disabled();
+	if (irqs_left_disabled)
 		local_irq_enable();
-	}
-	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
+
+	WARN(preempt_imbalanced || irqs_left_disabled, "initcall %pS returned with %s%s\n", fn,
+	     preempt_imbalanced ? "preemption imbalance " : "",
+	     irqs_left_disabled ? "disabled interrupts " : "");
 
 	add_latent_entropy();
 	return ret;