From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from vmicros1.altlinux.org (vmicros1.altlinux.org [194.107.17.57]) by sourceware.org (Postfix) with ESMTP id 05B043839416 for ; Mon, 6 Sep 2021 18:19:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 05B043839416 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=altlinux.org Received: from mua.local.altlinux.org (mua.local.altlinux.org [192.168.1.14]) by vmicros1.altlinux.org (Postfix) with ESMTP id 7F84F72C8D0 for ; Mon, 6 Sep 2021 21:19:35 +0300 (MSK) Received: by mua.local.altlinux.org (Postfix, from userid 508) id 631CE7CF776; Mon, 6 Sep 2021 21:19:35 +0300 (MSK) Date: Mon, 6 Sep 2021 18:00:00 +0000 From: "Dmitry V. Levin" To: elfutils-devel@sourceware.org Subject: [PATCH] findtextrel: do not use unbound alloca Message-ID: <20210906180000.GA19040@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Sep 2021 18:19:37 -0000 This fixes the following compilation warning: findtextrel.c:184:1: warning: stack usage might be unbounded [-Wstack-usage=] Signed-off-by: Dmitry V. Levin --- src/ChangeLog | 5 +++++ src/Makefile.am | 1 - src/findtextrel.c | 31 +++++++++---------------------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 297627df..2238916f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,10 @@ 2021-09-06 Dmitry V. Levin + * findtextrel.c: Include "libeu.h". + (process_file): Use xasprintf instead of alloca followed by series + of mempcpy and stpcpy. + * Makefile.am (findtextrel_no_Wstack_usage): Remove. + * objdump.c (show_disasm): Replace asprintf followed by error(EXIT_FAILURE) with xasprintf. * readelf.c (handle_gnu_hash): Likewise. diff --git a/src/Makefile.am b/src/Makefile.am index 88d0ac8f..ee695d5d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -61,7 +61,6 @@ nm_no_Wstack_usage = yes size_no_Wstack_usage = yes strip_no_Wstack_usage = yes elflint_no_Wstack_usage = yes -findtextrel_no_Wstack_usage = yes elfcmp_no_Wstack_usage = yes objdump_no_Wstack_usage = yes ranlib_no_Wstack_usage = yes diff --git a/src/findtextrel.c b/src/findtextrel.c index 220ee909..4bee80ae 100644 --- a/src/findtextrel.c +++ b/src/findtextrel.c @@ -36,6 +36,7 @@ #include #include +#include "libeu.h" #include "system.h" struct segments @@ -185,25 +186,17 @@ process_file (const char *fname, bool more_than_one) { int result = 0; void *knownsrcs = NULL; + char *new_fname = NULL; - size_t fname_len = strlen (fname); - size_t rootdir_len = strlen (rootdir); const char *real_fname = fname; if (fname[0] == '/' && (rootdir[0] != '/' || rootdir[1] != '\0')) - { - /* Prepend the user-provided root directory. */ - char *new_fname = alloca (rootdir_len + fname_len + 2); - *((char *) mempcpy (stpcpy (mempcpy (new_fname, rootdir, rootdir_len), - "/"), - fname, fname_len)) = '\0'; - real_fname = new_fname; - } + real_fname = new_fname = xasprintf ("%s/%s", rootdir, fname); int fd = open (real_fname, O_RDONLY); if (fd == -1) { error (0, errno, _("cannot open '%s'"), fname); - return 1; + goto err_free; } Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); @@ -225,6 +218,8 @@ process_file (const char *fname, bool more_than_one) elf_end (elf); err_close: close (fd); + err_free: + free (new_fname); return 1; } @@ -362,18 +357,10 @@ cannot get program header index at offset %zd: %s"), is specified with an absolute path. */ if (dw == NULL && fname[0] == '/') { - size_t debuginfo_rootlen = strlen (debuginfo_root); - char *difname = (char *) alloca (rootdir_len + debuginfo_rootlen - + fname_len + 8); - strcpy (mempcpy (stpcpy (mempcpy (mempcpy (difname, rootdir, - rootdir_len), - debuginfo_root, - debuginfo_rootlen), - "/"), - fname, fname_len), - ".debug"); - + char *difname = + xasprintf("%s%s/%s.debug", rootdir, debuginfo_root, fname); fd2 = open (difname, O_RDONLY); + free (difname); if (fd2 != -1 && (elf2 = elf_begin (fd2, ELF_C_READ_MMAP, NULL)) != NULL) dw = dwarf_begin_elf (elf2, DWARF_C_READ, NULL); -- ldv