From: Tudor Cretu <tudor.cr...@arm.com>

Calling fclose on an already closed file is undefined behaviour.

Rather than close cmd->sw_file, we instead walk through G.FILE_head and
close all the files from there. (cmd->sw_file is populated by
sed_xfopen_w, which adds the reference to G.FILE).

Signed-off-by: Tudor Cretu <tudor.cr...@arm.com>
Reviewed-by: Steve Capper <steve.cap...@arm.com>
[SteveC: add last paragraph to commit log]
Signed-off-by: Steve Capper <steve.cap...@arm.com>
---
 editors/sed.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/editors/sed.c b/editors/sed.c
index 32a4b61f6..c6b9474ae 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -182,15 +182,18 @@ struct globals {
 static void sed_free_and_close_stuff(void)
 {
        sed_cmd_t *sed_cmd = G.sed_cmd_head;
+       struct sed_FILE *sed_file = G.FILE_head;
 
        llist_free(G.append_head, free);
 
+       while (sed_file) {
+               fclose(sed_file->fp);
+               sed_file = sed_file->next;
+       }
+
        while (sed_cmd) {
                sed_cmd_t *sed_cmd_next = sed_cmd->next;
 
-               if (sed_cmd->sw_file)
-                       fclose(sed_cmd->sw_file);
-
                /* Used to free regexps, but now there is code
                 * in get_address() which can reuse a regexp
                 * for constructs as /regexp/cmd1;//cmd2
-- 
2.35.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to