Skip to content

Commit a0a8d05

Browse files
mhaynieclaude
andcommitted
fix: Handle null file_name/function_name in source_location stream operator
Prevents undefined behavior when streaming a default-constructed source_location with null pointers. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3a4a989 commit a0a8d05

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

cpp/include/mh/source_location.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ namespace mh
5252
template<typename CharT, typename Traits>
5353
inline std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const mh::source_location& location)
5454
{
55-
return os << location.file_name() << '(' << location.line() << "):" << location.function_name();
55+
const char* file = location.file_name();
56+
const char* func = location.function_name();
57+
return os << (file ? file : "(unknown)") << '(' << location.line() << "):" << (func ? func : "(unknown)");
5658
}
5759
}
5860

0 commit comments

Comments
 (0)