public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/c++-coroutines] [Ada] Fix memory leak in routine Wait_On_Socket
@ 2020-07-10 20:31 Iain D Sandoe
  0 siblings, 0 replies; only message in thread
From: Iain D Sandoe @ 2020-07-10 20:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:002f8329d208e3c3f032bf2ce914a70cfe02ef9f

commit 002f8329d208e3c3f032bf2ce914a70cfe02ef9f
Author: Pascal Obry <obry@adacore.com>
Date:   Fri May 22 18:37:17 2020 +0200

    [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.

Diff:
---
 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;
 
    -----------------


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-07-10 20:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-10 20:31 [gcc/devel/c++-coroutines] [Ada] Fix memory leak in routine Wait_On_Socket Iain D Sandoe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).