[clang] [clang-tools-extra] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)
Rahul Joshi via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 22 18:52:36 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";
----------------
jurahul wrote:
Thanks. This is not ready for review yet. I wanted to run CI locally as I suspect I need to fix more issues before it runs clean. But yes, we can tweak what gets dumped into the stream in error case for release builds.
https://github.com/llvm/llvm-project/pull/105745
More information about the cfe-commits
mailing list