malloc: fix potential null pointer dereference