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 D86B53857432 for ; Mon, 6 Sep 2021 10:55:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D86B53857432 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 532FF72C8B0 for ; Mon, 6 Sep 2021 13:55:35 +0300 (MSK) Received: by mua.local.altlinux.org (Postfix, from userid 508) id 361147CF776; Mon, 6 Sep 2021 13:55:35 +0300 (MSK) Date: Mon, 6 Sep 2021 10:00:00 +0000 From: "Dmitry V. Levin" To: elfutils-devel@sourceware.org Subject: [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE) Message-ID: <20210906100000.GB13778@altlinux.org> References: <20210906100000.GA13778@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210906100000.GA13778@altlinux.org> 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 10:55:37 -0000 Signed-off-by: Dmitry V. Levin --- lib/ChangeLog | 3 +++ lib/color.c | 6 ++---- src/ChangeLog | 7 +++++++ src/objdump.c | 17 ++++++++--------- src/readelf.c | 14 ++++++-------- src/unstrip.c | 8 ++------ 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 59d1d51c..8e630417 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,8 @@ 2021-09-06 Dmitry V. Levin + * color.c (parse_opt): Replace asprintf followed by error(EXIT_FAILURE) + with xasprintf. + * xasprintf.c: New file. * Makefile.am (libeu_a_SOURCES): Add it. * libeu.h (xasprintf): New prototype. diff --git a/lib/color.c b/lib/color.c index 454cb7ca..e43b6143 100644 --- a/lib/color.c +++ b/lib/color.c @@ -188,10 +188,8 @@ valid arguments are:\n\ if (name_len == known[i].len && memcmp (start, known[i].name, name_len) == 0) { - if (asprintf (known[i].varp, "\e[%.*sm", - (int) (env - val), val) < 0) - error (EXIT_FAILURE, errno, - _("cannot allocate memory")); + *known[i].varp = + xasprintf ("\e[%.*sm", (int) (env - val), val); break; } } diff --git a/src/ChangeLog b/src/ChangeLog index b729eaa4..297627df 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2021-09-06 Dmitry V. Levin + + * objdump.c (show_disasm): Replace asprintf followed by + error(EXIT_FAILURE) with xasprintf. + * readelf.c (handle_gnu_hash): Likewise. + * unstrip.c (handle_output_dir_module, main): Likewise. + 2021-08-20 Saleem Abdulrasool * elfclassify.c: Remove error.h include. diff --git a/src/objdump.c b/src/objdump.c index 3a93248c..f7ea6c92 100644 --- a/src/objdump.c +++ b/src/objdump.c @@ -717,15 +717,14 @@ show_disasm (Ebl *ebl, const char *fname, uint32_t shstrndx) info.address_color = color_address; info.bytes_color = color_bytes; - if (asprintf (&fmt, "%s%%7m %s%%.1o,%s%%.2o,%s%%.3o,,%s%%.4o%s%%.5o%%34a %s%%l", - color_mnemonic ?: "", - color_operand1 ?: "", - color_operand2 ?: "", - color_operand3 ?: "", - color_operand4 ?: "", - color_operand5 ?: "", - color_label ?: "") < 0) - error (EXIT_FAILURE, errno, _("cannot allocate memory")); + fmt = xasprintf ("%s%%7m %s%%.1o,%s%%.2o,%s%%.3o,,%s%%.4o%s%%.5o%%34a %s%%l", + color_mnemonic ?: "", + color_operand1 ?: "", + color_operand2 ?: "", + color_operand3 ?: "", + color_operand4 ?: "", + color_operand5 ?: "", + color_label ?: ""); } else { diff --git a/src/readelf.c b/src/readelf.c index 8191bde2..80b40918 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -3448,17 +3448,15 @@ handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx) nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff); } - char *str; - if (unlikely (asprintf (&str, _("\ + char *str = xasprintf (_("\ Symbol Bias: %u\n\ Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"), - (unsigned int) symbias, - bitmask_words * sizeof (Elf32_Word), - ((nbits * 100 + 50) - / (uint_fast32_t) (bitmask_words + (unsigned int) symbias, + bitmask_words * sizeof (Elf32_Word), + ((nbits * 100 + 50) + / (uint_fast32_t) (bitmask_words * sizeof (Elf32_Word) * 8)), - (unsigned int) shift) == -1)) - error (EXIT_FAILURE, 0, _("memory exhausted")); + (unsigned int) shift); print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms, lengths, str); diff --git a/src/unstrip.c b/src/unstrip.c index 6618ec9b..aacc9aad 100644 --- a/src/unstrip.c +++ b/src/unstrip.c @@ -2401,9 +2401,7 @@ handle_output_dir_module (const char *output_dir, Dwfl_Module *mod, bool force, if (file == NULL && ignore) return; - char *output_file; - if (asprintf (&output_file, "%s/%s", output_dir, modnames ? name : file) < 0) - error (EXIT_FAILURE, 0, _("memory exhausted")); + char *output_file = xasprintf ("%s/%s", output_dir, modnames ? name : file); handle_dwfl_module (output_file, true, force, mod, all, ignore, relocate); @@ -2606,9 +2604,7 @@ or - if no debuginfo was found, or . if FILE contains the debug information.\ if (info.output_dir != NULL) { - char *file; - if (asprintf (&file, "%s/%s", info.output_dir, info.args[0]) < 0) - error (EXIT_FAILURE, 0, _("memory exhausted")); + char *file = xasprintf ("%s/%s", info.output_dir, info.args[0]); handle_explicit_files (file, true, info.force, info.args[0], info.args[1]); free (file); -- ldv