Fix an incorrect malloc return check.
Add missing malloc return checks.