Fix crash in FirstDLOpenedLinkMap if app linked statically (Alpine Linux)
authorRyan Gonzalez <rymg19@gmail.com>
Thu, 13 Apr 2017 15:31:00 +0000 (18:31 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 13 Apr 2017 15:31:00 +0000 (18:31 +0300)
Issue #154 (bdwgc).

* dyn_load.c [SOLARISDL && !USE_PROC_FOR_LIBRARIES]
(GC_FirstDLOpenedLinkMap): Do not dereference d_un.d_ptr if it is null.
* dyn_load.c [(SCO_ELF || DGUX || HURD || LINUX || FREEBSD || NACL
|| NETBSD || OPENBSD) && !USE_PROC_FOR_LIBRARIES]
(GC_FirstDLOpenedLinkMap): Likewise.

dyn_load.c

index 69f637c..1c50f90 100644 (file)
@@ -185,13 +185,16 @@ GC_FirstDLOpenedLinkMap(void)
         /* _DYNAMIC symbol not resolved. */
         return(0);
     }
-    if( cachedResult == 0 ) {
+    if (cachedResult == 0) {
         int tag;
         for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
-            if( tag == DT_DEBUG ) {
-                struct link_map *lm
-                        = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
-                if( lm != 0 ) cachedResult = lm->l_next; /* might be NULL */
+            if (tag == DT_DEBUG) {
+                struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr;
+                if (rd != NULL) {
+                    struct link_map *lm = rd->r_map;
+                    if (lm != NULL)
+                        cachedResult = lm->l_next; /* might be NULL */
+                }
                 break;
             }
         }
@@ -713,10 +716,14 @@ GC_FirstDLOpenedLinkMap(void)
         int tag;
 
         for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
-            if( tag == DT_DEBUG ) {
-                struct link_map *lm
-                        = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
-                if( lm != 0 ) cachedResult = lm->l_next; /* might be NULL */
+            if (tag == DT_DEBUG) {
+                struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr;
+                /* d_ptr could be null if libs are linked statically. */
+                if (rd != NULL) {
+                    struct link_map *lm = rd->r_map;
+                    if (lm != NULL)
+                        cachedResult = lm->l_next; /* might be NULL */
+                }
                 break;
             }
         }