avoid resource leak upon write() exception (Coverity)