sync with upstream, fix read-past-end errors