Skip to content

Commit 9eea6ea

Browse files
siddheshvstinner
authored andcommitted
bpo-33015: Fix UB in pthread PyThread_start_new_thread (GH-6008)
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.
1 parent 1600f60 commit 9eea6ea

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
@@ -152,6 +152,28 @@ PyThread__init_thread(void)
152152
* Thread support.
153153
*/
154154

155+
/* bpo-33015: pythread_callback struct and pythread_wrapper() cast
156+
"void func(void *)" to "void* func(void *)": always return NULL.
157+
158+
PyThread_start_new_thread() uses "void func(void *)" type, whereas
159+
pthread_create() requires a void* return value. */
160+
typedef struct {
161+
void (*func) (void *);
162+
void *arg;
163+
} pythread_callback;
164+
165+
static void *
166+
pythread_wrapper(void *arg)
167+
{
168+
/* copy func and func_arg and free the temporary structure */
169+
pythread_callback *callback = arg;
170+
void (*func)(void *) = callback->func;
171+
void *func_arg = callback->arg;
172+
PyMem_RawFree(arg);
173+
174+
func(func_arg);
175+
return NULL;
176+
}
155177

156178
unsigned long
157179
PyThread_start_new_thread(void (*func)(void *), void *arg)
@@ -188,21 +210,31 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
188210
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
189211
#endif
190212

213+
pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback));
214+
215+
if (callback == NULL) {
216+
return PYTHREAD_INVALID_THREAD_ID;
217+
}
218+
219+
callback->func = func;
220+
callback->arg = arg;
221+
191222
status = pthread_create(&th,
192223
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
193224
&attrs,
194225
#else
195226
(pthread_attr_t*)NULL,
196227
#endif
197-
(void* (*)(void *))func,
198-
(void *)arg
199-
);
228+
pythread_wrapper, callback);
200229

201230
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
202231
pthread_attr_destroy(&attrs);
203232
#endif
204-
if (status != 0)
233+
234+
if (status != 0) {
235+
PyMem_RawFree(callback);
205236
return PYTHREAD_INVALID_THREAD_ID;
237+
}
206238

207239
pthread_detach(th);
208240

0 commit comments

Comments
 (0)