From bd8d30f23b2f968da052092f59a7298fdc073c4e Mon Sep 17 00:00:00 2001 From: Frédéric Desbiens Date: Sat, 8 Aug 2026 08:01:38 -0400 Subject: Fixed code_buffer leak in the module converter utilities (#581) The host-side module converter utilities allocated code_buffer inside the loop over the ELF code sections and never released it, so every code section in the input ELF leaked one buffer. The malloc() result was also unchecked, so a failed allocation passed a null pointer on to elf_object_read() and crashed the tool. Release the buffer at the end of each iteration and report a clean failure with exit code 5 when the allocation does not succeed. Verified with AddressSanitizer on a two-code-section input: 128 bytes leaked in 2 allocations before, none after, with byte-identical output. Fixes https://github.com/eclipse-threadx/threadx/issues/571 Assisted-by: Claude Code (Opus 5) --- .../module_manager/utilities/module_to_binary.c | 27 ++++++++++++++++++++++ .../module_manager/utilities/module_to_c_array.c | 27 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) (limited to 'common_modules') diff --git a/common_modules/module_manager/utilities/module_to_binary.c b/common_modules/module_manager/utilities/module_to_binary.c index bb4f294d..66657fcc 100644 --- a/common_modules/module_manager/utilities/module_to_binary.c +++ b/common_modules/module_manager/utilities/module_to_binary.c @@ -9,11 +9,20 @@ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ +// Some portions generated by Claude Code (Opus 5) + #include #include #include +/* MISRA C:2012 Rule 21.3 and Dir 4.12 deviation: dynamic memory is used in this + file. This is a host-side conversion utility executed on the development + machine, not ThreadX runtime code, and the ELF areas being read have sizes + that are only known at run time. Every allocation result is checked for NULL + and every buffer is released before its pointer is reused. */ + + /* Define the file handles. */ FILE *source_file; @@ -310,6 +319,20 @@ unsigned char zero_value; /* Now allocate memory for the code section. */ code_buffer = malloc(code_section_array[i].code_section_size); + /* Determine if the memory allocation was successful. */ + if (code_buffer == NULL) + { + + /* Print an error message out. */ + printf("**** Error: memory allocation failed for code section **** \n"); + + /* Close files. */ + fclose(source_file); + fclose(binary_file); + + return(5); + } + /* Read in the code area. */ j = code_section_array[i].code_section_index; elf_object_read(section_header[j].elf_section_header_offset, code_buffer, code_section_array[i].code_section_size); @@ -333,6 +356,10 @@ unsigned char zero_value; /* Move index into buffer. */ j++; } + + /* Release the code section buffer before the next section is read. */ + free(code_buffer); + code_buffer = NULL; } /* Close files. */ diff --git a/common_modules/module_manager/utilities/module_to_c_array.c b/common_modules/module_manager/utilities/module_to_c_array.c index d3472416..72461e27 100644 --- a/common_modules/module_manager/utilities/module_to_c_array.c +++ b/common_modules/module_manager/utilities/module_to_c_array.c @@ -9,11 +9,20 @@ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ +// Some portions generated by Claude Code (Opus 5) + #include #include #include +/* MISRA C:2012 Rule 21.3 and Dir 4.12 deviation: dynamic memory is used in this + file. This is a host-side conversion utility executed on the development + machine, not ThreadX runtime code, and the ELF areas being read have sizes + that are only known at run time. Every allocation result is checked for NULL + and every buffer is released before its pointer is reused. */ + + /* Define the file handles. */ FILE *source_file; @@ -342,6 +351,20 @@ CODE_SECTION_ENTRY code_section_temp; /* Now allocate memory for the code section. */ code_buffer = malloc(code_section_array[i].code_section_size); + /* Determine if the memory allocation was successful. */ + if (code_buffer == NULL) + { + + /* Print an error message out. */ + printf("**** Error: memory allocation failed for code section **** \n"); + + /* Close files. */ + fclose(source_file); + fclose(array_file); + + return(5); + } + /* Read in the code area. */ j = code_section_array[i].code_section_index; elf_object_read(section_header[j].elf_section_header_offset, code_buffer, code_section_array[i].code_section_size); @@ -390,6 +413,10 @@ CODE_SECTION_ENTRY code_section_temp; /* Move index into buffer. */ j++; } + + /* Release the code section buffer before the next section is read. */ + free(code_buffer); + code_buffer = NULL; } /* Finally, finish the C array containing the module code. */ -- cgit v1.3.1