Fix reading over the end of the allocated buffer.