From 78df8828a0a5d8e2d8ff3dced562bf1778ce2e6c Mon Sep 17 00:00:00 2001
From: "H. Peter Anvin (Intel)" <hpa@zytor.com>
Date: Thu, 30 Jul 2020 17:06:24 -0700
Subject: [PATCH] output/codeview.c: use list_for_each_safe() to free a list

https://github.com/netwide-assembler/nasm/commit/78df8828a0a5d8e2d8ff3dced562bf1778ce2e6c

Using list_for_each() is by definition not safe when freeing the
members of the list, use list_for_each_free() instead.

Also, use nasm_new() and nasm_free() where appropriate.

This was discovered as a downstream bug from BR 3392707.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
---
 output/codeview.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/output/codeview.c b/output/codeview.c
index be3fd27a..8276a4f3 100644
--- a/output/codeview.c
+++ b/output/codeview.c
@@ -305,7 +305,7 @@ static void build_type_table(struct coff_Section *const sect);
 static void cv8_cleanup(void)
 {
     struct cv8_symbol *sym;
-    struct source_file *file;
+    struct source_file *file, *ftmp;
 
     struct coff_Section *symbol_sect = coff_sects[cv8_state.symbol_sect];
     struct coff_Section *type_sect = coff_sects[cv8_state.type_sect];
@@ -316,10 +316,10 @@ static void cv8_cleanup(void)
     build_symbol_table(symbol_sect);
     build_type_table(type_sect);
 
-    list_for_each(file, cv8_state.source_files) {
+    list_for_each_safe(file, ftmp, cv8_state.source_files) {
         nasm_free(file->fullname);
         saa_free(file->lines);
-        free(file);
+        nasm_free(file);
     }
     hash_free(&cv8_state.file_hash);
 
@@ -398,8 +398,7 @@ static struct source_file *register_file(const char *filename)
 
         fullpath = nasm_realpath(filename);
 
-        file = nasm_zalloc(sizeof(*file));
-
+        nasm_new(file);
         file->filename = filename;
         file->fullname = fullpath;
         file->fullnamelen = strlen(fullpath);
