diff --git a/libswscale/utils.c b/libswscale/utils.c
index e5e4d60dd219c104bcfc136c08646954a481f013..f0a2b464fcac716c2d72d0d5d624c30a0e840083 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -844,8 +844,10 @@ SwsContext *sws_alloc_context(void)
 {
     SwsContext *c = av_mallocz(sizeof(SwsContext));
 
-    c->av_class = &sws_context_class;
-    av_opt_set_defaults(c);
+    if (c) {
+        c->av_class = &sws_context_class;
+        av_opt_set_defaults(c);
+    }
 
     return c;
 }