Browse code

cdp: close socket in case of error

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