Skip to content

Commit f2c8ddb

Browse files
committed
Fix --strace use-after-free in pthread_join()
1 parent d1d4388 commit f2c8ddb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

libc/intrin/pthread_tid.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
#include "libc/thread/thread.h"
2323

2424
int _pthread_tid(struct PosixThread *pt) {
25-
if (IsWindows()) // xxx: fixme
26-
return pt->ptid;
2725
int tid = 0;
28-
while (pt && !(tid = atomic_load_explicit(&pt->ptid, memory_order_acquire))) {
26+
while (pt && !(tid = atomic_load_explicit(&pt->ptid, memory_order_acquire)))
2927
pthread_pause_np();
30-
}
3128
return tid;
3229
}

libc/thread/pthread_timedjoin_np.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ static errno_t _pthread_wait(atomic_int *ctid, struct timespec *abstime) {
103103
*/
104104
errno_t pthread_timedjoin_np(pthread_t thread, void **value_ptr,
105105
struct timespec *abstime) {
106+
int tid;
106107
errno_t err;
107108
struct PosixThread *pt;
108109
enum PosixThreadStatus status;
109110
pt = (struct PosixThread *)thread;
111+
tid = _pthread_tid(pt);
112+
unassert(_pthread_tid(pt));
110113
status = atomic_load_explicit(&pt->pt_status, memory_order_acquire);
111114
// "The behavior is undefined if the value specified by the thread
112115
// argument to pthread_join() does not refer to a joinable thread."
@@ -121,7 +124,7 @@ errno_t pthread_timedjoin_np(pthread_t thread, void **value_ptr,
121124
}
122125
_pthread_unref(pt);
123126
}
124-
STRACE("pthread_timedjoin_np(%d, %s, %s) → %s", _pthread_tid(pt),
127+
STRACE("pthread_timedjoin_np(%d, %s, %s) → %s", tid,
125128
DescribeReturnValue(alloca(30), err, value_ptr),
126129
DescribeTimespec(err ? -1 : 0, abstime), DescribeErrno(err));
127130
return err;

0 commit comments

Comments
 (0)