Fix bug in error detection code, where a zero-length string was causing a traceback

Fixes #5064