[adasockets] thread safety fix

Dmitriy Anisimkov anisimkov at ada-ru.org
Tue Feb 1 15:05:30 CET 2005


Sam,

We should unlock Naming_Lock only after complete using "Res" access 
variable, because thread unsafety is because of
C_Gethostbyname and C_Gethostbyaddr returning access to the internal 
static buffer. It is a common C thread unsafety.

fix insufficient locking for thread safety around C_Gethostbyname and 
C_Gethostbyaddr is

sh-2.04$ diff -u -b sockets-naming.adb src/sockets-naming.adb
--- sockets-naming.adb  Tue Aug 10 21:30:50 2004
+++ src/sockets-naming.adb      Tue Feb  1 19:51:23 2005
@@ -302,14 +302,15 @@
    begin
       Naming_Lock.Lock;
       Res := C_Gethostbyname (C_Name);
-      Naming_Lock.Unlock;
       Free (C_Name);
       if Res = null then
+         Naming_Lock.Unlock;
          Raise_Naming_Error (Errno, Name);
       end if;
       declare
          Result : constant Host_Entry := Parse_Entry (Res.all);
       begin
+         Naming_Lock.Unlock;
          return Result;
       end;
    end Info_Of;
@@ -332,13 +333,14 @@
       Res := C_Gethostbyaddr (C_Addr,
                               int (Temp'Size / CHAR_BIT),
                               Constants.Af_Inet);
-      Naming_Lock.Unlock;
       if Res = null then
+         Naming_Lock.Unlock;
          Raise_Naming_Error (Errno, Image (Addr));
       end if;
       declare
          Result : constant Host_Entry := Parse_Entry (Res.all);
       begin
+         Naming_Lock.Unlock;
          return Result;
       end;
    end Info_Of;


More information about the AdaSockets mailing list