prevent passing NULL to memcpy (closes #22605)

Patch by Jakub Wilk.