diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2022-07-11 19:59:54 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-07-12 23:11:35 +0200 |
commit | 6eecc65787abd1dcbbff157e0c3be73b03a4c225 (patch) | |
tree | 3b38f5426eb14aaf6e27ee611d9cf10cd7b62b78 /AK/Format.h | |
parent | 52d017c61121dd6fcfbf31c274a83da126271252 (diff) | |
download | serenity-6eecc65787abd1dcbbff157e0c3be73b03a4c225.zip |
AK: Explicitly calculate length of char* when printing
This moves out the calculation of the char* out to the formatter.
Additionally, we now print (null) when a null pointer is passed.
Diffstat (limited to 'AK/Format.h')
-rw-r--r-- | AK/Format.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/AK/Format.h b/AK/Format.h index ddd612968d..b9b69ca15b 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -395,6 +395,8 @@ template<> struct Formatter<Bytes> : Formatter<ReadonlyBytes> { }; +// FIXME: Printing raw char pointers is inherently dangerous. Remove this and +// its users and prefer StringView over it. template<> struct Formatter<char const*> : Formatter<StringView> { ErrorOr<void> format(FormatBuilder& builder, char const* value) @@ -403,7 +405,8 @@ struct Formatter<char const*> : Formatter<StringView> { Formatter<FlatPtr> formatter { *this }; return formatter.format(builder, reinterpret_cast<FlatPtr>(value)); } - return Formatter<StringView>::format(builder, value); + + return Formatter<StringView>::format(builder, value != nullptr ? StringView { value, __builtin_strlen(value) } : "(null)"sv); } }; template<> |