Fix incorrect bounds check, from Alexander Cherepanov
Bail out on partial reads, from Alexander Cherepanov
2 more bugs.
merge with upstream
merge with upstream