From 3d23f5bbb3118f16cee1a6637aead4a17b7c13f6 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Tue, 27 Feb 2024 10:12:07 -0500 Subject: [PATCH] Use `std::vector` for reading object file sections --- src/link/object.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/link/object.cpp b/src/link/object.cpp index 21a76bdd..5ddf13f7 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -171,7 +171,7 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint tryReadlong(parentID, file, "%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i); - fileNodes[i].parent = parentID == (uint32_t)-1 ? NULL : &fileNodes[parentID]; + fileNodes[i].parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : NULL; tryReadlong(fileNodes[i].lineNo, file, "%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i); tryGetc(enum FileStackNodeType, fileNodes[i].type, file, @@ -289,10 +289,10 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha * Sets a patch's pcSection from its pcSectionID. * @param patch The struct to fix */ -static void linkPatchToPCSect(struct Patch *patch, struct Section *fileSections[]) +static void linkPatchToPCSect(struct Patch *patch, std::vector const &fileSections) { - patch->pcSection = patch->pcSectionID == (uint32_t)-1 ? NULL - : fileSections[patch->pcSectionID]; + patch->pcSection = patch->pcSectionID != (uint32_t)-1 ? fileSections[patch->pcSectionID] + : NULL; } /* @@ -534,8 +534,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) symbolLists.push_front({ .nbSymbols = nbSymbols, .symbolList = fileSymbols }); - uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1, - sizeof(*nbSymPerSect)); + std::vector nbSymPerSect(nbSections, 0); verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols); for (uint32_t i = 0; i < nbSymbols; i++) { @@ -554,8 +553,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) } // This file's sections, stored in a table to link symbols to them - struct Section **fileSections = - (struct Section **)malloc(sizeof(*fileSections) * (nbSections ? nbSections : 1)); + std::vector fileSections(nbSections, NULL); verbosePrint("Reading %" PRIu32 " sections...\n", nbSections); for (uint32_t i = 0; i < nbSections; i++) { @@ -580,8 +578,6 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) sect_AddSection(fileSections[i]); } - free(nbSymPerSect); - // Give patches' PC section pointers to their sections for (uint32_t i = 0; i < nbSections; i++) { if (sect_HasData(fileSections[i]->type)) { @@ -624,7 +620,6 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) assertion.fileSymbols = fileSymbols; } - free(fileSections); fclose(file); }