From patchwork Sun Mar 1 15:03:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 5397 Received: (qmail 87922 invoked by alias); 2 Mar 2015 08:42:31 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 87856 invoked by uid 89); 2 Mar 2015 08:42:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.0 required=5.0 tests=AWL, BAYES_00, DATE_IN_PAST_12_24, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD, UNWANTED_LANGUAGE_BODY autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-Id: In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 16:03:01 +0100 Subject: [PATCH 07/25] _dl_map_object_deps: Use struct scratch_buffer instead of extend_alloca To: libc-alpha@sourceware.org The function comment suggests that _dl_map_object_deps cannot use malloc, but it already allocates the l_initfini array on the heap, so the additional allocation should be acceptable. --- elf/dl-deps.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/elf/dl-deps.c b/elf/dl-deps.c index eee146a..a614ac8 100644 --- a/elf/dl-deps.c +++ b/elf/dl-deps.c @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -184,9 +185,8 @@ _dl_map_object_deps (struct link_map *map, /* Pointer to last unique object. */ tail = &known[nlist - 1]; - /* No alloca'd space yet. */ - struct link_map **needed_space = NULL; - size_t needed_space_bytes = 0; + struct scratch_buffer needed_space; + scratch_buffer_init (&needed_space); /* Process each element of the search list, loading each of its auxiliary objects and immediate dependencies. Auxiliary objects @@ -217,13 +217,12 @@ _dl_map_object_deps (struct link_map *map, if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL && l != map && l->l_ldnum > 0) { - size_t new_size = l->l_ldnum * sizeof (struct link_map *); - - if (new_size > needed_space_bytes) - needed_space - = extend_alloca (needed_space, needed_space_bytes, new_size); - - needed = needed_space; + /* l->l_ldnum includes space for the terminating NULL. */ + if (!scratch_buffer_set_array_size + (&needed_space, l->l_ldnum, sizeof (struct link_map *))) + _dl_signal_error (ENOMEM, map->l_name, NULL, + N_("cannot allocate dependency buffer")); + needed = needed_space.data; } if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG]) @@ -463,8 +462,11 @@ _dl_map_object_deps (struct link_map *map, struct link_map **l_initfini = (struct link_map **) malloc ((2 * nneeded + 1) * sizeof needed[0]); if (l_initfini == NULL) - _dl_signal_error (ENOMEM, map->l_name, NULL, - N_("cannot allocate dependency list")); + { + scratch_buffer_free (&needed_space); + _dl_signal_error (ENOMEM, map->l_name, NULL, + N_("cannot allocate dependency list")); + } l_initfini[0] = l; memcpy (&l_initfini[1], needed, nneeded * sizeof needed[0]); memcpy (&l_initfini[nneeded + 1], l_initfini, @@ -482,6 +484,8 @@ _dl_map_object_deps (struct link_map *map, } out: + scratch_buffer_free (&needed_space); + if (errno == 0 && errno_saved != 0) __set_errno (errno_saved);