mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] vsprintf: make basprintf an alias of xasprintf
@ 2025-03-13  6:42 Ahmad Fatoum
  2025-03-13  6:42 ` [PATCH 2/2] treewide: remove NULL checks for revised basprintf Ahmad Fatoum
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-03-13  6:42 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Less than 12% of all our basprintf callsites actually bother to check
for allocation failure. It's highly unlikely that barebox will be able
to function when the heap is so full that the usually small basprintf
requests fail.

Therefore, make basprintf an alias for xasprintf, so barebox panics on
OOM instead of possibly triggering undefined behavior.

Users doing big allocations with basprintf and wishing to handle their
failure should switch over to asprintf instead.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/stdio.h |  8 +++-----
 lib/vsprintf.c  | 17 -----------------
 2 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h
index c02d383d6bad..0e01a0e63a46 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -5,6 +5,7 @@
 #include <stdarg.h>
 #include <console.h>
 #include <printf.h>
+#include <xfuncs.h>
 
 /*
  * STDIO based functions (can always be used)
@@ -21,15 +22,10 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
 
 #if IN_PROPER || defined(CONFIG_PBL_CONSOLE)
-char *basprintf(const char *fmt, ...) __attribute__ ((format(__printf__, 1, 2)));
 int asprintf(char **strp, const char *fmt, ...)  __attribute__ ((format(__printf__, 2, 3)));
 char *bvasprintf(const char *fmt, va_list ap);
 int vasprintf(char **strp, const char *fmt, va_list ap);
 #else
-static inline char *basprintf(const char *fmt, ...)
-{
-	return NULL;
-}
 static inline int asprintf(char **strp, const char *fmt, ...)
 {
 	return -1;
@@ -44,6 +40,8 @@ static inline int vasprintf(char **strp, const char *fmt, va_list ap)
 }
 #endif
 
+#define basprintf xasprintf
+
 #ifdef CONFIG_ARCH_HAS_CTRLC
 int arch_ctrlc(void);
 #endif
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index cb42caebca16..9d3c7e09f08a 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -978,20 +978,3 @@ int asprintf(char **strp, const char *fmt, ...)
 	return len;
 }
 EXPORT_SYMBOL(asprintf);
-
-char *basprintf(const char *fmt, ...)
-{
-	va_list ap;
-	char *p;
-	int len;
-
-	va_start(ap, fmt);
-	len = vasprintf(&p, fmt, ap);
-	va_end(ap);
-
-	if (len < 0)
-		return NULL;
-
-	return p;
-}
-EXPORT_SYMBOL(basprintf);
-- 
2.39.5




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-13  6:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-13  6:42 [PATCH 1/2] vsprintf: make basprintf an alias of xasprintf Ahmad Fatoum
2025-03-13  6:42 ` [PATCH 2/2] treewide: remove NULL checks for revised basprintf Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox