Avoid closing stdin/stdout/stderr standard streams

This commit is contained in:
Rangi
2026-07-13 13:02:46 -04:00
parent 8ab6190f96
commit e9fa1e4e94
11 changed files with 42 additions and 22 deletions
+2 -1
View File
@@ -18,6 +18,7 @@
#include "extern/utf8decoder.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "util.hpp" // xfclose
#include "asm/charmap.hpp"
#include "asm/format.hpp"
@@ -147,7 +148,7 @@ std::optional<std::string> act_ReadFile(std::string const &name, uint32_t maxLen
}
return "";
}
Defer closeFile{[&] { fclose(file); }};
Defer closeFile{[&] { xfclose(file); }};
size_t readSize = maxLen;
if (fseek(file, 0, SEEK_END) == 0) {
+1 -5
View File
@@ -405,11 +405,7 @@ void LexerState::setFileAsNextState(std::string const &filePath, bool updateStat
if (fd >= 0) {
// If the file is stdin, or if measuring its size failed, read it in pieces
Defer closeFile{[&] {
if (fd != STDIN_FILENO) {
close(fd);
}
}};
Defer closeFile{[&] { xclose(fd); }};
// Reasonably large buffer size for `read` performance
char buf[8192];
+3 -2
View File
@@ -17,6 +17,7 @@
#include "helpers.hpp" // assume, Defer
#include "linkdefs.hpp"
#include "platform.hpp"
#include "util.hpp" // xfclose
#include "asm/charmap.hpp"
#include "asm/fstack.hpp"
@@ -210,7 +211,7 @@ void out_WriteObject() {
fatal("Failed to open object file \"%s\": %s", objectFileName, strerror(errno));
// LCOV_EXCL_STOP
}
Defer closeFile{[&] { fclose(file); }};
Defer closeFile{[&] { xfclose(file); }};
// Also write symbols that weren't written above
sym_ForEach(out_RegisterSymbol);
@@ -389,7 +390,7 @@ void out_WriteState(std::string name, std::vector<StateFeature> const &features)
fatal("Failed to open state file \"%s\": %s", name.c_str(), strerror(errno));
// LCOV_EXCL_STOP
}
Defer closeFile{[&] { fclose(file); }};
Defer closeFile{[&] { xfclose(file); }};
static char const *dumpHeadings[NB_STATE_FEATURES] = {
"Numeric constants",
+3 -2
View File
@@ -21,6 +21,7 @@
#include "helpers.hpp"
#include "itertools.hpp" // InsertionOrderedMap
#include "linkdefs.hpp"
#include "util.hpp" // xfclose
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
@@ -974,7 +975,7 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) {
if (!file) {
return fstk_FileError(name, "`INCBIN`");
}
Defer closeFile{[&] { fclose(file); }};
Defer closeFile{[&] { xfclose(file); }};
if (fseek(file, 0, SEEK_END) == 0) {
if (unsigned long fsize = ftell(file);
@@ -1035,7 +1036,7 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l
if (!file) {
return fstk_FileError(name, "`INCBIN`");
}
Defer closeFile{[&] { fclose(file); }};
Defer closeFile{[&] { xfclose(file); }};
if (fseek(file, 0, SEEK_END) == 0) {
if (unsigned long fsize = ftell(file);