From 2acebe64013101e9e85c596b49bcdde0ecb05a82 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Thu, 13 Apr 2017 18:31:00 +0300 Subject: [PATCH] Fix crash in FirstDLOpenedLinkMap if app linked statically (Alpine Linux) 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 | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/dyn_load.c b/dyn_load.c index 5a3d7087..f327cddf 100644 --- a/dyn_load.c +++ b/dyn_load.c @@ -188,13 +188,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; } } @@ -711,10 +714,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; } } -- 2.40.0