Fix segfaults/off-by-one bug in read scenario.