Fix potential infinite loop in double wildcard processing

Fixes #4610