Normalize NULL char* to <null> in Linux logging (#41271)
Feng Wang committed
Aug 7, 2026 at 10:13 UTC
ebe39330d4ab626d88624ab2154440c508b56309
1 file changed
+20
-1
src/linux/init/common.h
+20
-1
@@ -45,6 +45,7 @@ Abstract:
45
#include <poll.h>
46
#include <utility>
47
#include <format>
48
+#include <type_traits>
49
#include <new>
50
#include <thread>
51
#include <vector>
@@ -98,10 +99,28 @@ extern struct sigaction g_SavedSignalActions[_NSIG];
99
} \
100
}
101
102
+template <typename T>
103
+T&& NormalizeLogArgument(T&& Argument)
104
+{
105
+ using ArgumentType = std::remove_cv_t<std::remove_reference_t<T>>;
106
+
107
+ if constexpr (std::is_same_v<ArgumentType, char*> || std::is_same_v<ArgumentType, const char*>)
108
+ {
109
+ if (Argument == nullptr)
110
+ {
111
+ static char NullString[] = "<null>";
112
+ static ArgumentType NullArgument = NullString;
113
+ return static_cast<T&&>(NullArgument);
114
+ }
115
+ }
116
+
117
+ return std::forward<T>(Argument);
118
+}
119
+
120
template <typename... Args>
121
auto LogImpl(int fd, const std::format_string<Args...>& format, Args&&... args)
122
{
104
- auto logline = std::format(format, std::forward<Args>(args)...);
123
+ auto logline = std::format(format, NormalizeLogArgument(std::forward<Args>(args))...);
124
if (logline.empty())
125
{
126
return;