Browse code

fix memleaks

git-svn: trunk@2678

Tomasz Kojm authored on 2007/02/08 02:32:13
Showing 5 changed files
... ...
@@ -1,3 +1,8 @@
1
+Wed Feb  7 18:30:35 CET 2007 (tk)
2
+---------------------------------
3
+  * libclamav, shared: fix minor memory leaks in lockdb and cfgparser,
4
+		       patch from Edwin
5
+
1 6
 Wed Feb  7 18:11:32 CET 2007 (tk)
2 7
 ---------------------------------
3 8
   * libclamav: fix memory leaks in mew and unrar and use of uninitialized
... ...
@@ -95,6 +95,11 @@ int cli_unlockdb(const char *dbdirpath)
95 95
     return CL_SUCCESS;
96 96
 }
97 97
 
98
+int cli_freelocks(void)
99
+{
100
+	return CL_SUCCESS;
101
+}
102
+
98 103
 #else /* !DONT_LOCK_DBDIRS */
99 104
 
100 105
 int cli_readlockdb(const char *dbdirpath, int wait)
... ...
@@ -107,6 +112,29 @@ int cli_writelockdb(const char *dbdirpath, int wait)
107 107
     return cli_lockdb(dbdirpath, wait, 1);
108 108
 }
109 109
 
110
+int cli_freelocks(void)
111
+{
112
+	struct dblock * lock, *nextlock, *usedlocks = NULL;
113
+
114
+	pthread_mutex_lock(&lock_mutex);
115
+	for(lock = dblocks; lock; lock = nextlock) {
116
+		/* there might be some locks in use, eg: during a db reload, a failure can lead 
117
+		 * to cl_free being called */
118
+		nextlock = lock->lock_link;
119
+		if(lock->lock_type != -1 && lock->lock_fd != -1) {
120
+			lock->lock_link = usedlocks;
121
+			usedlocks = lock;
122
+		}
123
+		else {
124
+			free(lock);
125
+		}
126
+	}
127
+	dblocks = usedlocks;
128
+	pthread_mutex_unlock(&lock_mutex);
129
+	return CL_SUCCESS;
130
+}
131
+
132
+
110 133
 int cli_unlockdb(const char *dbdirpath)
111 134
 {
112 135
 	char lock_file[NAME_MAX];
... ...
@@ -23,5 +23,6 @@
23 23
 int cli_writelockdb(const char *dbdirpath, int wait);
24 24
 int cli_readlockdb(const char *dbdirpath, int wait);
25 25
 int cli_unlockdb(const char *dbdirpath);
26
+int cli_freelocks(void);
26 27
 
27 28
 #endif
... ...
@@ -1681,6 +1681,7 @@ void cl_free(struct cl_engine *engine)
1681 1681
     if(engine->dconf)
1682 1682
 	free(engine->dconf);
1683 1683
 
1684
+    cli_freelocks();
1684 1685
     free(engine);
1685 1686
 }
1686 1687
 
... ...
@@ -138,7 +138,7 @@ struct cfgstruct *getcfg(const char *cfgfile, int verbose)
138 138
 	if(!pt->name)
139 139
 	    break;
140 140
 
141
-	if(regcfg(&copt, strdup(pt->name), pt->strarg ? strdup(pt->strarg) : NULL, pt->numarg, pt->multiple) < 0) {
141
+	if(regcfg(&copt, pt->name, pt->strarg ? strdup(pt->strarg) : NULL, pt->numarg, pt->multiple) < 0) {
142 142
 	    fprintf(stderr, "ERROR: Can't register new options (not enough memory)\n");
143 143
 	    freecfg(copt);
144 144
 	    return NULL;
... ...
@@ -379,6 +379,7 @@ struct cfgstruct *getcfg(const char *cfgfile, int verbose)
379 379
 		freecfg(copt);
380 380
 		return NULL;
381 381
 	    }
382
+	    free(name);
382 383
 	}
383 384
     }
384 385
 
... ...
@@ -444,7 +445,7 @@ static int regcfg(struct cfgstruct **copt, char *optname, char *strarg, int numa
444 444
     if(!newnode)
445 445
 	return -1;
446 446
 
447
-    newnode->optname = optname;
447
+    newnode->optname = optname ? strdup(optname) : NULL;
448 448
     newnode->nextarg = NULL;
449 449
     newnode->next = NULL;
450 450
     newnode->enabled = 0;
... ...
@@ -470,17 +471,25 @@ static int regcfg(struct cfgstruct **copt, char *optname, char *strarg, int numa
470 470
 
471 471
 		pt->nextarg = newnode;
472 472
 	    } else {
473
+		if(pt->strarg)
474
+		    free(pt->strarg);
473 475
 		pt->strarg = newnode->strarg;
474 476
 		pt->numarg = newnode->numarg;
475 477
 		pt->enabled = newnode->enabled;
478
+		if(newnode->optname)
479
+		    free(newnode->optname);
476 480
 		free(newnode);
477 481
 	    }
478 482
 	    return 3; /* registered additional argument */
479 483
 
480 484
 	} else {
485
+	    if(pt->strarg)
486
+		free(pt->strarg);
481 487
 	    pt->strarg = newnode->strarg;
482 488
 	    pt->numarg = newnode->numarg;
483 489
 	    pt->enabled = newnode->enabled;
490
+	    if(newnode->optname)
491
+		free(newnode->optname);
484 492
 	    free(newnode);
485 493
 	    return 2;
486 494
 	}