The problem was that if base_name() was called from a context without an actual base name, it could gulp up the entire string, which can result in recursive duplications. The fix is to be more strict as to what qualifies as a base name.
Fixes PR31031
Thanks
Hmm, I wonder if we should negate and hoist this into the condition rather than the explicit check here. It makes it slightly more obvious what we are trying to do here. However, it does make failing more challenging (since we cannot as easily identify if the extraction failed.