Skip to content

Report the existing value when AddCustomContext rejects a duplicate key - #2375

Open
mohitduhan19 wants to merge 2 commits into
google:mainfrom
mohitduhan19:fix-add-custom-context-duplicate-key
Open

mohitduhan19 wants to merge 2 commits into
google:mainfrom
mohitduhan19:fix-add-custom-context-duplicate-key

Conversation

@mohitduhan19

Copy link
Copy Markdown

AddCustomContext moves key and value into std::map::emplace and then prints them in the duplicate-key warning. Two problems with that:

  1. The message reports the wrong value. It prints the rejected value, not the one already stored:
    benchmark::AddCustomContext("foo", "bar");
    benchmark::AddCustomContext("foo", "qux");
    // before: Failed to add custom context "foo" as it already exists with value "qux"
    // after:  Failed to add custom context "foo" as it already exists with value "bar"
  2. Use after move. Reading key/value after std::move relies on emplace leaving its arguments untouched when insertion fails, which the standard does not guarantee (an implementation may construct the node first). This is the bugprone-use-after-move finding in src/benchmark.cc from the clang-tidy list in [BUG] clang-tidy warnings and errors #1925. The moves were added in core(perf): use string move constructor for AddCustomContext #2005.

The fix reads the key and the existing value through the iterator returned by emplace, so it keeps the moves from #2005 and never touches the moved-from strings.

Testing

  • AddCustomContext.DuplicateKey now captures stderr and checks the warning text. It fails on current main (prints "qux") and passes with this change.
  • Full ctest: 88/88 pass with clang 18 and gcc (Linux).
  • clang-format --dry-run --Werror clean on both files; clang-tidy no longer reports bugprone-use-after-move in src/benchmark.cc.

Replaces #2374, which I closed because its commits carried a co-author trailer that cannot be covered by the CLA.

AI usage disclosure (per AGENTS.md)

I used Claude (Anthropic's AI assistant) to help find this clang-tidy finding, write the fix and the test, and run the test suite. I have reviewed the change and understand it, and I'm happy to discuss or revise it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8

AddCustomContext moved key and value into std::map::emplace and then
printed them in the duplicate-key warning. The warning reported the
rejected new value instead of the stored one, and reading key/value
after std::move relies on emplace not consuming its arguments on
failure (clang-tidy bugprone-use-after-move, see google#1925).

Read the key and existing value through the iterator returned by
emplace instead.
The test fails without the previous commit, because the warning reported
the rejected value "qux" instead of the stored value "bar".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant