Add defensive check to malloc