writing length check and card invalidation on write error