Skip to content

Commit 0d4a616

Browse files
committed
Validate lockCount in mutexCreate and handle errors during mutex locking
1 parent 3e2bac8 commit 0d4a616

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/modules/openfx/mlt_openfx.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,8 @@ static OfxStatus mutexCreate(OfxMutexHandle *mutex, int lockCount)
12601260
{
12611261
if (!mutex)
12621262
return kOfxStatFailed;
1263+
if (lockCount < 0)
1264+
return kOfxStatErrValue;
12631265
OfxMutexImpl *m = (OfxMutexImpl *) malloc(sizeof(OfxMutexImpl));
12641266
if (!m)
12651267
return kOfxStatErrMemory;
@@ -1273,13 +1275,16 @@ static OfxStatus mutexCreate(OfxMutexHandle *mutex, int lockCount)
12731275
return kOfxStatErrMemory;
12741276
}
12751277
*mutex = (OfxMutexHandle) m;
1276-
// lockCount: if negative, lock abs(lockCount) times; if positive, unlock that many times
1277-
if (lockCount < 0) {
1278-
for (int i = 0; i < -lockCount; ++i)
1279-
pthread_mutex_lock(&m->mtx);
1280-
} else if (lockCount > 0) {
1281-
for (int i = 0; i < lockCount; ++i)
1282-
pthread_mutex_unlock(&m->mtx);
1278+
for (int i = 0; i < lockCount; ++i) {
1279+
err = pthread_mutex_lock(&m->mtx);
1280+
if (err != 0) {
1281+
while (i-- > 0)
1282+
pthread_mutex_unlock(&m->mtx);
1283+
pthread_mutex_destroy(&m->mtx);
1284+
free(m);
1285+
*mutex = NULL;
1286+
return kOfxStatFailed;
1287+
}
12831288
}
12841289
return kOfxStatOK;
12851290
}

0 commit comments

Comments
 (0)