diff --git a/src/compiler/translator/HashNames.cpp b/src/compiler/translator/HashNames.cpp index 62b4f6d063..4550760e1a 100644 --- a/src/compiler/translator/HashNames.cpp +++ b/src/compiler/translator/HashNames.cpp @@ -76,6 +76,24 @@ ImmutableString HashName(const ImmutableString &name, // Has a hash function ImmutableString hashedName = HashName(name, hashFunction); + // The hash function is not collision-resistant. If a different source + // identifier already hashed to the same value, disambiguate by appending + // the original name so two distinct symbols can never share an emitted + // name. (A 64-bit birthday collision is feasible offline in ~2^32 work, + // and a collision lets web content alias differently-typed/-sized + // declarations after translation.) + if (nameMap) + { + for (const auto &it : *nameMap) + { + if (it.second == hashedName.data() && it.first != name.data()) + { + hashedName = + BuildConcatenatedImmutableString(hashedName, '_', name); + break; + } + } + } AddToNameMapIfNotMapped(name, hashedName, nameMap); return hashedName; }