[PATCH] archival: fix memory leaks in write_status_file() in dpkg.c

Anton Moryakov via busybox <[email protected]> Thu, 21 May 2026 23:04:59 +0300
Newsgroups gmane.linux.busybox
Message-ID <[email protected]>
Static analysis detected memory leaks in the write_status_file() function.
The read_package_field() function allocates memory for field_name and field_value
using xstrndup(), but these pointers were not freed in the processing loop,
causing a leak on each iteration.

Changes:
- Add free(field_name) and free(field_value) calls at the end of the loop
  in write_status_file() to properly release allocated memory.
- This aligns the memory management in this section with the pattern already
  used elsewhere in the same function (e.g., near fill_package_struct_cleanup).

Fixes memory leaks reported at lines 888 and 906.

Signed-off-by: Anton Moryakov <[email protected]>
---
 archival/dpkg.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/archival/dpkg.c b/archival/dpkg.c
index eda5ec7eb..ebf13c605 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -894,6 +894,8 @@ static void write_status_file(deb_file_t **deb_file)
 						) {
 							fprintf(new_status_file, "%s: %s\n", field_name, field_value);
 						}
+						free(field_name);
+						free(field_value);
 					}
 					write_flag = TRUE;
 					fputs("\n", new_status_file);
@@ -913,6 +915,8 @@ static void write_status_file(deb_file_t **deb_file)
 						} else {
 							fprintf(new_status_file, "%s: %s\n", field_name, field_value);
 						}
+						free(field_name);
+						free(field_value);
 					}
 					write_flag = TRUE;
 					fputs("\n", new_status_file);
-- 
2.39.2