From 002f8329d208e3c3f032bf2ce914a70cfe02ef9f Mon Sep 17 00:00:00 2001 From: Pascal Obry Date: Fri, 22 May 2020 18:37:17 +0200 Subject: [PATCH] [Ada] Fix memory leak in routine Wait_On_Socket gcc/ada/ * libgnat/g-socket.adb (Wait_On_Socket): Fix memory leaks and file descriptor leaks. A memory leak was created each time the routine was called without a selector (Selector = Null). Also, in case of exception in the routine a memory leak and descriptor leak was created as the created file selector was not closed. --- gcc/ada/libgnat/g-socket.adb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/gcc/ada/libgnat/g-socket.adb b/gcc/ada/libgnat/g-socket.adb index 8f65aea6e91..da631e381dc 100644 --- a/gcc/ada/libgnat/g-socket.adb +++ b/gcc/ada/libgnat/g-socket.adb @@ -2028,7 +2028,11 @@ package body GNAT.Sockets is type Local_Selector_Access is access Selector_Type; for Local_Selector_Access'Storage_Size use Selector_Type'Size; - S : Selector_Access; + procedure Unchecked_Free is new Ada.Unchecked_Deallocation + (Selector_Type, Local_Selector_Access); + + Local_S : Local_Selector_Access; + S : Selector_Access; -- Selector to use for waiting R_Fd_Set : Socket_Set_Type; @@ -2038,12 +2042,9 @@ package body GNAT.Sockets is -- Create selector if not provided by the user if Selector = null then - declare - Local_S : constant Local_Selector_Access := new Selector_Type; - begin - S := Local_S.all'Unchecked_Access; - Create_Selector (S.all); - end; + Local_S := new Selector_Type; + S := Local_S.all'Unchecked_Access; + Create_Selector (S.all); else S := Selector.all'Access; @@ -2059,7 +2060,14 @@ package body GNAT.Sockets is if Selector = null then Close_Selector (S.all); + Unchecked_Free (Local_S); end if; + exception + when others => + if Selector = null then + Close_Selector (S.all); + Unchecked_Free (Local_S); + end if; end Wait_On_Socket; ----------------- -- 2.30.2