Fixes off-by-one error when finding missing endstream

Fixes #4387.