gbranden pushed a commit to branch master in repository groff. commit 8a62f58d924776a9454ef43899060c95de6acf0a Author: G. Branden Robinson <g.branden.robin...@gmail.com> AuthorDate: Sun Feb 23 18:05:18 2025 -0600
[troff]: Free heap-allocated memory more reliably. * src/roff/troff/input.cpp: More reliably release heap memory allocated by `read_string()`. (next_file, do_source, pipe_source_request, pipe_output) (system_request, copy_file, transparent_file, do_macro_source): Do it. It is not necessary to make conditional the `delete[]` of a null pointer. "If the _delete-expression_ calls the implementation deallocation function (3.7.3.2), and if the operand of the delete expression is not the null pointer constant, the deallocation function will deallocate the storage referenced by the pointer thus rendering the pointer invalid" (ISO/IEC 14882-1998, ยง5.3.5, paragraph 4). Or as Stroustrup puts it, "Applying _delete_ to zero has no effect." (_The C++ Programming Language, Special Edition_, p. 128). --- ChangeLog | 8 ++++++++ src/roff/troff/input.cpp | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index a4d0b4926..2d28782f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-02-23 G. Branden Robinson <g.branden.robin...@gmail.com> + + * src/roff/troff/input.cpp: More reliably release heap memory + allocated by `read_string()`. + (next_file, do_source, pipe_source_request, pipe_output) + (system_request, copy_file, transparent_file, do_macro_source): + Do it. + 2025-02-23 G. Branden Robinson <g.branden.robin...@gmail.com> * src/roff/troff/input.cpp (class file_iterator): Make class diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp index 2b1f0faff..2656ff236 100644 --- a/src/roff/troff/input.cpp +++ b/src/roff/troff/input.cpp @@ -931,6 +931,7 @@ void next_file() else input_stack::next_file(fp, filename); } + delete[] filename; tok.next(); } @@ -6787,6 +6788,7 @@ void do_source(bool quietly) // expected problem. if (!(quietly && (ENOENT == errno))) error("cannot open '%1': %2", filename, strerror(errno)); + delete[] filename; tok.next(); } @@ -6827,7 +6829,7 @@ void pipe_source_request() // .pso return; } char *pcmd = read_string(); - // This shouldn't happen thanks to `has_arg()` above. + // `has_arg()` should have ensured that this pointer is non-null. assert(pcmd != 0 /* nullptr */); if (0 /* nullptr */ == pcmd) error("cannot apply piped command source request to empty" @@ -7604,6 +7606,7 @@ static void open_file(bool appending) stream_dictionary.define(stream, (object *)grost); } } + delete[] filename; tok.next(); } } @@ -8614,7 +8617,7 @@ void pipe_output() return; } char *pc = read_string(); - // This shouldn't happen thanks to `has_arg()` above. + // `has_arg()` should have ensured that this pointer is non-null. assert(pc != 0 /* nullptr */); if (0 /* nullptr */ == pc) error("cannot apply pipe request to empty command"); @@ -8633,6 +8636,7 @@ void pipe_output() } else pipe_command = pc; + delete[] pc; tok.next(); } @@ -8653,14 +8657,13 @@ void system_request() return; } char *command = read_string(); - // This shouldn't happen thanks to `has_arg()` above. + // `has_arg()` should have ensured that this pointer is non-null. assert(command != 0 /* nullptr */); if (0 /* nullptr */ == command) error("cannot apply system request to empty command"); - else { + else system_status = system(command); - delete[] command; - } + delete[] command; tok.next(); } @@ -8687,6 +8690,7 @@ void copy_file() curenv->do_break(); if (filename != 0 /* nullptr */) curdiv->copy_file(filename); + delete[] filename; tok.next(); } @@ -8751,6 +8755,7 @@ void transparent_file() fclose(fp); } } + delete[] filename; tok.next(); } @@ -8902,6 +8907,7 @@ void do_macro_source(bool quietly) if (!quietly && (ENOENT == errno)) warning(WARN_FILE, "cannot open macro file '%1': %2", macro_filename, strerror(errno)); + delete[] macro_filename; tok.next(); } _______________________________________________ groff-commit mailing list groff-commit@gnu.org https://lists.gnu.org/mailman/listinfo/groff-commit