Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

Started by Ranier Vilelaalmost 2 years ago6 messages
#1Ranier Vilela
ranier.vf@gmail.com
1 attachment(s)

Hi.

While there are plans to remove the sockets functions (Windows) [1]Re: Windows sockets (select missing events?) </messages/by-id/CA+hUKGKSLgxFhSP8+dqQqHsuZvrRRU3wZ6ytLOcno-mdGvckHg@mail.gmail.com&gt;, I
believe it is worth fixing possible current bugs.

In the pgwin32_socket function (src/backend/port/win32/socket.c), there is
a possible socket leak if the socket cannot be made non-blocking.

Trivial patch attached.

Best regards,
Ranier Vilela

[1]: Re: Windows sockets (select missing events?) </messages/by-id/CA+hUKGKSLgxFhSP8+dqQqHsuZvrRRU3wZ6ytLOcno-mdGvckHg@mail.gmail.com&gt;
</messages/by-id/CA+hUKGKSLgxFhSP8+dqQqHsuZvrRRU3wZ6ytLOcno-mdGvckHg@mail.gmail.com&gt;

Attachments:

fix-socket-leak-windows.patchapplication/octet-stream; name=fix-socket-leak-windows.patchDownload
diff --git a/src/backend/port/win32/socket.c b/src/backend/port/win32/socket.c
index 6b60a9f220..0a819e07ae 100644
--- a/src/backend/port/win32/socket.c
+++ b/src/backend/port/win32/socket.c
@@ -303,6 +303,7 @@ pgwin32_socket(int af, int type, int protocol)
 	if (ioctlsocket(s, FIONBIO, &on))
 	{
 		TranslateSocketError();
+		closesocket(s);
 		return INVALID_SOCKET;
 	}
 	errno = 0;
#2Daniel Gustafsson
daniel@yesql.se
In reply to: Ranier Vilela (#1)
Re: Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

On 13 Jan 2024, at 22:38, Ranier Vilela <ranier.vf@gmail.com> wrote:

In the pgwin32_socket function (src/backend/port/win32/socket.c), there is a possible socket leak if the socket cannot be made non-blocking.

I don't know Windows well enough to comment on the implications of not calling
closesocket here, but it definitely seems like a prudent thing to do
backpatched down to 12. Unless objections I'll do that.

--
Daniel Gustafsson

#3Ranier Vilela
ranier.vf@gmail.com
In reply to: Daniel Gustafsson (#2)
Re: Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

Em seg., 15 de jan. de 2024 às 09:43, Daniel Gustafsson <daniel@yesql.se>
escreveu:

On 13 Jan 2024, at 22:38, Ranier Vilela <ranier.vf@gmail.com> wrote:

In the pgwin32_socket function (src/backend/port/win32/socket.c), there

is a possible socket leak if the socket cannot be made non-blocking.

I don't know Windows well enough to comment on the implications of not
calling
closesocket here, but it definitely seems like a prudent thing to do
backpatched down to 12. Unless objections I'll do that.

Thanks for taking care of this.
Do you have plans or should I register for a commitfest?

Best regards,
Ranier Vilela

#4Michael Paquier
michael@paquier.xyz
In reply to: Ranier Vilela (#3)
Re: Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

On Tue, Jan 16, 2024 at 05:25:39PM -0300, Ranier Vilela wrote:

Thanks for taking care of this.

Yeah, that's a good catch.

Do you have plans or should I register for a commitfest?

Daniel has stated that he would take care of it, so why not letting
him a few days? I don't think that a CF entry is necessary.
--
Michael

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Michael Paquier (#4)
Re: Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

On 17 Jan 2024, at 07:26, Michael Paquier <michael@paquier.xyz> wrote:
On Tue, Jan 16, 2024 at 05:25:39PM -0300, Ranier Vilela wrote:

Do you have plans or should I register for a commitfest?

Daniel has stated that he would take care of it, so why not letting
him a few days? I don't think that a CF entry is necessary.

It isn't, I've now committed it backpatched down to 12.

--
Daniel Gustafsson

#6Ranier Vilela
ranier.vf@gmail.com
In reply to: Daniel Gustafsson (#5)
Re: Fix a possible socket leak at Windows (src/backend/port/win32/socket.c)

Em qua., 17 de jan. de 2024 09:54, Daniel Gustafsson <daniel@yesql.se>
escreveu:

On 17 Jan 2024, at 07:26, Michael Paquier <michael@paquier.xyz> wrote:
On Tue, Jan 16, 2024 at 05:25:39PM -0300, Ranier Vilela wrote:

Do you have plans or should I register for a commitfest?

Daniel has stated that he would take care of it, so why not letting
him a few days? I don't think that a CF entry is necessary.

It isn't, I've now committed it backpatched down to 12.

Thanks for the commit, Daniel.

Best regards,
Ranier Vilela