From cf08fed0670bd53f0093bcdc45eba2271fc99a9e Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 28 Feb 2024 22:57:18 -0500 Subject: [PATCH] Use `std::vector` for SDCC object section data --- src/link/sdas_obj.cpp | 58 ++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index fd2407fb..abe050d9 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -220,12 +220,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector fileSections; - size_t nbBytes = 0; // How many bytes are in `data`, including the ADDR_SIZE "header" bytes - size_t dataCapacity = 16 + ADDR_SIZE; // SDCC object files usually contain 16 bytes per T line - uint8_t *data = (uint8_t *)malloc(sizeof(*data) * dataCapacity); - - if (!data) - fatal(where, lineNo, "Failed to alloc data buffer: %s", strerror(errno)); + std::vector data; for (;;) { lineType = nextLine(line, lineNo, where, file); @@ -395,30 +390,21 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector= nbBytes) + if (offset >= data.size()) fatal(where, lineNo, "Relocation index is out of bounds (%" PRIu16 " >= %zu)", - offset, nbBytes); + offset, data.size()); getToken(NULL, "Incomplete relocation"); uint16_t idx = parseByte(where, lineNo, token, numberType); @@ -521,10 +507,10 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector %zu)", - nbBaseBytes, nbBytes - offset); + nbBaseBytes, data.size() - offset); for (uint8_t i = 0; i < nbBaseBytes; ++i) baseValue = baseValue | data[offset + i] << (8 * i); @@ -661,16 +647,16 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector writtenOfs); - if (*writeIndex + (nbBytes - writtenOfs) > section->size) + if (writtenOfs != data.size()) { + assert(data.size() > writtenOfs); + if (*writeIndex + (data.size() - writtenOfs) > section->size) fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")", - section->name.c_str(), *writeIndex + (nbBytes - writtenOfs), section->size); - memcpy(§ion->data[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); - *writeIndex += nbBytes - writtenOfs; + section->name.c_str(), *writeIndex + (data.size() - writtenOfs), section->size); + memcpy(§ion->data[*writeIndex], &data[writtenOfs], data.size() - writtenOfs); + *writeIndex += data.size() - writtenOfs; } - nbBytes = 0; // Do not allow two R lines to refer to the same T line + data.clear(); // Do not allow two R lines to refer to the same T line break; } @@ -681,7 +667,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector