fixed a leak where host and port were not being properly cleaned up
cleaned up error handling for make_connection_real function
added various null param checks
... | ... |
@@ -523,7 +523,8 @@ static void print_con_info(conn_t *conn, const char *fmt, ...) |
523 | 523 |
|
524 | 524 |
char *get_ip(const char *ip) |
525 | 525 |
{ |
526 |
- char *dupip, *p1; |
|
526 |
+ char *dupip = NULL; |
|
527 |
+ char *p1 = NULL; |
|
527 | 528 |
unsigned int i; |
528 | 529 |
|
529 | 530 |
/* |
... | ... |
@@ -552,6 +553,7 @@ char *get_ip(const char *ip) |
552 | 552 |
|
553 | 553 |
p1 = strdup(dupip + 1); |
554 | 554 |
free(dupip); |
555 |
+ dupip = NULL; |
|
555 | 556 |
return p1; |
556 | 557 |
} |
557 | 558 |
|
... | ... |
@@ -602,6 +604,10 @@ char *make_ip(const char *host, const char *port) |
602 | 602 |
size_t len; |
603 | 603 |
int ipv6; |
604 | 604 |
|
605 |
+ if (!host || !port){ |
|
606 |
+ return NULL; |
|
607 |
+ } |
|
608 |
+ |
|
605 | 609 |
len = strlen(host) + strlen(port); |
606 | 610 |
|
607 | 611 |
ipv6 = (strchr(host, ':') != NULL); |
... | ... |
@@ -622,12 +628,16 @@ static int make_connection_real(const char *soname, conn_t *conn) |
622 | 622 |
int s = -1; |
623 | 623 |
struct timeval tv; |
624 | 624 |
char *port = NULL; |
625 |
- char *pt = strdup(soname); |
|
625 |
+ char *pt = NULL; |
|
626 | 626 |
const char *host = pt; |
627 | 627 |
struct addrinfo hints, *res = NULL, *p; |
628 | 628 |
int err; |
629 |
+ int ret = 0; |
|
629 | 630 |
|
630 |
- OOM_CHECK(pt); |
|
631 |
+ if(soname) { |
|
632 |
+ pt = strdup(soname); |
|
633 |
+ OOM_CHECK(pt); |
|
634 |
+ } |
|
631 | 635 |
conn->tcp = 0; |
632 | 636 |
|
633 | 637 |
#ifndef _WIN32 |
... | ... |
@@ -637,7 +647,8 @@ static int make_connection_real(const char *soname, conn_t *conn) |
637 | 637 |
s = socket(AF_UNIX, SOCK_STREAM, 0); |
638 | 638 |
if (s < 0) { |
639 | 639 |
perror("socket"); |
640 |
- return -1; |
|
640 |
+ ret = -1; |
|
641 |
+ goto done; |
|
641 | 642 |
} |
642 | 643 |
|
643 | 644 |
memset(&addr, 0, sizeof(addr)); |
... | ... |
@@ -649,7 +660,8 @@ static int make_connection_real(const char *soname, conn_t *conn) |
649 | 649 |
if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) { |
650 | 650 |
perror("connect"); |
651 | 651 |
close(s); |
652 |
- return -1; |
|
652 |
+ ret = -1; |
|
653 |
+ goto done; |
|
653 | 654 |
} |
654 | 655 |
|
655 | 656 |
goto end; |
... | ... |
@@ -662,8 +674,10 @@ static int make_connection_real(const char *soname, conn_t *conn) |
662 | 662 |
hints.ai_flags = AI_PASSIVE; |
663 | 663 |
|
664 | 664 |
host = get_ip(soname); |
665 |
- if (!(host)) |
|
666 |
- return -1; |
|
665 |
+ if (!(host)) { |
|
666 |
+ ret = -1; |
|
667 |
+ goto done; |
|
668 |
+ } |
|
667 | 669 |
|
668 | 670 |
port = get_port(soname); |
669 | 671 |
|
... | ... |
@@ -672,7 +686,8 @@ static int make_connection_real(const char *soname, conn_t *conn) |
672 | 672 |
print_con_info(conn, "Looking up: %s:%s\n", host, port ? port : "3310"); |
673 | 673 |
if ((err = getaddrinfo(host, (port != NULL) ? port : "3310", &hints, &res))) { |
674 | 674 |
print_con_info(conn, "Could not look up %s:%s, getaddrinfo returned: %s\n", host, port ? port : "3310", gai_strerror(err)); |
675 |
- return -1; |
|
675 |
+ ret = -1; |
|
676 |
+ goto done; |
|
676 | 677 |
} |
677 | 678 |
|
678 | 679 |
for (p = res; p != NULL; p = p->ai_next) { |
... | ... |
@@ -695,36 +710,59 @@ static int make_connection_real(const char *soname, conn_t *conn) |
695 | 695 |
break; |
696 | 696 |
} |
697 | 697 |
|
698 |
- free(pt); |
|
699 | 698 |
|
700 |
- if (res) |
|
699 |
+ if (p == NULL) { |
|
700 |
+ ret = -1; |
|
701 |
+ goto done; |
|
702 |
+ } |
|
703 |
+end: |
|
704 |
+ conn->sd = s; |
|
705 |
+ gettimeofday(&conn->tv_conn, NULL); |
|
706 |
+ tv.tv_sec = 30; |
|
707 |
+ tv.tv_usec = 0; |
|
708 |
+ setsockopt(conn->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
|
709 |
+ |
|
710 |
+done: |
|
711 |
+ if (NULL != res) { |
|
701 | 712 |
freeaddrinfo(res); |
713 |
+ res = NULL; |
|
714 |
+ } |
|
702 | 715 |
|
703 |
- if (p == NULL) |
|
704 |
- return -1; |
|
716 |
+ if (NULL != pt) { |
|
717 |
+ free(pt); |
|
718 |
+ pt = NULL; |
|
719 |
+ } |
|
720 |
+ |
|
721 |
+ if (NULL != host) { |
|
722 |
+ free(host); |
|
723 |
+ host = NULL; |
|
724 |
+ } |
|
725 |
+ |
|
726 |
+ if (NULL != port) { |
|
727 |
+ free(port); |
|
728 |
+ port = NULL; |
|
729 |
+ } |
|
705 | 730 |
|
706 |
-end: |
|
707 | 731 |
if (conn->remote != soname) { |
708 | 732 |
/* when we reconnect, they are the same */ |
709 |
- if (conn->remote) |
|
733 |
+ if (NULL != conn->remote) { |
|
710 | 734 |
free(conn->remote); |
711 |
- |
|
735 |
+ conn->remote = NULL; |
|
736 |
+ } |
|
712 | 737 |
conn->remote = make_ip(host, (port != NULL) ? port : "3310"); |
713 | 738 |
} |
714 |
- if (port) |
|
715 |
- free(port); |
|
716 |
- conn->sd = s; |
|
717 |
- gettimeofday(&conn->tv_conn, NULL); |
|
718 |
- tv.tv_sec = 30; |
|
719 |
- tv.tv_usec = 0; |
|
720 |
- setsockopt(conn->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
|
721 |
- return 0; |
|
739 |
+ |
|
740 |
+ return ret; |
|
722 | 741 |
} |
723 | 742 |
|
724 | 743 |
static int make_connection(const char *soname, conn_t *conn) |
725 | 744 |
{ |
726 | 745 |
int rc; |
727 | 746 |
|
747 |
+ if (!soname) { |
|
748 |
+ return -1; |
|
749 |
+ } |
|
750 |
+ |
|
728 | 751 |
if ((rc = make_connection_real(soname, conn))) |
729 | 752 |
return rc; |
730 | 753 |
|
... | ... |
@@ -1360,10 +1398,15 @@ static void setup_connections(int argc, char *argv[]) |
1360 | 1360 |
OOM_CHECK(global.conn); |
1361 | 1361 |
|
1362 | 1362 |
for (i = 0; i < global.num_clamd; i++) { |
1363 |
- const char *soname = conn ? conn : opts->filename[i]; |
|
1363 |
+ const char *soname; |
|
1364 |
+ if (!conn && !opts->filename) { |
|
1365 |
+ soname = NULL; |
|
1366 |
+ } else { |
|
1367 |
+ soname = conn ? conn : opts->filename[i]; |
|
1368 |
+ } |
|
1364 | 1369 |
global.conn[i].line = i + 1; |
1365 | 1370 |
if (make_connection(soname, &global.conn[i]) < 0) { |
1366 |
- EXIT_PROGRAM(FAIL_INITIAL_CONN); |
|
1371 |
+ EXIT_PROGRAM(FAIL_INITIAL_CONN); |
|
1367 | 1372 |
} |
1368 | 1373 |
} |
1369 | 1374 |
|