Browse code

cdp: close socket in case of error

(cherry picked from commit 993c036bb185f6053a3a2f0486be30398918a87c)

Daniel-Constantin Mierla authored on 04/05/2022 12:55:01
Showing 1 changed files
... ...
@@ -843,7 +843,7 @@ drop_peer:
843 843
  */
844 844
 int peer_connect(peer *p)
845 845
 {
846
-	int sock;
846
+	int sock = -1;
847 847
 	int tmp = 0;
848 848
 	unsigned int option = 1;
849 849
 
... ...
@@ -921,17 +921,20 @@ int peer_connect(peer *p)
921 921
 						if (valopt) {
922 922
 							LM_ERR("peer_connect(): Error opening connection to to %s port %s >%s\n",host,serv,strerror(valopt));
923 923
 							close(sock);
924
+							sock = -1;
924 925
 							continue;
925 926
 						}
926 927
 					}else{
927 928
 						LM_ERR("peer_connect(): Timeout or error opening connection to to %s port %s >%s\n",host,serv,strerror(errno));
928 929
 						close(sock);
930
+						sock = -1;
929 931
 						continue;
930 932
 					}
931 933
 				}
932 934
 			}else{
933 935
 				LM_ERR("peer_connect(): Error opening connection to to %s port %s >%s\n",host,serv,strerror(errno));
934 936
 				close(sock);
937
+				sock = -1;
935 938
 				continue;
936 939
 			}
937 940
 
... ...
@@ -955,7 +958,6 @@ int peer_connect(peer *p)
955 958
 
956 959
 		if (!send_fd(p->fd_exchange_pipe,sock,p)){
957 960
 			LM_ERR("peer_connect(): [%.*s] Error sending fd to respective receiver\n",p->fqdn.len,p->fqdn.s);
958
-			close(sock);
959 961
 			goto error;
960 962
 		}
961 963
 
... ...
@@ -966,6 +968,7 @@ int peer_connect(peer *p)
966 968
 error:
967 969
 	if (res) freeaddrinfo(res);
968 970
 	if (sainfo) freeaddrinfo(sainfo);
971
+	if (sock!=-1) close(sock);
969 972
 	return -1;
970 973
 }
971 974