Use std::optional<uint64_t> instead of long with a special -1 sentinel for seekSize

This commit is contained in:
Rangi
2026-07-21 14:31:05 -04:00
parent c3073f4318
commit c691ec6607
5 changed files with 29 additions and 26 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ int xfclose(FILE *file);
int xclose(int fd); int xclose(int fd);
// Measure file size with `fseek` and `ftell` idiom // Measure file size with `fseek` and `ftell` idiom
long seekSize(FILE *file); std::optional<uint64_t> seekSize(FILE *file);
// Locale-independent character class functions // Locale-independent character class functions
bool isNewline(int c); bool isNewline(int c);
+3 -3
View File
@@ -151,11 +151,11 @@ std::optional<std::string> act_ReadFile(std::string const &name, uint32_t maxLen
Defer closeFile{[&] { xfclose(file); }}; Defer closeFile{[&] { xfclose(file); }};
size_t readSize = maxLen; size_t readSize = maxLen;
if (long fileSize = seekSize(file); fileSize != -1) { if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
// If the file is seekable and shorter than the max length, // If the file is seekable and shorter than the max length,
// just read as many bytes as there are // just read as many bytes as there are
if (static_cast<size_t>(fileSize) < readSize) { if (*fileSize < readSize) {
readSize = fileSize; readSize = *fileSize;
} }
// LCOV_EXCL_START // LCOV_EXCL_START
} else if (errno != ESPIPE) { } else if (errno != ESPIPE) {
+13 -11
View File
@@ -977,13 +977,14 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) {
} }
Defer closeFile{[&] { xfclose(file); }}; Defer closeFile{[&] { xfclose(file); }};
if (long fileSize = seekSize(file); fileSize != -1) { if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
if (startPos > static_cast<size_t>(fileSize)) { if (startPos > *fileSize) {
error( error(
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)", "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64
")",
startPos, startPos,
name.c_str(), name.c_str(),
fileSize *fileSize
); );
return false; return false;
} }
@@ -1037,23 +1038,24 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l
} }
Defer closeFile{[&] { xfclose(file); }}; Defer closeFile{[&] { xfclose(file); }};
if (long fileSize = seekSize(file); fileSize != -1) { if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
if (startPos > static_cast<size_t>(fileSize)) { if (startPos > *fileSize) {
error( error(
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)", "Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64
")",
startPos, startPos,
name.c_str(), name.c_str(),
fileSize *fileSize
); );
return false; return false;
} else if (startPos + length > static_cast<size_t>(fileSize)) { } else if (startPos + length > *fileSize) {
error( error(
"Specified range in `INCBIN` file \"%s\" is out of bounds (%" PRIu32 " + %" PRIu32 "Specified range in `INCBIN` file \"%s\" is out of bounds (%" PRIu32 " + %" PRIu32
" > %ld)", " > %" PRIu64 ")",
name.c_str(), name.c_str(),
startPos, startPos,
length, length,
fileSize *fileSize
); );
return false; return false;
} }
+7 -6
View File
@@ -6,6 +6,7 @@
#include <deque> #include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <optional>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -112,23 +113,23 @@ static uint32_t checkOverlaySize() {
return 0; return 0;
} }
long overlaySize = seekSize(overlayFile); std::optional<uint64_t> overlaySize = seekSize(overlayFile);
if (overlaySize == -1) { if (!overlaySize.has_value()) {
warnx("Overlay file is not seekable, cannot check if properly formed"); warnx("Overlay file is not seekable, cannot check if properly formed");
return 0; return 0;
} }
if (overlaySize % BANK_SIZE) { if (*overlaySize % BANK_SIZE) {
warnx("Overlay file does not have a size multiple of 0x4000"); 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"); warnx("Overlay is not exactly 0x8000 bytes large");
} }
if (overlaySize < 0x8000) { if (*overlaySize < 0x8000) {
warnx("Overlay is less than 0x8000 bytes large"); 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. // Expand `sections[SECTTYPE_ROMX]` to cover all the overlay banks.
+5 -5
View File
@@ -25,18 +25,18 @@ int xclose(int fd) {
return close(fd); return close(fd);
} }
long seekSize(FILE *file) { std::optional<uint64_t> seekSize(FILE *file) {
if (fseek(file, 0, SEEK_END) != 0) { if (fseek(file, 0, SEEK_END) != 0) {
return -1; return std::nullopt;
} }
long size = ftell(file); long size = ftell(file);
if (size < 0) { if (size < 0) {
return -1; return std::nullopt;
} }
if (fseek(file, 0, SEEK_SET) != 0) { if (fseek(file, 0, SEEK_SET) != 0) {
return -1; return std::nullopt;
} }
return size; return static_cast<uint64_t>(size);
} }
bool isNewline(int c) { bool isNewline(int c) {