aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPascal Obry <obry@adacore.com>2020-05-22 18:37:17 +0200
committerGiuliano Belinassi <giuliano.belinassi@usp.br>2020-08-17 13:17:12 -0300
commit8abff83e28a4efdaf625c1aa3d690b30dbb20694 (patch)
tree002d03c719d7a1ca297b3aad35f6cb9afbc3126c /gcc
parent0a8b2434df01fde5acedb208b33921f4dfe62450 (diff)
downloadgcc-8abff83e28a4efdaf625c1aa3d690b30dbb20694.zip
gcc-8abff83e28a4efdaf625c1aa3d690b30dbb20694.tar.gz
gcc-8abff83e28a4efdaf625c1aa3d690b30dbb20694.tar.bz2
[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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ada/libgnat/g-socket.adb22
1 files changed, 15 insertions, 7 deletions
diff --git a/gcc/ada/libgnat/g-socket.adb b/gcc/ada/libgnat/g-socket.adb
index 8f65aea..da631e3 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;
-----------------