Fix incorrect buffer length handling