Fix RGBFIX --output with a pre-existing or same-as-input file (#2145)

This commit is contained in:
Rangi authored and GitHub committed 2026-09-27 01:31:53 +02:00
1 parent f5f503c7b1
commit 321ad497fa
3 files changed
+48 -4

No files matched your search

+19 -2
View File
@@ -381,6 +381,24 @@ static void
if (options.padValue == UNSPECIFIED) {
rom0Len = headerSize;
}
} else if (output != STDOUT_FILENO) {
// Truncate a pre-existing output file before writing to it
if (ftruncate(output, 0) != 0) {
// LCOV_EXCL_START
int errnum = errno;
if (errnum == EINVAL) {
// We do not support FIFOs or symlinks
if (struct stat stat; fstat(output, &stat) == 0 && !S_ISREG(stat.st_mode)) {
error(
"\"%s\" is not a regular file, and thus cannot be modified in-place", name
);
return;
}
}
error("Failed to truncate \"%s\": %s", name, strerror(errnum));
return;
// LCOV_EXCL_STOP
}
}
writeLen = writeBytes(output, rom0, rom0Len);
@@ -498,8 +516,7 @@ bool fix_ProcessFile(char const *name, char const *outputName) {
error("Failed to open \"%s\" for reading+writing: %s", name, strerror(errno));
} else {
Defer closeInput{[&] { xclose(input); }};
struct stat stat;
if (fstat(input, &stat) == -1) {
if (struct stat stat; fstat(input, &stat) == -1) {
error("Failed to stat \"%s\": %s", name, strerror(errno)); // LCOV_EXCL_LINE
} else if (!S_ISREG(stat.st_mode)) { // We do not support FIFOs or symlinks
// LCOV_EXCL_START