mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 03:28:25 +03:00
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.
This commit is contained in:
parent
52d017c611
commit
6eecc65787
Notes:
sideshowbarker
2024-07-18 04:38:32 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/6eecc65787 Pull-request: https://github.com/SerenityOS/serenity/pull/14555 Reviewed-by: https://github.com/Dexesttp ✅ Reviewed-by: https://github.com/kleinesfilmroellchen
@ -395,6 +395,8 @@ template<>
|
|||||||
struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
|
struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// FIXME: Printing raw char pointers is inherently dangerous. Remove this and
|
||||||
|
// its users and prefer StringView over it.
|
||||||
template<>
|
template<>
|
||||||
struct Formatter<char const*> : Formatter<StringView> {
|
struct Formatter<char const*> : Formatter<StringView> {
|
||||||
ErrorOr<void> format(FormatBuilder& builder, char const* value)
|
ErrorOr<void> format(FormatBuilder& builder, char const* value)
|
||||||
@ -403,7 +405,8 @@ struct Formatter<char const*> : Formatter<StringView> {
|
|||||||
Formatter<FlatPtr> formatter { *this };
|
Formatter<FlatPtr> formatter { *this };
|
||||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value));
|
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<>
|
template<>
|
||||||
|
@ -275,8 +275,12 @@ void ArgsParser::print_usage_markdown(FILE* file, char const* argv0)
|
|||||||
for (auto& opt : m_options) {
|
for (auto& opt : m_options) {
|
||||||
if (opt.hide_mode != OptionHideMode::None)
|
if (opt.hide_mode != OptionHideMode::None)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// FIXME: We allow opt.value_name to be empty even if the option
|
||||||
|
// requires an argument. This should be disallowed as it will
|
||||||
|
// currently display a blank name after the option.
|
||||||
if (opt.requires_argument)
|
if (opt.requires_argument)
|
||||||
out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
|
out(file, " [{} {}]", opt.name_for_display(), opt.value_name ?: "");
|
||||||
else
|
else
|
||||||
out(file, " [{}]", opt.name_for_display());
|
out(file, " [{}]", opt.name_for_display());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user