sed: free allocated memory under lint, remove DEBUG_LEAKS

Under normal operation there is no need for explicit freeing,
as all memory will be released when sed terminates.
During development (and specifically, valgrind and address-sanitizing)
enabling lint prevents false-positive warnings about memory
leaks.
Lint mode can be enabled with CFLAGS="-Dlint". It is also automatically
enabled by default when building from git (as opposed to tarball).

For consistency all instances of "#ifdef DEBUG_LEAKS" are replaced
with "#ifdef lint".

* sed/sed.h (struct subst): Add member variable to keep the address of
allocated buffer in compile.c:setup_replacement().
(release_regex): Add declaration.
(finish_program): Function now takes an argument: the sed program
vector.
* sed/sed.c (main): Adjust call to finish_program.
* sed/compile.c (finish_program): Release program allocations.
(setup_replacement): Remember the allocated buffer address.
(compile_program): Free temporary array in 'y' command.
* sed/execute.c (execute_program): Free allocated but unused buffer in
'R' command when reaching EOF.
* sed/regexp.c (release_regex): Remove 'static', free the allocated
dfa struct.
* sed/utils.c (panic): Free open files linked-list elements.
This commit is contained in:
Assaf Gordon committed 2018-08-08 14:14:39 -06:00
1 parent 08675afe9a
commit 35b30b124c
6 files changed
+59 -10

No files matched your search

+29 -3
View File
@@ -757,6 +757,8 @@ setup_replacement (struct subst *sub, const char *text, size_t length)
base = MEMDUP (text, length, char);
length = normalize_text (base, length, TEXT_REPLACEMENT);
IF_LINT (sub->replacement_buffer = base);
text_end = base + length;
tail = &root;
@@ -1330,6 +1332,8 @@ compile_program (struct vector *vector)
trans_pairs[2 * i] = NULL;
if (idx != dest_len)
bad_prog (_(Y_CMD_LEN));
IF_LINT (free (src_lens));
}
else
{
@@ -1678,7 +1682,7 @@ rewind_read_files (void)
/* Release all resources which were allocated in this module. */
void
finish_program (void)
finish_program (struct vector *program)
{
/* close all files... */
{
@@ -1708,7 +1712,29 @@ finish_program (void)
file_read = file_write = NULL;
}
#ifdef DEBUG_LEAKS
#ifdef lint
for (int i = 0; i < program->v_length; ++i)
{
const struct sed_cmd *sc = &program->v[i];
if (sc->a1 && sc->a1->addr_regex)
release_regex (sc->a1->addr_regex);
if (sc->a2 && sc->a2->addr_regex)
release_regex (sc->a2->addr_regex);
switch (sc->cmd)
{
case 's':
free (sc->x.cmd_subst->replacement_buffer);
if (sc->x.cmd_subst->regx)
release_regex (sc->x.cmd_subst->regx);
break;
}
}
obstack_free (&obs, NULL);
#endif /*DEBUG_LEAKS*/
#else
(void)program;
#endif /* lint */
}
+9 -2
View File
@@ -1502,6 +1502,13 @@ execute_program (struct vector *vec, struct input *input)
aq->text = text;
aq->textlen = result;
}
else
{
/* The external input file (for R command) reached EOF,
the 'text' buffer will not be added to the append queue
so release it */
free (text);
}
}
break;
@@ -1683,7 +1690,7 @@ process_files (struct vector *the_program, char **argv)
}
closedown (&input);
#ifdef DEBUG_LEAKS
#ifdef lint
/* We're about to exit, so these free()s are redundant.
But if we're running under a memory-leak detecting
implementation of malloc(), we want to explicitly
@@ -1694,7 +1701,7 @@ process_files (struct vector *the_program, char **argv)
free (hold.text);
free (line.text);
free (s_accum.text);
#endif /*DEBUG_LEAKS*/
#endif /* lint */
if (input.bad_count)
status = EXIT_BAD_INPUT;
+8 -2
View File
@@ -425,11 +425,17 @@ match_regex (struct regex *regex, char *buf, size_t buflen,
}
#ifdef DEBUG_LEAKS
#ifdef lint
void
release_regex (struct regex *regex)
{
if (regex->dfa)
{
dfafree (regex->dfa);
free (regex->dfa);
regex->dfa = NULL;
}
regfree (&regex->pattern);
free (regex);
}
#endif /*DEBUG_LEAKS*/
#endif /* lint */
+1 -1
View File
@@ -375,7 +375,7 @@ main (int argc, char **argv)
return_code = process_files (the_program, argv+optind);
finish_program ();
finish_program (the_program);
ck_fclose (NULL);
return return_code;
+5 -2
View File
@@ -126,6 +126,9 @@ struct subst {
unsigned print : 2; /* 'p' option given (before/after eval) */
unsigned eval : 1; /* 'e' option given */
unsigned max_id : 4; /* maximum backreference on the RHS */
#ifdef lint
char* replacement_buffer;
#endif
};
#ifdef REG_PERL
@@ -190,13 +193,13 @@ struct vector *compile_string (struct vector *, char *str, size_t len);
struct vector *compile_file (struct vector *, const char *cmdfile);
void check_final_program (struct vector *);
void rewind_read_files (void);
void finish_program (void);
void finish_program (struct vector *);
struct regex *compile_regex (struct buffer *b, int flags, int needed_sub);
int match_regex (struct regex *regex,
char *buf, size_t buflen, size_t buf_start_offset,
struct re_registers *regarray, int regsize);
#ifdef DEBUG_LEAKS
#ifdef lint
void release_regex (struct regex *);
#endif
+7
View File
@@ -74,7 +74,14 @@ panic (const char *str, ...)
strerror (errno));
}
#ifdef lint
struct open_file *next = open_files->link;
free (open_files->name);
free (open_files);
open_files = next;
#else
open_files = open_files->link;
#endif
}
exit (EXIT_PANIC);