prevent passing NULL to memcpy (closes #22605)

Patch by Jakub Wilk.
merge 3.4 (closes #22605)