Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/libutil/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)));
Expand Down
14 changes: 4 additions & 10 deletions src/libutil/include/nix/util/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <list>
#include <memory>
#include <optional>
#include <utility>

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -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
Loading