Skip to content

Commit add176e

Browse files
committed
Make sure we don't allocate a TLS ID clobbering an application defined one
Fixes #14359 (cherry picked from commit d9ca045)
1 parent 86384af commit add176e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/thread/SDL_thread.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ bool SDL_SetTLS(SDL_TLSID *id, const void *value, SDL_TLSDestructorCallback dest
8080
* will have the same storage index for this id.
8181
*/
8282
storage_index = SDL_GetAtomicInt(id) - 1;
83+
} else {
84+
// Make sure we don't allocate an ID clobbering this one
85+
int tls_id = SDL_GetAtomicInt(&SDL_tls_id);
86+
while (storage_index >= tls_id) {
87+
if (SDL_CompareAndSwapAtomicInt(&SDL_tls_id, tls_id, storage_index + 1)) {
88+
break;
89+
}
90+
tls_id = SDL_GetAtomicInt(&SDL_tls_id);
91+
}
8392
}
8493

8594
// Get the storage for the current thread

0 commit comments

Comments
 (0)