diff --git a/compat/os2threads.h b/compat/os2threads.h
index 441ac437107a0d0666f509a3da4663a70458a4b6..5b6ca5579df3bc51d546c993bd452efb771a7c9e 100644
--- a/compat/os2threads.h
+++ b/compat/os2threads.h
@@ -66,6 +66,8 @@ static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr
     struct thread_arg *thread_arg;
 
     thread_arg = av_mallocz(sizeof(struct thread_arg));
+    if (!thread_arg)
+        return ENOMEM;
 
     thread_arg->start_routine = start_routine;
     thread_arg->arg = arg;