Skip to content

Commit

Permalink
Add smtprc-tid unsound case
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Sep 20, 2023
1 parent ba3be87 commit f217bdf
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/regression/03-practical/50-smtprc-tid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <pthread.h>
#include <stdlib.h>

int threads_total = 4;
pthread_t *tids;

void *cleaner(void *arg) {
while (1) {
for (int i = 0; i < threads_total; i++) {
if (tids[i]) { // RACE!
if (!pthread_join(tids[i], NULL)) // RACE!
tids[i] = 0; // RACE!
}
}
}
return NULL;
}

void *thread(int i) { // wrong argument type is important
tids[i] = pthread_self(); // RACE!
return NULL;
}

int main() {
pthread_t tid;
tids = malloc(threads_total * sizeof(pthread_t));

for(int i = 0; i < threads_total; i++)
tids[i] = 0;

pthread_create(&tid, NULL, cleaner, NULL);

for(int i = 0; i < threads_total; i++) {
pthread_create(&tid, NULL, thread, (int *)i); // cast is important
}

return 0;
}

0 comments on commit f217bdf

Please sign in to comment.