Add checks for malloc() failures.
Submitted by: Dmitry Luhtionov <dmitryluhtionov@gmail.com> MFC after: 3 days