Commit 9724ff03 authored by Bartosz Podrygajlo's avatar Bartosz Podrygajlo

Fix memory leaks and heap buffer overflow in test_thread-pool.

parent 5af81bbc
...@@ -46,7 +46,7 @@ void displayList(notifiedFIFO_t *nf) ...@@ -46,7 +46,7 @@ void displayList(notifiedFIFO_t *nf)
struct testData { struct testData {
int id; int id;
int sleepTime; int sleepTime;
char txt[30]; char txt[50];
}; };
void processing(void *arg) void processing(void *arg)
...@@ -64,21 +64,27 @@ int main() ...@@ -64,21 +64,27 @@ int main()
logInit(); logInit();
notifiedFIFO_t myFifo; notifiedFIFO_t myFifo;
initNotifiedFIFO(&myFifo); initNotifiedFIFO(&myFifo);
int num_elements_on_queue = 0;
pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1234, NULL, NULL)); pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1234, NULL, NULL));
num_elements_on_queue++;
for (int i = 10; i > 1; i--) { for (int i = 10; i > 1; i--) {
pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1000 + i, NULL, NULL)); pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 1000 + i, NULL, NULL));
num_elements_on_queue++;
} }
displayList(&myFifo); displayList(&myFifo);
notifiedFIFO_elt_t *tmp = pullNotifiedFIFO(&myFifo); notifiedFIFO_elt_t *tmp = pullNotifiedFIFO(&myFifo);
printf("pulled: %lu\n", tmp->key); printf("pulled: %lu\n", tmp->key);
displayList(&myFifo); displayList(&myFifo);
delNotifiedFIFO_elt(tmp);
tmp = pullNotifiedFIFO(&myFifo); tmp = pullNotifiedFIFO(&myFifo);
num_elements_on_queue--;
printf("pulled: %lu\n", tmp->key); printf("pulled: %lu\n", tmp->key);
displayList(&myFifo); displayList(&myFifo);
pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 12345678, NULL, NULL)); pushNotifiedFIFO(&myFifo, newNotifiedFIFO_elt(sizeof(struct testData), 12345678, NULL, NULL));
displayList(&myFifo); displayList(&myFifo);
delNotifiedFIFO_elt(tmp);
do { do {
tmp = pollNotifiedFIFO(&myFifo); tmp = pollNotifiedFIFO(&myFifo);
...@@ -86,9 +92,12 @@ int main() ...@@ -86,9 +92,12 @@ int main()
if (tmp) { if (tmp) {
printf("pulled: %lu\n", tmp->key); printf("pulled: %lu\n", tmp->key);
displayList(&myFifo); displayList(&myFifo);
delNotifiedFIFO_elt(tmp);
num_elements_on_queue--;
} else } else
printf("Empty list \n"); printf("Empty list \n");
} while (tmp); } while (num_elements_on_queue > 0);
AssertFatal(pollNotifiedFIFO(&myFifo) == NULL, "Unexpected extra element on queue\n");
tpool_t pool; tpool_t pool;
char params[] = "1,2,3,4,5"; char params[] = "1,2,3,4,5";
...@@ -129,5 +138,6 @@ int main() ...@@ -129,5 +138,6 @@ int main()
dur / 1000, dur / 1000,
cumulProcessTime / 1000, cumulProcessTime / 1000,
(dur - cumulProcessTime) / (1000 * nb_jobs)); (dur - cumulProcessTime) / (1000 * nb_jobs));
abortTpool(&pool);
return 0; return 0;
} }
...@@ -336,7 +336,9 @@ static inline int abortTpool(tpool_t *t) { ...@@ -336,7 +336,9 @@ static inline int abortTpool(tpool_t *t) {
thread = t->allthreads; thread = t->allthreads;
while (thread != NULL) { while (thread != NULL) {
pthread_cancel(thread->threadID); pthread_cancel(thread->threadID);
thread = thread->next; struct one_thread *next = thread->next;
free(thread);
thread = next;
} }
return nbRemoved; return nbRemoved;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment