[Lldb-commits] [clang] [clang-tools-extra] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)
Yanzuo Liu via lldb-commits
lldb-commits at lists.llvm.org
Thu Aug 22 18:10:50 PDT 2024
================
@@ -74,16 +75,31 @@ class formatv_object_base {
static std::pair<ReplacementItem, StringRef>
splitLiteralAndReplacement(StringRef Fmt);
- formatv_object_base(StringRef Fmt,
+ formatv_object_base(StringRef Fmt, bool ValidateNumArgs,
ArrayRef<support::detail::format_adapter *> Adapters)
- : Fmt(Fmt), Adapters(Adapters) {}
+ : Fmt(Fmt), ValidateNumArgs(ValidateNumArgs), Adapters(Adapters) {}
formatv_object_base(formatv_object_base const &rhs) = delete;
formatv_object_base(formatv_object_base &&rhs) = default;
public:
void format(raw_ostream &S) const {
- for (auto &R : parseFormatString(Fmt)) {
+ const auto [Replacements, NumExpectedParams] = parseFormatString(Fmt);
+ // Fail formatv() call if the number of replacement parameters provided
+ // does not match the expected number after parsing the format string.
+ // Assert in debug builds.
+ if (ValidateNumArgs && NumExpectedParams != Adapters.size()) {
+ errs() << "Invalid format() in formatv: " << Fmt << "\n";
+ assert(0 &&
+ "Mismatch between replacement parameters expected and provided");
+
+ S << "formatv() error: " << NumExpectedParams
+ << " replacement parameters expected, but " << Adapters.size()
+ << " provided";
----------------
zwuis wrote:
Do we need to add a `\n` here?
https://github.com/llvm/llvm-project/pull/105745
More information about the lldb-commits
mailing list