From 8fcb759694603e0c45ddfa634a4bed00fd789a92 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 26 Nov 2024 02:42:51 +0900 Subject: [PATCH] fix: check std::source_location::file_name result encountered an error with nullptr from std::source_location::file_name --- include/toml11/compat.hpp | 42 +++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/include/toml11/compat.hpp b/include/toml11/compat.hpp index 3308a32..c954bf2 100644 --- a/include/toml11/compat.hpp +++ b/include/toml11/compat.hpp @@ -434,9 +434,19 @@ using source_location = std::source_location; inline std::string to_string(const source_location& loc) { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(loc.file_name()); + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } } + } // cxx } // toml #elif defined(TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION) @@ -449,9 +459,19 @@ using source_location = std::experimental::source_location; inline std::string to_string(const source_location& loc) { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(loc.file_name()); + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } } + } // cxx } // toml #elif defined(TOML11_HAS_BUILTIN_FILE_LINE) @@ -483,9 +503,19 @@ struct source_location inline std::string to_string(const source_location& loc) { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(loc.file_name()); + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } } + } // cxx } // toml #else // no builtin