[clang-tools-extra] [clangd] Fix dangling references to StringMap's value. (PR #169339)
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 30 00:12:01 PST 2025
HighCommander4 wrote:
> StringMap insertion may cause elements to be rehased, or the underlying storage to be reallocated, so it is generatelly unsafe to take pointers to StringMap "values".
Are you sure about this?
The [comment above](https://searchfox.org/llvm/rev/f5742c4d540a20651a67de51e16242a52e5d4064/clang-tools-extra/clangd/GlobalCompilationDatabase.h#145) the declaration of `DirCaches` specifically says "We can hand out pointers as they're stable and entries are never removed."
And while I haven't studied the implementation of `StringMap` in detail, I at least can't find an obvious place where values would get moved after initial insertion: the hashtable stores [pointers to entries](https://searchfox.org/llvm/rev/f5742c4d540a20651a67de51e16242a52e5d4064/llvm/include/llvm/ADT/StringMap.h#35-38) (suggesting that you can reallocate/rearrange the table without moving the entry objects themselves), and the entries are created via placement-new [here](https://searchfox.org/llvm/rev/f5742c4d540a20651a67de51e16242a52e5d4064/llvm/include/llvm/ADT/StringMapEntry.h#128) and I'm not seeing what would call that during a rehash or reallocation.
https://github.com/llvm/llvm-project/pull/169339
More information about the cfe-commits
mailing list