mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] vsprintf: make basprintf an alias of xasprintf
Date: Thu, 13 Mar 2025 07:42:09 +0100	[thread overview]
Message-ID: <20250313064210.74011-1-a.fatoum@pengutronix.de> (raw)

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




             reply	other threads:[~2025-03-13  6:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13  6:42 Ahmad Fatoum [this message]
2025-03-13  6:42 ` [PATCH 2/2] treewide: remove NULL checks for revised basprintf Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250313064210.74011-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox