From ba00cf568442b71aafa1670498e1135264e73502 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sat, 2 Mar 2024 04:47:02 -0500 Subject: [PATCH] Use `std::string` for section names --- include/asm/section.hpp | 2 +- src/asm/output.cpp | 5 ++--- src/asm/parser.y | 2 +- src/asm/section.cpp | 18 +++++++----------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/include/asm/section.hpp b/include/asm/section.hpp index 4862cac1..a9758562 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -26,7 +26,7 @@ struct Patch { }; struct Section { - char *name; + std::string name; enum SectionType type; enum SectionModifier modifier; FileStackNode const *src; // Where the section was defined diff --git a/src/asm/output.cpp b/src/asm/output.cpp index d5df428f..16bc953c 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -25,7 +25,6 @@ #include "error.hpp" #include "linkdefs.hpp" -#include "platform.hpp" // strdup struct Assertion { Patch patch; @@ -95,7 +94,7 @@ static uint32_t getSectIDIfAny(Section *sect) return it - sectionList.begin(); } - fatalerror("Unknown section '%s'\n", sect->name); + fatalerror("Unknown section '%s'\n", sect->name.c_str()); } // Write a patch to a file @@ -115,7 +114,7 @@ static void writepatch(Patch const &patch, FILE *f) // Write a section to a file static void writesection(Section const §, FILE *f) { - putstring(sect.name, f); + putstring(sect.name.c_str(), f); putlong(sect.size, f); diff --git a/src/asm/parser.y b/src/asm/parser.y index 2c2f8771..ea0a8b10 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -1329,7 +1329,7 @@ string : T_STRING fatalerror("\"%s\" does not belong to any section\n", sym->name); // Section names are capped by rgbasm's maximum string length, // so this currently can't overflow. - strcpy($$, section->name); + strcpy($$, section->name.c_str()); } ; diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 770af789..66f4ad84 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -23,7 +23,6 @@ #include "error.hpp" #include "linkdefs.hpp" -#include "platform.hpp" // strdup uint8_t fillByte; @@ -71,7 +70,7 @@ attr_(warn_unused_result) static bool checkcodesection() return true; error("Section '%s' cannot contain code or data (not ROM0 or ROMX)\n", - currentSection->name); + currentSection->name.c_str()); return false; } @@ -84,7 +83,7 @@ attr_(warn_unused_result) static bool checkSectionSize(Section const *sect, uint return true; error("Section '%s' grew too big (max size = 0x%" PRIX32 - " bytes, reached 0x%" PRIX32 ").\n", sect->name, maxSize, size); + " bytes, reached 0x%" PRIX32 ").\n", sect->name.c_str(), maxSize, size); return false; } @@ -113,7 +112,7 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size) Section *sect_FindSectionByName(char const *name) { for (Section § : sectionList) { - if (strcmp(name, sect.name) == 0) + if (sect.name == name) return § } return nullptr; @@ -258,7 +257,7 @@ static void mergeSections(Section *sect, enum SectionType type, uint32_t org, ui if (nbSectErrors) fatalerror("Cannot create section \"%s\" (%u error%s)\n", - sect->name, nbSectErrors, nbSectErrors == 1 ? "" : "s"); + sect->name.c_str(), nbSectErrors, nbSectErrors == 1 ? "" : "s"); } #undef fail @@ -270,10 +269,7 @@ static Section *createSection(char const *name, enum SectionType type, uint32_t // Add the new section to the list (order doesn't matter) Section § = sectionList.emplace_front(); - sect.name = strdup(name); - if (sect.name == nullptr) - fatalerror("Not enough memory for section name: %s\n", strerror(errno)); - + sect.name = name; sect.type = type; sect.modifier = mod; sect.src = fstk_GetFileStack(); @@ -387,7 +383,7 @@ bool Section::isSizeKnown() const // Any section on the stack is still growing for (SectionStackEntry &entry : sectionStack) { - if (entry.section && !strcmp(name, entry.section->name)) + if (entry.section && entry.section->name == name) return false; } @@ -402,7 +398,7 @@ void sect_NewSection(char const *name, enum SectionType type, uint32_t org, fatalerror("Cannot change the section within a `LOAD` block\n"); for (SectionStackEntry &entry : sectionStack) { - if (entry.section && !strcmp(name, entry.section->name)) + if (entry.section && entry.section->name == name) fatalerror("Section '%s' is already on the stack\n", name); }