Added a null check for malloc