Fix all memory leaks in RGBLINK

At least all that were reported in the tests.
Partial fix for #709, that only leaves RGBASM to be fixed... oh boy!
This commit is contained in:
ISSOtm
2021-05-03 12:43:41 +02:00
parent 8bbafb7200
commit 60b85298a9
4 changed files with 29 additions and 33 deletions

View File

@@ -21,10 +21,7 @@ struct Assertion {
struct Patch patch; struct Patch patch;
// enum AssertionType type; The `patch`'s field is instead re-used // enum AssertionType type; The `patch`'s field is instead re-used
char *message; char *message;
/* // This would be redundant with `.section->fileSymbols`... but `section` is sometimes NULL!
* This would be redundant with `.section->fileSymbols`... but
* `section` is sometimes NULL!
*/
struct Symbol **fileSymbols; struct Symbol **fileSymbols;
struct Assertion *next; struct Assertion *next;

View File

@@ -28,18 +28,6 @@
#include "extern/getopt.h" #include "extern/getopt.h"
#include "version.h" #include "version.h"
#ifdef __clang__
#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
#define __SANITIZE_ADDRESS__
#endif /* __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) */
#endif /* __clang__ */
#ifdef __SANITIZE_ADDRESS__
// There are known, non-trivial to fix leaks. We would still like to have `make develop'
// detect memory corruption, though.
const char *__asan_default_options(void) { return "detect_leaks=0"; }
#endif
bool isDmgMode; /* -d */ bool isDmgMode; /* -d */
char *linkerScriptName; /* -l */ char *linkerScriptName; /* -l */
char const *mapFileName; /* -m */ char const *mapFileName; /* -m */

View File

@@ -640,19 +640,33 @@ void obj_Setup(unsigned int nbFiles)
nodes = malloc(sizeof(*nodes) * nbFiles); nodes = malloc(sizeof(*nodes) * nbFiles);
} }
static void freeNode(struct FileStackNode *node)
{
if (node->type == NODE_REPT)
free(node->iters);
else
free(node->name);
}
static void freeSection(struct Section *section, void *arg) static void freeSection(struct Section *section, void *arg)
{ {
(void)arg; (void)arg;
free(section->name); do {
if (sect_HasData(section->type)) { struct Section *next = section->nextu;
free(section->data);
for (uint32_t i = 0; i < section->nbPatches; i++) free(section->name);
free(section->patches[i].rpnExpression); if (sect_HasData(section->type)) {
free(section->patches); free(section->data);
} for (uint32_t i = 0; i < section->nbPatches; i++)
free(section->symbols); free(section->patches[i].rpnExpression);
free(section); free(section->patches);
}
free(section->symbols);
free(section);
section = next;
} while (section);
} }
static void freeSymbol(struct Symbol *symbol) static void freeSymbol(struct Symbol *symbol)
@@ -665,8 +679,7 @@ void obj_Cleanup(void)
{ {
for (unsigned int i = 0; i < nbObjFiles; i++) { for (unsigned int i = 0; i < nbObjFiles; i++) {
for (uint32_t j = 0; j < nodes[i].nbNodes; j++) { for (uint32_t j = 0; j < nodes[i].nbNodes; j++) {
if (nodes[i].nodes[j].type == NODE_REPT) freeNode(&nodes[i].nodes[j]);
free(nodes[i].nodes[j].iters);
} }
free(nodes[i].nodes); free(nodes[i].nodes);
} }
@@ -677,16 +690,12 @@ void obj_Cleanup(void)
sect_ForEach(freeSection, NULL); sect_ForEach(freeSection, NULL);
sect_CleanupSections(); sect_CleanupSections();
struct SymbolList *list = symbolLists; for (struct SymbolList *list = symbolLists, *next; list; list = next) {
next = list->next;
while (list) {
for (size_t i = 0; i < list->nbSymbols; i++) for (size_t i = 0; i < list->nbSymbols; i++)
freeSymbol(list->symbolList[i]); freeSymbol(list->symbolList[i]);
free(list->symbolList); free(list->symbolList);
struct SymbolList *next = list->next;
free(list); free(list);
list = next;
} }
} }

View File

@@ -478,6 +478,8 @@ void patch_CheckAssertions(struct Assertion *assert)
} }
struct Assertion *next = assert->next; struct Assertion *next = assert->next;
free(assert->patch.rpnExpression);
free(assert->message);
free(assert); free(assert);
assert = next; assert = next;
} }