[git commit] *: use xasprintf_inplace() in more places

Denys Vlasenko vda.linux at googlemail.com
Fri Feb 6 10:19:11 UTC 2026


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

function                                             old     new   delta
.rodata                                           107009  107018      +9
parse_stream                                        3075    3069      -6
buffer_print                                         612     603      -9
expand_args                                          159     144     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 9/-30)             Total: -21 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/vi.c     | 16 +++++++++-------
 miscutils/less.c | 14 +++++++-------
 shell/hush.c     |  5 +----
 3 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index f48bcf514..5d736a3a5 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2682,19 +2682,23 @@ static void setops(char *args, int flg_no)
 # if ENABLE_FEATURE_VI_COLON_EXPAND
 static char *expand_args(char *args)
 {
-	char *s, *t;
+	char *s;
 	const char *replace;
 
 	args = xstrdup(args);
 	for (s = args; *s; s++) {
+		unsigned n;
+
 		if (*s == '%') {
 			replace = current_filename;
 		} else if (*s == '#') {
 			replace = alt_filename;
 		} else {
 			if (*s == '\\' && s[1] != '\0') {
-				for (t = s++; *t; t++)
+				char *t;
+				for (t = s; *t; t++)
 					*t = t[1];
+				s++;
 			}
 			continue;
 		}
@@ -2705,11 +2709,9 @@ static char *expand_args(char *args)
 			return NULL;
 		}
 
-		*s = '\0';
-		t = xasprintf("%s%s%s", args, replace, s+1);
-		s = t + (s - args) + strlen(replace);
-		free(args);
-		args = t;
+		n = (s - args);
+		xasprintf_inplace(args, "%.*s%s%s", n, args, replace, s+1);
+		s = args + n + strlen(replace);
 	}
 	return args;
 }
diff --git a/miscutils/less.c b/miscutils/less.c
index 2204b1cec..e24500591 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -839,13 +839,13 @@ static void print_found(const char *line)
 	goto start;
 
 	while (match_status == 0) {
-		char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
-				growline ? growline : "",
-				(int)match_structs.rm_so, str,
-				(int)(match_structs.rm_eo - match_structs.rm_so),
-						str + match_structs.rm_so);
-		free(growline);
-		growline = new;
+		xasprintf_inplace(growline,
+			"%s%.*s"HIGHLIGHT"%.*s"NORMAL,
+			growline ? growline : "",
+			(int)match_structs.rm_so,
+				str,
+			(int)(match_structs.rm_eo - match_structs.rm_so),
+				str + match_structs.rm_so);
 		str += match_structs.rm_eo;
 		line += match_structs.rm_eo;
 		eflags = REG_NOTBOL;
diff --git a/shell/hush.c b/shell/hush.c
index da0db7948..23e7f512f 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2941,12 +2941,9 @@ static void i_prepend_to_alias_buffer(struct in_str *i, char *prepend, char ch)
 		 * a
 		 * ^^^ runs "echo A: c"
 		 */
-		char *old = i->albuf;
 		//bb_error_msg("before'%s' p'%s'", i->albuf, i->p);
-		i->albuf = xasprintf("%s%c%s", prepend, ch, i->p);
-		i->p = i->albuf;
+		i->p = xasprintf_inplace(i->albuf, "%s%c%s", prepend, ch, i->p);
 		//bb_error_msg("after'%s' p'%s'", i->albuf, i->p);
-		free(old);
 		return;
 	}
 	i->saved_ibuf = i->p;


More information about the busybox-cvs mailing list