Fix buffer length checking