From 04e3a904c21ec6ca7adfa248dbf42e1f841d4dc5 Mon Sep 17 00:00:00 2001 From: Rangi Date: Sat, 8 Nov 2025 12:06:16 -0500 Subject: [PATCH] Avoid calling `style_Set/Reset` before `strerror(errno)`, since they may call `isatty` which can change `errno` Fixes #1857 --- include/verbosity.hpp | 10 +++++----- src/cli.cpp | 3 ++- src/verbosity.cpp | 9 +++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/verbosity.hpp b/include/verbosity.hpp index f7658880..e2838769 100644 --- a/include/verbosity.hpp +++ b/include/verbosity.hpp @@ -3,17 +3,14 @@ #ifndef RGBDS_VERBOSITY_HPP #define RGBDS_VERBOSITY_HPP +#include #include -#include "style.hpp" - // This macro does not evaluate its arguments unless the condition is true. #define verbosePrint(level, ...) \ do { \ if (checkVerbosity(level)) { \ - style_Set(stderr, STYLE_MAGENTA, false); \ - fprintf(stderr, __VA_ARGS__); \ - style_Reset(stderr); \ + printVerbosely(__VA_ARGS__); \ } \ } while (0) @@ -30,6 +27,9 @@ enum Verbosity { void incrementVerbosity(); bool checkVerbosity(Verbosity level); +[[gnu::format(printf, 1, 2)]] +void printVerbosely(char const *fmt, ...); + void printVVVVVVerbosity(); #endif // RGBDS_VERBOSITY_HPP diff --git a/src/cli.cpp b/src/cli.cpp index e920aa07..4e8538b8 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -21,10 +21,11 @@ static std::vector std::filebuf file; if (!file.open(path, std::ios_base::in)) { + int errnum = errno; style_Set(stderr, STYLE_RED, true); fputs("FATAL: ", stderr); style_Reset(stderr); - fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errno)); + fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errnum)); usage.printAndExit(1); } diff --git a/src/verbosity.cpp b/src/verbosity.cpp index 4204527b..a2e4081c 100644 --- a/src/verbosity.cpp +++ b/src/verbosity.cpp @@ -24,6 +24,15 @@ void incrementVerbosity() { } } +void printVerbosely(char const *fmt, ...) { + va_list args; + style_Set(stderr, STYLE_MAGENTA, false); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + style_Reset(stderr); +} + void printVVVVVVerbosity() { if (!checkVerbosity(VERB_VVVVVV)) { return;