mirror of
https://github.com/gbdev/rgbds.git
synced 2026-09-24 06:37:07 +00:00
Encapsulate fseek+ftell idiom in seekSize
This allows a single check for the return value of `seekSize` to verify all three `fseek`+`ftell`+`fseek` calls.
This commit is contained in:
+3
-4
@@ -18,7 +18,7 @@
|
||||
#include "extern/utf8decoder.hpp"
|
||||
#include "helpers.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
#include "util.hpp" // xfclose
|
||||
#include "util.hpp" // xfclose, seekSize
|
||||
|
||||
#include "asm/charmap.hpp"
|
||||
#include "asm/format.hpp"
|
||||
@@ -151,13 +151,12 @@ std::optional<std::string> act_ReadFile(std::string const &name, uint32_t maxLen
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
size_t readSize = maxLen;
|
||||
if (fseek(file, 0, SEEK_END) == 0) {
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
// If the file is seekable and shorter than the max length,
|
||||
// just read as many bytes as there are
|
||||
if (long fileSize = ftell(file); static_cast<size_t>(fileSize) < readSize) {
|
||||
if (static_cast<size_t>(fileSize) < readSize) {
|
||||
readSize = fileSize;
|
||||
}
|
||||
fseek(file, 0, SEEK_SET);
|
||||
// LCOV_EXCL_START
|
||||
} else if (errno != ESPIPE) {
|
||||
error(
|
||||
|
||||
+11
-13
@@ -21,7 +21,7 @@
|
||||
#include "helpers.hpp"
|
||||
#include "itertools.hpp" // InsertionOrderedMap
|
||||
#include "linkdefs.hpp"
|
||||
#include "util.hpp" // xfclose
|
||||
#include "util.hpp" // xfclose, seekSize
|
||||
|
||||
#include "asm/fstack.hpp"
|
||||
#include "asm/lexer.hpp"
|
||||
@@ -977,14 +977,13 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) {
|
||||
}
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
if (fseek(file, 0, SEEK_END) == 0) {
|
||||
if (unsigned long fsize = ftell(file);
|
||||
startPos > fsize) { // `ftell` cannot fail here, since `fseek` succeeded.
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
if (startPos > static_cast<size_t>(fileSize)) {
|
||||
error(
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%lu)",
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)",
|
||||
startPos,
|
||||
name.c_str(),
|
||||
fsize
|
||||
fileSize
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -1038,24 +1037,23 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l
|
||||
}
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
if (fseek(file, 0, SEEK_END) == 0) {
|
||||
if (unsigned long fsize = ftell(file);
|
||||
startPos > fsize) { // `ftell` cannot fail here, since `fseek` succeeded.
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
if (startPos > static_cast<size_t>(fileSize)) {
|
||||
error(
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%lu)",
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)",
|
||||
startPos,
|
||||
name.c_str(),
|
||||
fsize
|
||||
fileSize
|
||||
);
|
||||
return false;
|
||||
} else if (startPos + length > fsize) {
|
||||
} else if (startPos + length > static_cast<size_t>(fileSize)) {
|
||||
error(
|
||||
"Specified range in `INCBIN` file \"%s\" is out of bounds (%" PRIu32 " + %" PRIu32
|
||||
" > %ld)",
|
||||
name.c_str(),
|
||||
startPos,
|
||||
length,
|
||||
fsize
|
||||
fileSize
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user