[PATCH 28/31] more: speed up command_expansion()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Simply copy the command string if there is nothing to expand.

Additionally make variable names easier to understand in
command_expansion(), and remove possible buffer overflow in do_shell().

Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 text-utils/more.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/text-utils/more.c b/text-utils/more.c
index 8cbc62f..84c2108 100644
--- a/text-utils/more.c
+++ b/text-utils/more.c
@@ -971,22 +971,25 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 static int command_expansion(struct more_control *ctl, char **outbuf, char *inbuf)
 {
 	char *inpstr = inbuf;
+	char *allocation;
 	char *outstr;
 	char c;
-	char *temp;
 	int changed = 0;
-	int tempsz, xtra, offset;
+	size_t xtra;
+	ptrdiff_t offset;
 
-	xtra = strlen(ctl->fnames[ctl->argv_position]) + strlen(ctl->shell_line) + 1;
-	tempsz = 200 + xtra;
-	temp = xmalloc(tempsz);
-	outstr = temp;
+	if (!strpbrk(inbuf, "%!\\")) {
+		*outbuf = xstrdup(inbuf);
+		return 0;
+	}
+	xtra = strlen(ctl->fnames[ctl->argv_position]) + strlen(ctl->shell_line) + COMMAND_BUF + 1;
+	outstr = allocation = xmalloc(xtra);
 	while ((c = *inpstr++) != '\0') {
-		offset = outstr - temp;
-		if (tempsz - offset - 1 < xtra) {
-			tempsz += 200 + xtra;
-			temp = xrealloc(temp, tempsz);
-			outstr = temp + offset;
+		offset = outstr - allocation;
+		if (xtra - offset - 1 < xtra) {
+			xtra += COMMAND_BUF;
+			allocation = xrealloc(allocation, xtra);
+			outstr = allocation + offset;
 		}
 		switch (c) {
 		case '%':
@@ -1015,7 +1018,7 @@ static int command_expansion(struct more_control *ctl, char **outbuf, char *inbu
 		}
 	}
 	*outstr++ = '\0';
-	*outbuf = temp;
+	*outbuf = allocation;
 	return changed;
 }
 
@@ -1122,8 +1125,8 @@ static void do_shell(struct more_control *ctl, char *filename)
 		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
 		rc = command_expansion(ctl, &expanded, cmdbuf);
 		if (expanded) {
-			if (strlen(expanded) < sizeof(ctl->shell_line))
-				strcpy(ctl->shell_line, expanded);
+			if (strlen(expanded) < sizeof(ctl->shell_line) - 1)
+				xstrncpy(ctl->shell_line, expanded, sizeof ctl->shell_line);
 			else
 				rc = -1;
 			free(expanded);
-- 
2.3.0

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux