diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -185,8 +185,8 @@ Optional macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, bool isBundleLoader) { - StringRef path = mbref.getBufferIdentifier(); - DylibFile *&file = loadedDylibs[CachedHashStringRef(path)]; + CachedHashStringRef path(mbref.getBufferIdentifier()); + DylibFile *file = loadedDylibs[path]; if (file) return file; @@ -206,6 +206,11 @@ magic == file_magic::macho_bundle); file = make(mbref, umbrella, isBundleLoader); } + // Note that DylibFile's ctor may recursively invoke loadDylib(), which can + // cause loadedDylibs to get resized and its iterators invalidated. As such, + // we redo the key lookup here instead of caching an iterator from our earlier + // lookup at the start of the function. + loadedDylibs[path] = file; return file; }