diff --git a/libavutil/frame.c b/libavutil/frame.c
index 53e6174223ad328601b0d67dd9d262e00b1af81e..9050946c487df6273a4e708d68af736914b53474 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -349,6 +349,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 wipe_side_data(dst);
                 return AVERROR(ENOMEM);
             }
+            if (sd_src->buf) {
             sd_dst->buf = av_buffer_ref(sd_src->buf);
             if (!sd_dst->buf) {
                 wipe_side_data(dst);
@@ -356,6 +357,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
             }
             sd_dst->data = sd_dst->buf->data;
             sd_dst->size = sd_dst->buf->size;
+            }
         }
         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
     }