properly handle malloc failure (closes #24044)

Patch by Christian Heimes.
2 files changed