From c691ec6607db63cd5becddbaa647cb3b4970df03 Mon Sep 17 00:00:00 2001 From: Rangi Date: Tue, 21 Jul 2026 14:31:05 -0400 Subject: [PATCH] Use `std::optional` instead of `long` with a special -1 sentinel for `seekSize` --- include/util.hpp | 2 +- src/asm/actions.cpp | 6 +++--- src/asm/section.cpp | 24 +++++++++++++----------- src/link/output.cpp | 13 +++++++------ src/util.cpp | 10 +++++----- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/include/util.hpp b/include/util.hpp index 1ec2c49d..daabfabc 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -27,7 +27,7 @@ int xfclose(FILE *file); int xclose(int fd); // Measure file size with `fseek` and `ftell` idiom -long seekSize(FILE *file); +std::optional seekSize(FILE *file); // Locale-independent character class functions bool isNewline(int c); diff --git a/src/asm/actions.cpp b/src/asm/actions.cpp index de1ba1ce..7ab356e5 100644 --- a/src/asm/actions.cpp +++ b/src/asm/actions.cpp @@ -151,11 +151,11 @@ std::optional act_ReadFile(std::string const &name, uint32_t maxLen Defer closeFile{[&] { xfclose(file); }}; size_t readSize = maxLen; - if (long fileSize = seekSize(file); fileSize != -1) { + if (std::optional fileSize = seekSize(file); fileSize.has_value()) { // If the file is seekable and shorter than the max length, // just read as many bytes as there are - if (static_cast(fileSize) < readSize) { - readSize = fileSize; + if (*fileSize < readSize) { + readSize = *fileSize; } // LCOV_EXCL_START } else if (errno != ESPIPE) { diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 165b5fb8..e5c6d49f 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -977,13 +977,14 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) { } Defer closeFile{[&] { xfclose(file); }}; - if (long fileSize = seekSize(file); fileSize != -1) { - if (startPos > static_cast(fileSize)) { + if (std::optional fileSize = seekSize(file); fileSize.has_value()) { + if (startPos > *fileSize) { error( - "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)", + "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64 + ")", startPos, name.c_str(), - fileSize + *fileSize ); return false; } @@ -1037,23 +1038,24 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l } Defer closeFile{[&] { xfclose(file); }}; - if (long fileSize = seekSize(file); fileSize != -1) { - if (startPos > static_cast(fileSize)) { + if (std::optional fileSize = seekSize(file); fileSize.has_value()) { + if (startPos > *fileSize) { error( - "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)", + "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64 + ")", startPos, name.c_str(), - fileSize + *fileSize ); return false; - } else if (startPos + length > static_cast(fileSize)) { + } else if (startPos + length > *fileSize) { error( "Specified range in `INCBIN` file \"%s\" is out of bounds (%" PRIu32 " + %" PRIu32 - " > %ld)", + " > %" PRIu64 ")", name.c_str(), startPos, length, - fileSize + *fileSize ); return false; } diff --git a/src/link/output.cpp b/src/link/output.cpp index 6869c300..aca09b44 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -112,23 +113,23 @@ static uint32_t checkOverlaySize() { return 0; } - long overlaySize = seekSize(overlayFile); + std::optional overlaySize = seekSize(overlayFile); - if (overlaySize == -1) { + if (!overlaySize.has_value()) { warnx("Overlay file is not seekable, cannot check if properly formed"); return 0; } - if (overlaySize % BANK_SIZE) { + if (*overlaySize % BANK_SIZE) { warnx("Overlay file does not have a size multiple of 0x4000"); - } else if (options.is32kMode && overlaySize != 0x8000) { + } else if (options.is32kMode && *overlaySize != 0x8000) { warnx("Overlay is not exactly 0x8000 bytes large"); } - if (overlaySize < 0x8000) { + if (*overlaySize < 0x8000) { warnx("Overlay is less than 0x8000 bytes large"); } - return (overlaySize + BANK_SIZE - 1) / BANK_SIZE; + return (*overlaySize + BANK_SIZE - 1) / BANK_SIZE; } // Expand `sections[SECTTYPE_ROMX]` to cover all the overlay banks. diff --git a/src/util.cpp b/src/util.cpp index 27bd3490..5384107a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -25,18 +25,18 @@ int xclose(int fd) { return close(fd); } -long seekSize(FILE *file) { +std::optional seekSize(FILE *file) { if (fseek(file, 0, SEEK_END) != 0) { - return -1; + return std::nullopt; } long size = ftell(file); if (size < 0) { - return -1; + return std::nullopt; } if (fseek(file, 0, SEEK_SET) != 0) { - return -1; + return std::nullopt; } - return size; + return static_cast(size); } bool isNewline(int c) {