Skip to content

Commit 085cc93

Browse files
authored
Merge pull request #4486 from rlefko/fix-pthread-init-memleak
Fix memory leak in pthread init functions on failure
2 parents e6e5a95 + c59812e commit 085cc93

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/common/threading.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,14 @@ int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t con
143143
*mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
144144
if (!*mutex)
145145
return 1;
146-
return pthread_mutex_init(*mutex, attr);
146+
{
147+
int const ret = pthread_mutex_init(*mutex, attr);
148+
if (ret != 0) {
149+
ZSTD_free(*mutex);
150+
*mutex = NULL;
151+
}
152+
return ret;
153+
}
147154
}
148155

149156
int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
@@ -164,7 +171,14 @@ int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const*
164171
*cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
165172
if (!*cond)
166173
return 1;
167-
return pthread_cond_init(*cond, attr);
174+
{
175+
int const ret = pthread_cond_init(*cond, attr);
176+
if (ret != 0) {
177+
ZSTD_free(*cond);
178+
*cond = NULL;
179+
}
180+
return ret;
181+
}
168182
}
169183

170184
int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)

0 commit comments

Comments
 (0)