[git commit] cut: allocate "printed" only if OPT_CHAR or OPT_BYTE

Denys Vlasenko vda.linux at googlemail.com
Tue Dec 10 00:50:58 UTC 2024


commit: https://git.busybox.net/busybox/commit/?id=478ac90f2c55cf66c13aa9805bde69bc705647c3
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/cut.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/coreutils/cut.c b/coreutils/cut.c
index e81c6fecb..ca2408f97 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -101,12 +101,13 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
 
 		/* set up a list so we can keep track of what's been printed */
 		int linelen = strlen(line);
-		char *printed = xzalloc(linelen + 1);
 		char *orig_line = line;
 		unsigned cl_pos = 0;
 
 		/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
 		if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
+			char *printed = xzalloc(linelen + 1);
+
 			/* print the chars specified in each cut list */
 			for (; cl_pos < nlists; cl_pos++) {
 				int spos;
@@ -120,6 +121,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
 					}
 				}
 			}
+			free(printed);
 		} else if (*delim == '\n') {	/* cut by lines */
 			int spos = cut_lists[cl_pos].startpos;
 
@@ -214,9 +216,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
 		putchar('\n');
  next_line:
 		linenum++;
-		free(printed);
 		free(orig_line);
-	}
+	} /* while (got line) */
 #undef opt_REGEX
 }
 


More information about the busybox-cvs mailing list