Browse code

fix possible memory leaks and improve handling of memory allocation errors (closes bug#75)

git-svn: trunk@2455

Tomasz Kojm authored on 2006/10/29 23:12:53
Showing 2 changed files
... ...
@@ -1,3 +1,9 @@
1
+Sun Oct 29 15:06:21 CET 2006 (tk)
2
+---------------------------------
3
+  * clamd/thrmgr.c: fix possible memory leaks and improve handling of memory
4
+		    allocation errors (closes bug#75)
5
+		    Patch by Mark Pizzolato
6
+
1 7
 Sun Oct 29 14:55:20 CET 2006 (tk)
2 8
 ---------------------------------
3 9
   * shared/options.[ch]: fix 'const' declarations (closes bug#69)
... ...
@@ -9,7 +15,6 @@ Sun Oct 29 13:54:38 GMT 2006 (njh)
9 9
 			It would be better to have a separate limit for that
10 10
 			Bugzilla 104
11 11
 
12
->>>>>>> 1.1568
13 12
 Sun Oct 29 14:40:14 CET 2006 (tk)
14 13
 ---------------------------------
15 14
   * clamd/session.c: NAME_MAX was not defined (closes bug#60)
... ...
@@ -40,20 +40,27 @@ static work_queue_t *work_queue_new(void)
40 40
 	work_queue_t *work_q;
41 41
 	
42 42
 	work_q = (work_queue_t *) mmalloc(sizeof(work_queue_t));
43
+	if (!work_q) {
44
+		return NULL;
45
+	}
43 46
 	
44 47
 	work_q->head = work_q->tail = NULL;
45 48
 	work_q->item_count = 0;
46 49
 	return work_q;
47 50
 }
48 51
 
49
-static void work_queue_add(work_queue_t *work_q, void *data)
52
+static int work_queue_add(work_queue_t *work_q, void *data)
50 53
 {
51 54
 	work_item_t *work_item;
52 55
 	
53 56
 	if (!work_q) {
54
-		return;
57
+		return FALSE;
55 58
 	}
56 59
 	work_item = (work_item_t *) mmalloc(sizeof(work_item_t));
60
+	if (!work_item) {
61
+		return FALSE;
62
+	}
63
+	
57 64
 	work_item->next = NULL;
58 65
 	work_item->data = data;
59 66
 	gettimeofday(&(work_item->time_queued), NULL);
... ...
@@ -66,7 +73,7 @@ static void work_queue_add(work_queue_t *work_q, void *data)
66 66
 		work_q->tail = work_item;
67 67
 		work_q->item_count++;
68 68
 	}
69
-	return;
69
+	return TRUE;
70 70
 }
71 71
 
72 72
 static void *work_queue_pop(work_queue_t *work_q)
... ...
@@ -133,6 +140,9 @@ threadpool_t *thrmgr_new(int max_threads, int idle_timeout, void (*handler)(void
133 133
 	}
134 134
 	
135 135
 	threadpool = (threadpool_t *) mmalloc(sizeof(threadpool_t));
136
+	if (!threadpool) {
137
+		return NULL;
138
+	}
136 139
 
137 140
 	threadpool->queue = work_queue_new();
138 141
 	if (!threadpool->queue) {
... ...
@@ -147,16 +157,25 @@ threadpool_t *thrmgr_new(int max_threads, int idle_timeout, void (*handler)(void
147 147
 	
148 148
 	pthread_mutex_init(&(threadpool->pool_mutex), NULL);
149 149
 	if (pthread_cond_init(&(threadpool->pool_cond), NULL) != 0) {
150
+		pthread_mutex_destroy(&(threadpool->pool_mutex));
151
+		free(threadpool->queue);
150 152
 		free(threadpool);
151 153
 		return NULL;
152 154
 	}
153 155
 		
154 156
 	if (pthread_attr_init(&(threadpool->pool_attr)) != 0) {
157
+		pthread_cond_destroy(&(threadpool->pool_cond));
158
+		pthread_mutex_destroy(&(threadpool->pool_mutex));
159
+		free(threadpool->queue);
155 160
 		free(threadpool);
156 161
 		return NULL;
157 162
 	}
158 163
 	
159 164
 	if (pthread_attr_setdetachstate(&(threadpool->pool_attr), PTHREAD_CREATE_DETACHED) != 0) {
165
+		pthread_attr_destroy(&(threadpool->pool_attr));
166
+		pthread_cond_destroy(&(threadpool->pool_cond));
167
+		pthread_mutex_destroy(&(threadpool->pool_mutex));
168
+		free(threadpool->queue);
160 169
 		free(threadpool);
161 170
 		return NULL;
162 171
 	}
... ...
@@ -247,7 +266,13 @@ int thrmgr_dispatch(threadpool_t *threadpool, void *user_data)
247 247
 		}
248 248
 		return FALSE;
249 249
 	}
250
-	work_queue_add(threadpool->queue, user_data);
250
+	if (!work_queue_add(threadpool->queue, user_data)) {
251
+		if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
252
+			logg("!Mutex unlock failed\n");
253
+			return FALSE;
254
+		}
255
+		return FALSE;
256
+	}
251 257
 
252 258
 	if ((threadpool->thr_idle == 0) &&
253 259
 			(threadpool->thr_alive < threadpool->thr_max)) {