From patchwork Sun Mar 1 15:03:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 444974 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 143BC14009B for ; Mon, 2 Mar 2015 19:43:35 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=lMsSS4N4; dkim-adsp=none (unprotected policy); dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:in-reply-to:references:from:date :subject:to; q=dns; s=default; b=TEy2aFtZ4AVMcXHkeBKgi3gqb+e+FEb Rs/7d2Jc4/yqmLXWz0NB+m3VyQ2dvC+oL4KLehNWvmoOteoxh6ybuT6SfMJ7xXNT YRG/Ep8rLv77pOOyD+RzuqlTWZ1Gfw5Gus4uwk05CK7cRpruMw/eu+tAa0ZwZd28 4PDZuLA2IDT4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:in-reply-to:references:from:date :subject:to; s=default; bh=fqkdaFSL4xEaniw3LbMmSCK8cmI=; b=lMsSS 4N4JSjLF7a2847PfukjTb21N1VtYu10+hra92eDNzfPg/lebSeazd8d3fdVIKUAq B5MjsfyKgXwmbK7Pk+cEsbcl3xneo6YORH2o7fKHrRFReNs5rBkp4dvMkVMdzp1k swBnE+q5gyKLrAVwDYh8KMtjrbKaKE5kLKUeoQ= Received: (qmail 91143 invoked by alias); 2 Mar 2015 08:43:11 -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 91083 invoked by uid 89); 2 Mar 2015 08:43:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL, BAYES_40, 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: <573cf09ca1e56fa3cf23dfa07c53ee9c49c7d43b.1425285061.git.fweimer@redhat.com> In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 16:03:54 +0100 Subject: [PATCH 08/25] pldd: Use struct scratch_buffer instead of extend_alloca To: libc-alpha@sourceware.org --- elf/pldd-xx.c | 32 +++++++++++++++++++++----------- elf/pldd.c | 24 ++++++++++++++++-------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/elf/pldd-xx.c b/elf/pldd-xx.c index d865739..2f19628 100644 --- a/elf/pldd-xx.c +++ b/elf/pldd-xx.c @@ -186,35 +186,43 @@ E(find_maps) (pid_t pid, void *auxv, size_t auxv_size) printf ("%lu:\t%s\n", (unsigned long int) pid, exe); /* Iterate over the list of objects and print the information. */ - size_t strsize = 256; - char *str = alloca (strsize); + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + int status = 0; do { struct E(link_map) m; if (pread64 (memfd, &m, sizeof (m), list) != sizeof (m)) { error (0, 0, gettext ("cannot read link map")); - return EXIT_FAILURE; + status = EXIT_FAILURE; + goto out; } EW(Addr) name_offset = m.l_name; again: while (1) { - ssize_t n = pread64 (memfd, str, strsize, name_offset); + ssize_t n = pread64 (memfd, tmpbuf.data, tmpbuf.length, name_offset); if (n == -1) { error (0, 0, gettext ("cannot read object name")); - return EXIT_FAILURE; + status = EXIT_FAILURE; + goto out; } - if (memchr (str, '\0', n) != NULL) + if (memchr (tmpbuf.data, '\0', n) != NULL) break; - str = extend_alloca (str, strsize, strsize * 2); + if (!scratch_buffer_grow (&tmpbuf)) + { + error (0, 0, gettext ("cannot allocate buffer for object name")); + status = EXIT_FAILURE; + goto out; + } } - if (str[0] == '\0' && name_offset == m.l_name + if (((char *)tmpbuf.data)[0] == '\0' && name_offset == m.l_name && m.l_libname != 0) { /* Try the l_libname element. */ @@ -227,14 +235,16 @@ E(find_maps) (pid_t pid, void *auxv, size_t auxv_size) } /* Skip over the executable. */ - if (str[0] != '\0') - printf ("%s\n", str); + if (((char *)tmpbuf.data)[0] != '\0') + printf ("%s\n", (char *)tmpbuf.data); list = m.l_next; } while (list != 0); - return 0; + out: + scratch_buffer_free (&tmpbuf); + return status; } diff --git a/elf/pldd.c b/elf/pldd.c index 9e1d822..2b86224 100644 --- a/elf/pldd.c +++ b/elf/pldd.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -118,18 +119,25 @@ main (int argc, char *argv[]) if (dfd == -1) error (EXIT_FAILURE, errno, gettext ("cannot open %s"), buf); - size_t exesize = 1024; -#ifdef PATH_MAX - exesize = PATH_MAX; -#endif - exe = alloca (exesize); + struct scratch_buffer exebuf; + scratch_buffer_init (&exebuf); ssize_t nexe; - while ((nexe = readlinkat (dfd, "exe", exe, exesize)) == exesize) - extend_alloca (exe, exesize, 2 * exesize); + while ((nexe = readlinkat (dfd, "exe", + exebuf.data, exebuf.length)) == exebuf.length) + { + if (!scratch_buffer_grow (&exebuf)) + { + nexe = -1; + break; + } + } if (nexe == -1) exe = (char *) ""; else - exe[nexe] = '\0'; + { + exe = exebuf.data; + exe[nexe] = '\0'; + } /* Stop all threads since otherwise the list of loaded modules might change while we are reading it. */