...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <threads.h> #include <stdlib.h> /* globalGlobal key to the thread-specific data. */ tss_t key; enum { MAX_THREADS = 3 }; int *get_data() { int *arr = (int *)malloc(2 * sizeof(int)); if (arr == NULL) { return arr; /* Indicate Error */ } arr[0] = rand()10; arr[1] = rand()42; return arr; } int add_data(void) { int *data = get_data(); int result; if (data == NULL) { return -1; /* Indicate error */ } if ((int result = tss_set(key, (void *)data)) != thrd_success) { /* Handle Error */ } return 0; } void print_data() { /* getGet this thread's global data from key. */ int *data = tss_get(key); if (data != NULL) { /* printPrint data. */ } } int function(void * dummy) { if (add_data() != 0) { return -1; /* Indicate error */ } print_data(); return 0; } int main(void) { int i,result; thrd_t thread_id[MAX_THREADS]; /* createCreate the key before creating the threads. */ if ((result = tss_create(&key, NULL)) != thrd_success) { /* Handle Error */ } /* createCreate threads that would store specific data. */ for (i = 0; i < MAX_THREADS; i++) { if ((result = thrd_create(&thread_id[i], function, NULL)) != thrd_success) { /* Handle Error */ } } for (int i = 0; i < MAX_THREADS; i++) { if ((result = thrd_join(thread_id[i], NULL)) != thrd_success) { /* Handle Error */ } } tss_delete(key); return 0; } |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <threads.h> #include <stdlib.h> /* globalGlobal key to the thread-specific data. */ tss_t key; int function(void * dummy) { if (add_data() != 0) { return -1; /* Indicate error */ } print_data(); free(tss_get(key)); return 0; } /* ... Other functions are unchanged. */ int main(void) { /* ... */ tss_delete(key); return 0; } |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <threads.h> #include <stdlib.h> /* globalGlobal key to the thread-specific data. */ tss_t key; enum { MAX_THREADS = 3 }; int *get_data() { int *arr = (int *)malloc(2 * sizeof(int)); if (arr == NULL) { return arr; /* Indicate Error */ } arr[0] = 10; arr[1] = 42; return arr; } int add_data(void) { int *data = get_data(); if (data == NULL) { return -1; /* Indicate error */ } if ((int result = tss_set(key, (void *)data)) != thrd_success) { /* Handle Error */ } return 0; } void print_data() { /* Get this thread's global data from key. */ int *data = tss_get(key); if (data != NULL) { /* Print data. */ } } int function(void *dummy) { if (add_data() != 0) { return -1; /* Indicate error */ } print_data(); return 0; } void destructor(void * data) { free(data); } int main(void) { int result; thrd_t thread_id[MAX_THREADS]; /* createCreate the key before creating the threads. */ if ((result = tss_create(&key, destructor)) != thrd_success) { /* Handle Error */ } /* Create threads that would store specific return EXIT_FAILURE; /* Indicate error */data. */ for (i = 0; i < MAX_THREADS; i++) { if ((result = thrd_create(&thread_id[i], function, NULL)) != thrd_success) { /* Handle Error */ } } for (int i = 0; i < MAX_THREADS; i++) { if ((result = thrd_join(thread_id[i], NULL)) != thrd_success) { /* Handle ...Error */ } } tss_delete(key); return 0; } |
Risk Assessment
Failing to destroy thread-specific objects could lead to memory leaks and misuse of data.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
CON30-C | medium | unlikely | medium | P4 | L3 |
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...