ref: d90a7e43028d9859fc0771a11b56379c18b8d7be
parent: 7377a142451ecea51eaa51b6047b5537f8926ef4
author: ISSOtm <[email protected]>
date: Tue Oct 4 09:58:05 EDT 2022
Terminate RGBGFX when opening a file fails `std::filebuf::open`'s result must be checked, though that's not obvious.
--- a/src/gfx/main.cpp
+++ b/src/gfx/main.cpp
@@ -254,7 +254,9 @@
*/
static std::vector<size_t> readAtFile(std::string const &path, std::vector<char> &argPool) {
std::filebuf file;
- file.open(path, std::ios_base::in);
+ if (!file.open(path, std::ios_base::in)) {
+ fatal("Error reading @%s: %s", path.c_str(), strerror(errno));
+ }
static_assert(decltype(file)::traits_type::eof() == EOF,
"isblank(char_traits<...>::eof()) is UB!");
--- a/src/gfx/process.cpp
+++ b/src/gfx/process.cpp
@@ -625,7 +625,9 @@
static void outputPalettes(std::vector<Palette> const &palettes) {
std::filebuf output;
- output.open(options.palettes, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.palettes, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to open \"%s\": %s", options.palettes.c_str(), strerror(errno));
+ }
for (Palette const &palette : palettes) {
for (uint8_t i = 0; i < options.nbColorsPerPal; ++i) {
@@ -751,7 +753,9 @@
std::vector<Palette> const &palettes,
DefaultInitVec<size_t> const &mappings) {
std::filebuf output;
- output.open(options.output, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to open \"%s\": %s", options.output.c_str(), strerror(errno));
+ }
uint64_t remainingTiles = (png.getWidth() / 8) * (png.getHeight() / 8);
if (remainingTiles <= options.trim) {
@@ -783,15 +787,21 @@
std::optional<std::filebuf> tilemapOutput, attrmapOutput, palmapOutput;
if (!options.tilemap.empty()) {
tilemapOutput.emplace();
- tilemapOutput->open(options.tilemap, std::ios_base::out | std::ios_base::binary);
+ if (!tilemapOutput->open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to open \"%s\": %s", options.tilemap.c_str(), strerror(errno));
+ }
}
if (!options.attrmap.empty()) {
attrmapOutput.emplace();
- attrmapOutput->open(options.attrmap, std::ios_base::out | std::ios_base::binary);
+ if (!attrmapOutput->open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to open \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+ }
}
if (!options.palmap.empty()) {
palmapOutput.emplace();
- palmapOutput->open(options.palmap, std::ios_base::out | std::ios_base::binary);
+ if (!palmapOutput->open(options.palmap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to open \"%s\": %s", options.palmap.c_str(), strerror(errno));
+ }
}
uint8_t tileID = 0;
@@ -887,7 +897,9 @@
static void outputTileData(UniqueTiles const &tiles) {
std::filebuf output;
- output.open(options.output, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to create \"%s\": %s", options.output.c_str(), strerror(errno));
+ }
uint16_t tileID = 0;
for (auto iter = tiles.begin(), end = tiles.end() - options.trim; iter != end; ++iter) {
@@ -900,7 +912,9 @@
static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
std::filebuf output;
- output.open(options.tilemap, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to create \"%s\": %s", options.tilemap.c_str(), strerror(errno));
+ }
for (AttrmapEntry const &entry : attrmap) {
output.sputc(entry.tileID); // The tile ID has already been converted
@@ -910,7 +924,9 @@
static void outputAttrmap(DefaultInitVec<AttrmapEntry> const &attrmap,
DefaultInitVec<size_t> const &mappings) {
std::filebuf output;
- output.open(options.attrmap, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to create \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+ }
for (AttrmapEntry const &entry : attrmap) {
uint8_t attr = entry.xFlip << 5 | entry.yFlip << 6;
@@ -923,7 +939,9 @@
static void outputPalmap(DefaultInitVec<AttrmapEntry> const &attrmap,
DefaultInitVec<size_t> const &mappings) {
std::filebuf output;
- output.open(options.attrmap, std::ios_base::out | std::ios_base::binary);
+ if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
+ fatal("Failed to create \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+ }
for (AttrmapEntry const &entry : attrmap) {
output.sputc(entry.getPalID(mappings));
--- a/src/gfx/reverse.cpp
+++ b/src/gfx/reverse.cpp
@@ -147,7 +147,9 @@
};
if (!options.palettes.empty()) {
std::filebuf file;
- file.open(options.palettes, std::ios::in | std::ios::binary);
+ if (!file.open(options.palettes, std::ios::in | std::ios::binary)) {
+ fatal("Failed to open \"%s\": %s", options.palettes.c_str(), strerror(errno));
+ }
palettes.clear();
std::array<uint8_t, sizeof(uint16_t) * 4> buf; // 4 colors
@@ -232,7 +234,9 @@
options.verbosePrint(Options::VERB_LOG_ACT, "Writing image...\n");
std::filebuf pngFile;
- pngFile.open(options.input, std::ios::out | std::ios::binary);
+ if (!pngFile.open(options.input, std::ios::out | std::ios::binary)) {
+ fatal("Failed to create \"%s\": %s", options.input.c_str(), strerror(errno));
+ }
png_structp png = png_create_write_struct(
PNG_LIBPNG_VER_STRING,
const_cast<png_voidp>(static_cast<void const *>(options.input.c_str())), pngError,