1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

Merge pull request #13861 from xokdvium/terminate-for-unreachable

libutil: Try to call std::terminate for panic, use C++20 std::source_location
This commit is contained in:
Jörg Thalheim 2025-08-29 07:15:49 +02:00 committed by GitHub
commit 04d2122de2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View file

@ -6,6 +6,7 @@
#include "nix/util/terminal.hh"
#include "nix/util/position.hh"
#include <cinttypes>
#include <iostream>
#include <optional>
#include "nix/util/serialise.hh"
@ -436,13 +437,19 @@ void panic(std::string_view msg)
writeErr("\n\n" ANSI_RED "terminating due to unexpected unrecoverable internal error: " ANSI_NORMAL);
writeErr(msg);
writeErr("\n");
abort();
std::terminate();
}
void panic(const char * file, int line, const char * func)
void unreachable(std::source_location loc)
{
char buf[512];
int n = snprintf(buf, sizeof(buf), "Unexpected condition in %s at %s:%d", func, file, line);
int n = snprintf(
buf,
sizeof(buf),
"Unexpected condition in %s at %s:%" PRIuLEAST32,
loc.function_name(),
loc.file_name(),
loc.line());
if (n < 0)
panic("Unexpected condition and could not format error message");
panic(std::string_view(buf, std::min(static_cast<int>(sizeof(buf)), n)));

View file

@ -22,6 +22,7 @@
#include <list>
#include <memory>
#include <optional>
#include <utility>
#include <sys/types.h>
#include <sys/stat.h>
@ -299,23 +300,16 @@ using NativeSysError =
void throwExceptionSelfCheck();
/**
* Print a message and abort().
* Print a message and std::terminate().
*/
[[noreturn]]
void panic(std::string_view msg);
/**
* Print a basic error message with source position and abort().
* Use the unreachable() macro to call this.
*/
[[noreturn]]
void panic(const char * file, int line, const char * func);
/**
* Print a basic error message with source position and abort().
* Print a basic error message with source position and std::terminate().
*
* @note: This assumes that the logger is operational
*/
#define unreachable() (::nix::panic(__FILE__, __LINE__, __func__))
[[gnu::noinline, gnu::cold, noreturn]] void unreachable(std::source_location loc = std::source_location::current());
} // namespace nix