Browse code

fix some leaks and crashes

git-svn: trunk@4390

Török Edvin authored on 2008/11/13 04:11:19
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Nov 12 20:12:09 EET 2008 (edwin)
2
+------------------------------------
3
+ * contrib/clamdtop/clamdtop.c: fix some leaks and crashes
4
+
1 5
 Wed Nov 12 19:32:17 EET 2008 (edwin)
2 6
 ------------------------------------
3 7
  * libclamav/readdb.c, libltdl/Makefile.am, libltdl/Makefile.in: Fix
... ...
@@ -357,7 +357,7 @@ static void cleanup(void)
357 357
 	}
358 358
 	curses_inited = 0;
359 359
 	for (i=0;i<global.num_clamd;i++) {
360
-		if (global.conn[i].sd)
360
+		if (global.conn[i].sd && global.conn[i].sd != -1)
361 361
 			send_string_noreconn(&global.conn[i], "END\n");
362 362
 		close(global.conn[i].sd);
363 363
 		free(global.conn[i].version);
... ...
@@ -423,16 +423,17 @@ static void print_con_info(conn_t *conn, const char *fmt, ...)
423 423
 	va_list ap;
424 424
 	va_start(ap, fmt);
425 425
 	if (stats_head_window) {
426
-		char *buf = malloc(maxx+1);
426
+		char *buf = malloc(maxx);
427 427
 		memset(buf, ' ', maxx);
428 428
 		OOM_CHECK(buf);
429
-		vsnprintf(buf, maxx, fmt, ap);
429
+		vsnprintf(buf, maxx-1, fmt, ap);
430 430
 		buf[strlen(buf)] = ' ';
431
-		buf[maxx] = '\0';
431
+		buf[maxx-1] = '\0';
432 432
 		wattron(stats_head_window, ERROR_ATTR);
433 433
 		mvwprintw(stats_head_window, conn->line, 0, "%s", buf);
434 434
 		wattroff(stats_head_window, ERROR_ATTR);
435 435
 		wrefresh(stats_head_window);
436
+		free(buf);
436 437
 	} else
437 438
 		vfprintf(stdout, fmt, ap);
438 439
 	va_end(ap);
... ...
@@ -504,6 +505,8 @@ static int make_connection(const char *soname, conn_t *conn)
504 504
 	tv.tv_usec = 0;
505 505
 	setsockopt(conn->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
506 506
 	send_string(conn, "SESSION\nVERSION\n");
507
+	free(conn->version);
508
+	conn->version = NULL;
507 509
 	read_version(conn);
508 510
 	return 0;
509 511
 }
... ...
@@ -534,7 +537,6 @@ static void reconnect(conn_t *conn)
534 534
 		print_con_info(conn, "Unable to reconnect to %s: %s", conn->remote, strerror(errno));
535 535
 		exit(3);
536 536
 	}
537
-	free(conn->version);
538 537
 	tries = 0;
539 538
 }
540 539
 
... ...
@@ -652,30 +654,32 @@ static void output_memstats(struct stats *stats)
652 652
 	int blink = 0;
653 653
 
654 654
 	werase(mem_window);
655
-	box(mem_window, 0, 0);
655
+	if (stats->mem != -1) {
656
+		box(mem_window, 0, 0);
656 657
 
657
-	snprintf(buf, sizeof(buf),"heap %4luM mmap %4luM unused %3luM",
658
-			stats->lheapu/1024, stats->lmmapu/1024, stats->lreleasable/1024);
659
-	mvwprintw(mem_window, 1, 1, "Mem:  ");
660
-	print_colored(mem_window, buf);
658
+		snprintf(buf, sizeof(buf),"heap %4luM mmap %4luM unused %3luM",
659
+				stats->lheapu/1024, stats->lmmapu/1024, stats->lreleasable/1024);
660
+		mvwprintw(mem_window, 1, 1, "Mem:  ");
661
+		print_colored(mem_window, buf);
661 662
 
662
-	mvwprintw(mem_window, 2, 1, "Libc: ");
663
-	snprintf(buf, sizeof(buf),"used %4luM free %4luM total %4luM",
664
-			stats->ltotalu/1024, stats->ltotalf/1024, (stats->ltotalu+stats->ltotalf)/1024);
665
-	print_colored(mem_window, buf);
663
+		mvwprintw(mem_window, 2, 1, "Libc: ");
664
+		snprintf(buf, sizeof(buf),"used %4luM free %4luM total %4luM",
665
+				stats->ltotalu/1024, stats->ltotalf/1024, (stats->ltotalu+stats->ltotalf)/1024);
666
+		print_colored(mem_window, buf);
666 667
 
667
-	mvwprintw(mem_window, 3, 1, "Pool: ");
668
-	snprintf(buf, sizeof(buf), "count   %u  used %4luM total %4luM",
668
+		mvwprintw(mem_window, 3, 1, "Pool: ");
669
+		snprintf(buf, sizeof(buf), "count   %u  used %4luM total %4luM",
669 670
 			stats->pools_cnt, stats->lpoolu/1024, stats->lpoolt/1024);
670
-	print_colored(mem_window, buf);
671
+		print_colored(mem_window, buf);
671 672
 
672
-	totalmem = stats->lheapu + stats->lmmapu + stats->lpoolt;
673
-	if(totalmem > biggest_mem) {
674
-		biggest_mem = totalmem;
675
-		blink = 1;
673
+		totalmem = stats->lheapu + stats->lmmapu + stats->lpoolt;
674
+		if(totalmem > biggest_mem) {
675
+			biggest_mem = totalmem;
676
+			blink = 1;
677
+		}
678
+		show_bar(mem_window, 4, totalmem, stats->lmmapu + stats->lreleasable + stats->lpoolt - stats->lpoolu,
679
+				biggest_mem, blink);
676 680
 	}
677
-	show_bar(mem_window, 4, totalmem, stats->lmmapu + stats->lreleasable + stats->lpoolt - stats->lpoolu,
678
-			biggest_mem, blink);
679 681
 	wrefresh(mem_window);
680 682
 }
681 683
 
... ...
@@ -694,6 +698,7 @@ static void parse_memstats(const char *line, struct stats *stats)
694 694
 	stats->lpoolu = pools_used*1000;
695 695
 	stats->lpoolt = pools_total*1000;
696 696
 	stats->mem = heapu + mmapu + pools_total;
697
+	if (stats->mem == 0) stats->mem = -1;
697 698
 }
698 699
 
699 700
 static int show_detail(int idx)
... ...
@@ -821,6 +826,10 @@ static void parse_stats(conn_t *conn, struct stats *stats, unsigned idx)
821 821
 	else
822 822
 		stats->remote = "local";
823 823
 
824
+	if (!conn->version) {
825
+		stats->engine_version = "???";
826
+		return;
827
+	}
824 828
 	p = pstart = conn->version;
825 829
 	/* find digit in version */
826 830
 	while (*p && !isdigit(*p))
... ...
@@ -1025,7 +1034,8 @@ int main(int argc, char *argv[])
1025 1025
 			free_global_stats();
1026 1026
 			for(i=0;i<global.num_clamd;i++) {
1027 1027
 				struct stats *stats = &global.all_stats[i];
1028
-				send_string(&global.conn[i], "STATS\n");
1028
+				if (global.conn[i].sd != -1)
1029
+					send_string(&global.conn[i], "STATS\n");
1029 1030
 				memset(stats, 0, sizeof(*stats));
1030 1031
 				parse_stats(&global.conn[i], stats, i);
1031 1032
 			}