Skip to content

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

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

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

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.

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

mohitduhan19 and others added 2 commits November 11, 2026 12:47
AddCustomContext moved key and value into std::map::emplace and then
printed them in the duplicate-key warning. That has two problems:

* The warning printed the rejected new value instead of the value that
  is already stored, e.g. adding ("foo", "bar") then ("foo", "qux")
  reported that "foo" already exists with value "qux".
* Reading key/value after std::move relies on emplace not consuming its
  arguments when insertion fails, which the standard does not guarantee
  (flagged by clang-tidy bugprone-use-after-move, see google#1925).

Read the key and existing value through the iterator returned by
emplace instead, and check the warning text in the DuplicateKey test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8
The test fails without the previous commit, because the warning reported
the rejected value "qux" instead of the stored value "bar".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8
@google-cla

google-cla Bot commented Nov 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@mohitduhan19

Copy link
Copy Markdown
Author

Superseded by #2375 (same change, recreated so every commit author is covered by the CLA).

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