[git commit] *: use xasprintf_inplace() and concat_path_file() where appropriate

Denys Vlasenko vda.linux at googlemail.com
Fri Feb 6 12:16:36 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=c8cbd3516d85e029b17d17c27d249ea727df3db9
branch: https://git.busybox.net/busybox/log/?h=master

function                                             old     new   delta
acpid_main                                          1059    1063      +4
resume_main                                          561     560      -1
unpack_package                                       642     640      -2
adduser_main                                         861     859      -2
getty_main                                          1517    1512      -5
ftpd_main                                           2149    2142      -7
.rodata                                           107018  107010      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/6 up/down: 4/-25)             Total: -21 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 archival/dpkg.c           |  2 +-
 klibc-utils/resume.c      |  2 +-
 loginutils/adduser.c      |  2 +-
 loginutils/getty.c        |  2 +-
 modutils/modprobe-small.c |  6 ++----
 networking/ftpd.c         | 13 ++++++-------
 util-linux/acpid.c        |  2 +-
 7 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/archival/dpkg.c b/archival/dpkg.c
index 8031956e9..eda5ec7eb 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -1689,7 +1689,7 @@ static void unpack_package(deb_file_t *deb_file)
 	accept_list = NULL;
 	i = 0;
 	while (i < ARRAY_SIZE(all_control_files)) {
-		char *c = xasprintf("./%s", all_control_files[i]);
+		char *c = concat_path_file(".", all_control_files[i]);
 		llist_add_to(&accept_list, c);
 		i++;
 	}
diff --git a/klibc-utils/resume.c b/klibc-utils/resume.c
index 7b9d18b5d..fde5587e2 100644
--- a/klibc-utils/resume.c
+++ b/klibc-utils/resume.c
@@ -59,7 +59,7 @@ static dev_t name_to_dev_t(const char *devname)
 				return res;
 		}
 
-		devname = xasprintf("/dev/%s", devname);
+		devname = concat_path_file("/dev", devname);
 	}
 	/* Now devname is always "/dev/FOO" */
 
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index bfab05203..ac43feb48 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -227,7 +227,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
 	die_if_bad_username(pw.pw_name);
 	if (!pw.pw_dir) {
 		/* create string for $HOME if not specified already */
-		pw.pw_dir = xasprintf("/home/%s", argv[0]);
+		pw.pw_dir = concat_path_file("/home", argv[0]);
 	}
 	pw.pw_passwd = (char *)"x";
 	if (opts & OPT_SYSTEM_ACCOUNT) {
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 4581cc9f7..67a08f487 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -213,7 +213,7 @@ static void open_tty(void)
 	/* Set up new standard input, unless we are given an already opened port */
 	if (NOT_LONE_DASH(G.tty_name)) {
 		if (G.tty_name[0] != '/')
-			G.tty_name = xasprintf("/dev/%s", G.tty_name); /* will leak it */
+			G.tty_name = concat_path_file("/dev", G.tty_name); /* will leak it */
 
 		/* Open the tty as standard input */
 		debug("open(2)\n");
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index b3c0768ee..7f584102d 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -723,7 +723,7 @@ static int process_module(char *name, const char *cmdline_options)
 
 	options = NULL;
 	if (!is_remove) {
-		char *opt_filename = xasprintf("/etc/modules/%s", name);
+		char *opt_filename = concat_path_file("/etc/modules", name);
 		options = xmalloc_open_read_close(opt_filename, NULL);
 		if (options)
 			replace_char(options, '\n', ' ');
@@ -731,10 +731,8 @@ static int process_module(char *name, const char *cmdline_options)
 		if (cmdline_options) {
 			/* NB: cmdline_options always have one leading ' '
 			 * (see main()), we remove it here */
-			char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
+			xasprintf_inplace(options, options ? "%s %s" : "%s %s" + 3,
 						cmdline_options + 1, options);
-			free(options);
-			options = op;
 		}
 #endif
 		free(opt_filename);
diff --git a/networking/ftpd.c b/networking/ftpd.c
index c3125410e..96db5d9fd 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -491,19 +491,18 @@ static void
 handle_pasv(void)
 {
 	unsigned port;
-	char *addr, *response;
+	char *response;
 
 	port = bind_for_passive_mode();
 
 	if (G.local_addr->u.sa.sa_family == AF_INET)
-		addr = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
+		response = xmalloc_sockaddr2dotted_noport(&G.local_addr->u.sa);
 	else /* seen this in the wild done by other ftp servers: */
-		addr = xstrdup("0.0.0.0");
-	replace_char(addr, '.', ',');
+		response = xstrdup("0.0.0.0");
+	replace_char(response, '.', ',');
 
-	response = xasprintf(STR(FTP_PASVOK)" PASV ok (%s,%u,%u)\r\n",
-			addr, (int)(port >> 8), (int)(port & 255));
-	free(addr);
+	xasprintf_inplace(response, STR(FTP_PASVOK)" PASV ok (%s,%u,%u)\r\n",
+			response, (int)(port >> 8), (int)(port & 255));
 	cmdio_write_raw(response);
 	free(response);
 }
diff --git a/util-linux/acpid.c b/util-linux/acpid.c
index 5c0bb1768..386b988d2 100644
--- a/util-linux/acpid.c
+++ b/util-linux/acpid.c
@@ -145,7 +145,7 @@ struct globals {
 static void process_event(const char *event)
 {
 	struct stat st;
-	char *handler = xasprintf("./%s", event);
+	char *handler = concat_path_file(".", event);
 	const char *args[] = { "run-parts", handler, NULL };
 
 	// log the event


More information about the busybox-cvs mailing list