Skip to content

Commit

Permalink
pthreads.h: avoid undefined behavior
Browse files Browse the repository at this point in the history
You can't pass a function pointer through a void pointer.
So wrap the pthread callback in a struct.

Fixes libressl#966
  • Loading branch information
botovq authored and busterb committed Mar 3, 2024
1 parent b2bd5fe commit 84e6283
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions include/compat/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ struct pthread_once {
};
typedef struct pthread_once pthread_once_t;

struct _pthread_win32_cb_arg {
void (*cb)(void);
};

static inline BOOL CALLBACK
_pthread_once_win32_cb(PINIT_ONCE once, PVOID param, PVOID *context)
{
void (*cb) (void) = param;
cb();
struct _pthread_win32_cb_arg *arg = param;
arg->cb();
return TRUE;
}

static inline int
pthread_once(pthread_once_t *once, void (*cb) (void))
{
BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, cb, NULL);
struct _pthread_win32_cb_arg arg = { .cb = cb };
BOOL rc = InitOnceExecuteOnce(&once->once, _pthread_once_win32_cb, &arg, NULL);
if (rc == 0)
return -1;
else
Expand Down

0 comments on commit 84e6283

Please sign in to comment.