From 478ac90f2c55cf66c13aa9805bde69bc705647c3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 10 Dec 2024 01:50:58 +0100 Subject: cut: allocate "printed" only if OPT_CHAR or OPT_BYTE Signed-off-by: Denys Vlasenko --- 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 } -- cgit v1.2.3-55-g6feb