Skip to content

Commit 03b1200

Browse files
authored
bpo-33015: Fix UB in pthread PyThread_start_new_thread (GH-6008) (GH-10822)
Fix an undefined behaviour in the pthread implementation of PyThread_start_new_thread(): add a function wrapper to always return NULL. Add pythread_callback struct and pythread_wrapper() to thread_pthread.h. (cherry picked from commit 9eea6ea)
1 parent af7e81f commit 03b1200

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an undefined behaviour in the pthread implementation of
2+
:c:func:`PyThread_start_new_thread`: add a function wrapper to always return
3+
``NULL``.

Python/thread_pthread.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ PyThread__init_thread(void)
183183
* Thread support.
184184
*/
185185

186+
/* bpo-33015: pythread_callback struct and pythread_wrapper() cast
187+
"void func(void *)" to "void* func(void *)": always return NULL.
188+
189+
PyThread_start_new_thread() uses "void func(void *)" type, whereas
190+
pthread_create() requires a void* return value. */
191+
typedef struct {
192+
void (*func) (void *);
193+
void *arg;
194+
} pythread_callback;
195+
196+
static void *
197+
pythread_wrapper(void *arg)
198+
{
199+
/* copy func and func_arg and free the temporary structure */
200+
pythread_callback *callback = arg;
201+
void (*func)(void *) = callback->func;
202+
void *func_arg = callback->arg;
203+
PyMem_RawFree(arg);
204+
205+
func(func_arg);
206+
return NULL;
207+
}
186208

187209
long
188210
PyThread_start_new_thread(void (*func)(void *), void *arg)
@@ -218,21 +240,31 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
218240
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
219241
#endif
220242

243+
pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback));
244+
245+
if (callback == NULL) {
246+
return -1;
247+
}
248+
249+
callback->func = func;
250+
callback->arg = arg;
251+
221252
status = pthread_create(&th,
222253
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
223254
&attrs,
224255
#else
225256
(pthread_attr_t*)NULL,
226257
#endif
227-
(void* (*)(void *))func,
228-
(void *)arg
229-
);
258+
pythread_wrapper, callback);
230259

231260
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
232261
pthread_attr_destroy(&attrs);
233262
#endif
234-
if (status != 0)
263+
264+
if (status != 0) {
265+
PyMem_RawFree(callback);
235266
return -1;
267+
}
236268

237269
pthread_detach(th);
238270

0 commit comments

Comments
 (0)