From patchwork Thu Oct 24 10:07:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 99481 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 94AA4385840A for ; Thu, 24 Oct 2024 10:08:21 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id E13E03858D21 for ; Thu, 24 Oct 2024 10:07:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E13E03858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E13E03858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729764470; cv=none; b=VjcK3mm6UglrNr1b6aV0T1uj1QiPpX6Bi1tQKuWiOr5swl/ffPmBS8A01fvSgdqClqR4j0va1jz90BiTl8i1dJw7aVmi9pHpxdNM2xap+EBph0pp7cBwH53gpr+aPjnicvOPga0MCne+OrFvARmc3+WyMkiCoTNT4lm0Jwsaikg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729764470; c=relaxed/simple; bh=gTuDfPqdosh1w5i+qlN9yUdXSGIwbTDZC372WFFca/0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=jqCyG7qtUv1aZ0siFdICyhozix80WFmgXIQMWdiugwxQOyVMGh3dTRZz8EARUujDsMo3Wf2fRTDVX5ZGfUnwH5962/ow+ZlJvmVXZ0bqhGaVYtcharyUGAT1zFvSVLVJNdBinX9Bg9ZqD5Oizn2VgVi7vfscEC9N/v/FeDv5dGg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-71f5208217dso456679b3a.2 for ; Thu, 24 Oct 2024 03:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729764465; x=1730369265; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=tw+zRSUwtE9QSPzn+FVeRCQbc6g4W+XtuqkrT7lM/e8=; b=XpWni48LMI4vAG8OSQzTZGMV1CVlfAeTPqql1IEKsFbAVicM6FaVjkeSqiY3Hdua2p J/WgYD9JEc7Ydn5HvlXOEtmJoa4cDEQrkj7w8DtuOMePYs0zglMKSfSh3xd3K9FL16n7 CH+yFHAUuJgx8C8Vmc1IQKeBdqMWZbK5doRc/YyovgKeq+DLlFL1eC3hPnohyZuqsqOU 8uhPBfhMCJVlXDRmzCWc3kLVy+bWTTc7UYmnsCbSG0UF7uuhPCdCgUsfFZiWbuNEdQwN BRFQreve4BdBKjdg1cVAdclZ9PGiiYmMKG1ouibdxJion7hJYmNXR5lKdYahp6qEaa1k kkPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729764465; x=1730369265; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tw+zRSUwtE9QSPzn+FVeRCQbc6g4W+XtuqkrT7lM/e8=; b=U9qbuOjrz4eBx+NuZdtEIELLXFRt3FkDbEcp8jer7e/Zi/6sQOAVg+2IbecCNTmxCj SUBF/1yAQjDRzSyebZ5ITgc16Z6asZ7KGHOUORwl/zMpjHOpXPQcwXahmIGwZVTqARlo egEG6tq3Rgk6BDYDNyzOe+DterEmc7JQgFNEQ1qr9JPuv7bakI8TToSnnK8fmwuiToH5 8UnSiuq6j9xxpJrzSc152cW/Ckqsu9UYtfoShv0wE2wTC/IFUoRk1YCcdag1Lt5lRMHf RB9PObSR5LvPU091HEQVwD1Aa5o6rpQ6qUCiPQA+MmzMYl4wcUUNmKNpr3SHrwIdALkx pIrA== X-Gm-Message-State: AOJu0YzaXAC/RUdQVH0irkDsk/tUbnkNLseweho6qI36pZQbp2jj67VC zUILi/WD3R6/VOOMababE0kstFi5yRsu6Qh71Uedj/uTtjyeKk9TMsy2WQ== X-Google-Smtp-Source: AGHT+IFf4+6wPbtNRrXP83rTbxYkJhm8wO8IzKQIhOgTMlJkiw0Q6c9zrGZumO8ANJJCoHHCAKFHYA== X-Received: by 2002:a05:6a21:1707:b0:1d9:386b:5320 with SMTP id adf61e73a8af0-1d989d03c6amr1452961637.48.1729764465063; Thu, 24 Oct 2024 03:07:45 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec1312e2bsm7634703b3a.42.2024.10.24.03.07.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 03:07:44 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id E80FF114010A; Thu, 24 Oct 2024 20:37:41 +1030 (ACDT) Date: Thu, 24 Oct 2024 20:37:41 +1030 From: Alan Modra To: binutils@sourceware.org Subject: Replace uses of asprintf with xasprintf Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org xasprintf has a nicer interface and behaves like xmalloc as far as memory is concerned, ie. no need to check a return status and the program exits with an error on OOM. binutils/ * dwarf.c (load_debug_sup_file): Replace asprintf with xasprintf. * nm.c (get_elf_symbol_type, get_coff_symbol_type): Likewise. * objdump.c (dump_ctf_indent_lines): Likewise. * readelf.c (display_lto_symtab, dump_ctf_indent_lines): Likewise. * windres.c (main): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate. gas/ * config/tc-kvx.c (kvx_emit_single_noop): Simplify. * config/tc-riscv.c (md_assemblef): Replace asprintf with xasprintf. * read.c (s_nop, do_s_func): Likewise. * stabs.c (stabs_generate_asm_func): Likewise. (stabs_generate_asm_endfunc): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate. ld/ * ldlang.c (lang_leave_overlay_section): Replace xmalloc+sprintf with xasprintf. Localise vars. * lexsup.c (parse_args): Replace asprintf with xasprintf. * pe-dll.c (make_head, make_tail, make_one): Likewise. (make_singleton_name_thunk, make_import_fixup_entry): Likewise. (make_runtime_pseudo_reloc): Likewise. (pe_create_runtime_relocator_reference): Likewise. * configure.ac: Remove asprintf from AC_CHECK_DECLS. * config.in: Regenerate. * configure: Regenerate. diff --git a/binutils/config.in b/binutils/config.in index ee148c756f4..93ff04667fe 100644 --- a/binutils/config.in +++ b/binutils/config.in @@ -50,10 +50,6 @@ */ #undef HAVE_DCGETTEXT -/* Define to 1 if you have the declaration of `asprintf', and to 0 if you - don't. */ -#undef HAVE_DECL_ASPRINTF - /* Define to 1 if you have the declaration of `environ', and to 0 if you don't. */ #undef HAVE_DECL_ENVIRON diff --git a/binutils/configure b/binutils/configure index ac3de5e39a8..41e41c61efe 100755 --- a/binutils/configure +++ b/binutils/configure @@ -15708,16 +15708,6 @@ $as_echo "#define HAVE_GOOD_UTIME_H 1" >>confdefs.h fi -ac_fn_c_check_decl "$LINENO" "asprintf" "ac_cv_have_decl_asprintf" "$ac_includes_default" -if test "x$ac_cv_have_decl_asprintf" = xyes; then : - ac_have_decl=1 -else - ac_have_decl=0 -fi - -cat >>confdefs.h <<_ACEOF -#define HAVE_DECL_ASPRINTF $ac_have_decl -_ACEOF ac_fn_c_check_decl "$LINENO" "environ" "ac_cv_have_decl_environ" "$ac_includes_default" if test "x$ac_cv_have_decl_environ" = xyes; then : ac_have_decl=1 diff --git a/binutils/configure.ac b/binutils/configure.ac index 06c88e0048c..31fab69fd0f 100644 --- a/binutils/configure.ac +++ b/binutils/configure.ac @@ -279,7 +279,7 @@ if test $bu_cv_header_utime_h = yes; then AC_DEFINE(HAVE_GOOD_UTIME_H, 1, [Does define struct utimbuf?]) fi -AC_CHECK_DECLS([asprintf, environ, getc_unlocked, stpcpy, strnlen]) +AC_CHECK_DECLS([environ, getc_unlocked, stpcpy, strnlen]) # Link in zlib/zstd if we can. This allows us to read compressed debug # sections. This is used only by readelf.c (objdump uses bfd for diff --git a/binutils/dwarf.c b/binutils/dwarf.c index 424353ccdb2..4b46100c753 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -12318,34 +12318,14 @@ load_debug_sup_file (const char * main_filename, void * file) } if (filename[0] != '/' && strchr (main_filename, '/')) - { - char * new_name; - int new_len; - - new_len = asprintf (& new_name, "%.*s/%s", + filename = xasprintf ("%.*s/%s", (int) (strrchr (main_filename, '/') - main_filename), main_filename, filename); - if (new_len < 3) - { - warn (_("unable to construct path for supplementary debug file\n")); - if (new_len > -1) - free (new_name); - return; - } - filename = new_name; - } else - { - /* PR 27796: Make sure that we pass a filename that can be free'd to - add_separate_debug_file(). */ - filename = strdup (filename); - if (filename == NULL) - { - warn (_("out of memory constructing filename for .debug_sup link\n")); - return; - } - } + /* PR 27796: Make sure that we pass a filename that can be free'd to + add_separate_debug_file(). */ + filename = xstrdup (filename); void * handle = open_debug_file (filename); if (handle == NULL) diff --git a/binutils/nm.c b/binutils/nm.c index 7acf9a26333..2546dc5105f 100644 --- a/binutils/nm.c +++ b/binutils/nm.c @@ -433,7 +433,6 @@ static const char * get_elf_symbol_type (unsigned int type) { static char *bufp; - int n; switch (type) { @@ -448,13 +447,11 @@ get_elf_symbol_type (unsigned int type) free (bufp); if (type >= STT_LOPROC && type <= STT_HIPROC) - n = asprintf (&bufp, _(": %d"), type); + bufp = xasprintf (_(": %d"), type); else if (type >= STT_LOOS && type <= STT_HIOS) - n = asprintf (&bufp, _(": %d"), type); + bufp = xasprintf (_(": %d"), type); else - n = asprintf (&bufp, _(": %d"), type); - if (n < 0) - fatal ("%s", xstrerror (errno)); + bufp = xasprintf (_(": %d"), type); return bufp; } @@ -462,7 +459,6 @@ static const char * get_coff_symbol_type (const struct internal_syment *sym) { static char *bufp; - int n; switch (sym->n_sclass) { @@ -482,9 +478,7 @@ get_coff_symbol_type (const struct internal_syment *sym) } free (bufp); - n = asprintf (&bufp, _(": %d/%d"), sym->n_sclass, sym->n_type); - if (n < 0) - fatal ("%s", xstrerror (errno)); + bufp = xasprintf (_(": %d/%d"), sym->n_sclass, sym->n_type); return bufp; } diff --git a/binutils/objdump.c b/binutils/objdump.c index 324254658c6..4980929d6ab 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -4769,11 +4769,7 @@ dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED, char *s, void *arg) { const char *blanks = arg; - char *new_s; - - if (asprintf (&new_s, "%s%s", blanks, s) < 0) - return s; - return new_s; + return xasprintf ("%s%s", blanks, s); } /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */ diff --git a/binutils/readelf.c b/binutils/readelf.c index 0f8dc1b9716..73163e0ee21 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -14421,17 +14421,14 @@ display_lto_symtab (Filedata * filedata, return false; /* Look for extended data for the symbol table. */ - Elf_Internal_Shdr * ext = NULL; void * ext_data_orig = NULL; char * ext_data = NULL; char * ext_data_end = NULL; - char * ext_name = NULL; - - if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s", - (section_name (filedata, section) - + sizeof (".gnu.lto_.symtab.") - 1)) > 0 - && ext_name != NULL /* Paranoia. */ - && (ext = find_section (filedata, ext_name)) != NULL) + char *ext_name = xasprintf (".gnu.lto_.ext_symtab.%s", + (section_name (filedata, section) + + sizeof (".gnu.lto_.symtab."))); + Elf_Internal_Shdr *ext = find_section (filedata, ext_name); + if (ext != NULL) { if (ext->sh_size < 3) error (_("LTO Symbol extension table '%s' is empty!\n"), @@ -16871,11 +16868,7 @@ dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED, char *s, void *arg) { const char *blanks = arg; - char *new_s; - - if (asprintf (&new_s, "%s%s", blanks, s) < 0) - return s; - return new_s; + return xasprintf ("%s%s", blanks, s); } /* Dump CTF errors/warnings. */ diff --git a/binutils/windres.c b/binutils/windres.c index 3f691d3e829..f0f6433160c 100644 --- a/binutils/windres.c +++ b/binutils/windres.c @@ -885,10 +885,7 @@ main (int argc, char **argv) case OPTION_PREPROCESSOR: if (strchr (optarg, ' ')) - { - if (asprintf (& preprocessor, "\"%s\"", optarg) == -1) - preprocessor = optarg; - } + preprocessor = xasprintf ("\"%s\"", optarg); else preprocessor = optarg; break; diff --git a/gas/config.in b/gas/config.in index c32b46b93f5..bb896244640 100644 --- a/gas/config.in +++ b/gas/config.in @@ -89,10 +89,6 @@ */ #undef HAVE_DCGETTEXT -/* Define to 1 if you have the declaration of `asprintf', and to 0 if you - don't. */ -#undef HAVE_DECL_ASPRINTF - /* Is the prototype for getopt in in the expected format? */ #undef HAVE_DECL_GETOPT diff --git a/gas/config/tc-kvx.c b/gas/config/tc-kvx.c index b864b95721f..e0860c0294a 100644 --- a/gas/config/tc-kvx.c +++ b/gas/config/tc-kvx.c @@ -2107,21 +2107,13 @@ kvx_check_label (symbolS *sym) void kvx_emit_single_noop (void) { - char *nop; - char *end_of_bundle; - - if (asprintf (&nop, "nop") < 0) - as_fatal ("%s", xstrerror (errno)); - - if (asprintf (&end_of_bundle, "be") < 0) - as_fatal ("%s", xstrerror (errno)); + char nop[] = "nop"; + char end_of_bundle[] = "be"; char *saved_ilp = input_line_pointer; md_assemble (nop); md_assemble (end_of_bundle); input_line_pointer = saved_ilp; - free (nop); - free (end_of_bundle); } /* edit out some syntactic sugar that confuses GAS */ diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index ef455e449b9..4e804ffa4c8 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -2107,17 +2107,12 @@ macro_build (expressionS *ep, const char *name, const char *fmt, ...) static void md_assemblef (const char *format, ...) { - char *buf = NULL; + char *buf; va_list ap; - int r; va_start (ap, format); - r = vasprintf (&buf, format, ap); - - if (r < 0) - as_fatal (_("internal: vasprintf failed")); - + buf = xvasprintf (format, ap); md_assemble (buf); free(buf); diff --git a/gas/configure b/gas/configure index 47c43c034c7..5d10ec546c7 100755 --- a/gas/configure +++ b/gas/configure @@ -15871,16 +15871,6 @@ $as_echo "#define NEED_DECLARATION_FFS 1" >>confdefs.h fi -ac_fn_c_check_decl "$LINENO" "asprintf" "ac_cv_have_decl_asprintf" "$ac_includes_default" -if test "x$ac_cv_have_decl_asprintf" = xyes; then : - ac_have_decl=1 -else - ac_have_decl=0 -fi - -cat >>confdefs.h <<_ACEOF -#define HAVE_DECL_ASPRINTF $ac_have_decl -_ACEOF ac_fn_c_check_decl "$LINENO" "mempcpy" "ac_cv_have_decl_mempcpy" "$ac_includes_default" if test "x$ac_cv_have_decl_mempcpy" = xyes; then : ac_have_decl=1 diff --git a/gas/configure.ac b/gas/configure.ac index ab1d0e04e2f..997966d4f5e 100644 --- a/gas/configure.ac +++ b/gas/configure.ac @@ -1002,7 +1002,7 @@ fi GAS_CHECK_DECL_NEEDED(environ, f, char **f, $gas_test_headers) GAS_CHECK_DECL_NEEDED(ffs, f, int (*f)(int), $gas_test_headers) -AC_CHECK_DECLS([asprintf, mempcpy, stpcpy]) +AC_CHECK_DECLS([mempcpy, stpcpy]) BFD_BINARY_FOPEN diff --git a/gas/read.c b/gas/read.c index 7a72adac121..aefbd7aefe8 100644 --- a/gas/read.c +++ b/gas/read.c @@ -3604,8 +3604,7 @@ s_nop (int ignore ATTRIBUTE_UNUSED) #endif /* md_assemble might modify its argument, so we must pass it a string that is writable. */ - if (asprintf (&nop, "%s", md_single_noop_insn) < 0) - as_fatal ("%s", xstrerror (errno)); + nop = xasprintf ("%s", md_single_noop_insn); /* Some targets assume that they can update input_line_pointer inside md_assemble, and, worse, that they can leave it @@ -6533,20 +6532,14 @@ do_s_func (int end_p, const char *default_prefix) if (*input_line_pointer != ',') { if (default_prefix) - { - if (asprintf (&label, "%s%s", default_prefix, name) == -1) - as_fatal ("%s", xstrerror (errno)); - } + label = xasprintf ("%s%s", default_prefix, name); else { char leading_char = bfd_get_symbol_leading_char (stdoutput); /* Missing entry point, use function's name with the leading char prepended. */ if (leading_char) - { - if (asprintf (&label, "%c%s", leading_char, name) == -1) - as_fatal ("%s", xstrerror (errno)); - } + label = xasprintf ("%c%s", leading_char, name); else label = xstrdup (name); } diff --git a/gas/stabs.c b/gas/stabs.c index 6542e710b55..da7fb471a1e 100644 --- a/gas/stabs.c +++ b/gas/stabs.c @@ -644,10 +644,8 @@ stabs_generate_asm_func (const char *funcname, const char *startlabname) } as_where (&lineno); - if (asprintf (&buf, "\"%s:F1\",%d,0,%d,%s", - funcname, N_FUN, lineno + 1, startlabname) == -1) - as_fatal ("%s", xstrerror (errno)); - + buf = xasprintf ("\"%s:F1\",%d,0,%d,%s", + funcname, N_FUN, lineno + 1, startlabname); temp_ilp (buf); s_stab ('s'); restore_ilp (); @@ -670,9 +668,7 @@ stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED, ++endfunc_label_count; colon (sym); - if (asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname) == -1) - as_fatal ("%s", xstrerror (errno)); - + buf = xasprintf ("\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname); temp_ilp (buf); s_stab ('s'); restore_ilp (); diff --git a/ld/config.in b/ld/config.in index f2aaf0a6879..633105a43ad 100644 --- a/ld/config.in +++ b/ld/config.in @@ -95,10 +95,6 @@ */ #undef HAVE_DCGETTEXT -/* Define to 1 if you have the declaration of `asprintf', and to 0 if you - don't. */ -#undef HAVE_DECL_ASPRINTF - /* Define to 1 if you have the declaration of `environ', and to 0 if you don't. */ #undef HAVE_DECL_ENVIRON diff --git a/ld/configure b/ld/configure index d905f1c6001..0b4197d1c4f 100755 --- a/ld/configure +++ b/ld/configure @@ -18761,16 +18761,6 @@ $as_echo "#define USE_BINARY_FOPEN 1" >>confdefs.h ;; esac -ac_fn_c_check_decl "$LINENO" "asprintf" "ac_cv_have_decl_asprintf" "$ac_includes_default" -if test "x$ac_cv_have_decl_asprintf" = xyes; then : - ac_have_decl=1 -else - ac_have_decl=0 -fi - -cat >>confdefs.h <<_ACEOF -#define HAVE_DECL_ASPRINTF $ac_have_decl -_ACEOF ac_fn_c_check_decl "$LINENO" "environ" "ac_cv_have_decl_environ" "$ac_includes_default" if test "x$ac_cv_have_decl_environ" = xyes; then : ac_have_decl=1 diff --git a/ld/configure.ac b/ld/configure.ac index 5d10b38a528..3ac2b46ee03 100644 --- a/ld/configure.ac +++ b/ld/configure.ac @@ -408,7 +408,7 @@ AC_CHECK_FUNCS(close glob lseek mkstemp open realpath waitpid) BFD_BINARY_FOPEN -AC_CHECK_DECLS([asprintf, environ, stpcpy]) +AC_CHECK_DECLS([environ, stpcpy]) GCC_AC_FUNC_MMAP diff --git a/ld/ldlang.c b/ld/ldlang.c index 00b214411d6..6335e1f67ad 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -9195,12 +9195,7 @@ void lang_leave_overlay_section (fill_type *fill, lang_output_section_phdr_list *phdrs) { - const char *name; - char *clean, *s2; - const char *s1; - char *buf; - - name = current_section->name; + const char *name = current_section->name;; /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory region and that no load-time region has been specified. It doesn't @@ -9210,21 +9205,19 @@ lang_leave_overlay_section (fill_type *fill, /* Define the magic symbols. */ - clean = (char *) xmalloc (strlen (name) + 1); - s2 = clean; - for (s1 = name; *s1 != '\0'; s1++) + char *clean = xmalloc (strlen (name) + 1); + char *s2 = clean; + for (const char *s1 = name; *s1 != '\0'; s1++) if (ISALNUM (*s1) || *s1 == '_') *s2++ = *s1; *s2 = '\0'; - buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); - sprintf (buf, "__load_start_%s", clean); + char *buf = xasprintf ("__load_start_%s", clean); lang_add_assignment (exp_provide (buf, exp_nameop (LOADADDR, name), false)); - buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); - sprintf (buf, "__load_stop_%s", clean); + buf = xasprintf ("__load_stop_%s", clean); lang_add_assignment (exp_provide (buf, exp_binop ('+', exp_nameop (LOADADDR, name), diff --git a/ld/lexsup.c b/ld/lexsup.c index 8982073bc91..92a12fe19af 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -1868,7 +1868,6 @@ parse_args (unsigned argc, char **argv) { char * new_name = NULL; char * percent; - int res = 0; if (config.map_filename[0] == 0) { @@ -1885,9 +1884,9 @@ parse_args (unsigned argc, char **argv) output filename. If the % character was the last character in the original map filename then add a .map extension. */ percent[0] = 0; - res = asprintf (&new_name, "%s%s%s", config.map_filename, - output_filename, - percent[1] ? percent + 1 : ".map"); + new_name = xasprintf ("%s%s%s", config.map_filename, + output_filename, + percent[1] ? percent + 1 : ".map"); /* FIXME: Should we ensure that any directory components in new_name exist ? */ } @@ -1905,10 +1904,9 @@ parse_args (unsigned argc, char **argv) else if (S_ISDIR (s.st_mode)) { char lastc = config.map_filename[strlen (config.map_filename) - 1]; - res = asprintf (&new_name, "%s%s%s.map", - config.map_filename, - IS_DIR_SEPARATOR (lastc) ? "" : "/", - lbasename (output_filename)); + new_name = xasprintf ("%s%s%s.map", config.map_filename, + IS_DIR_SEPARATOR (lastc) ? "" : "/", + lbasename (output_filename)); } else if (! S_ISREG (s.st_mode)) { @@ -1918,14 +1916,7 @@ parse_args (unsigned argc, char **argv) /* else FIXME: Check write permission ? */ } - if (res < 0) - { - /* If the asprintf failed then something is probably very - wrong. Better to halt now rather than continue on - into more problems. */ - einfo (_("%P%F: cannot create name for linker map file: %E\n")); - } - else if (new_name != NULL) + if (new_name != NULL) { /* This is a trivial memory leak. */ config.map_filename = new_name; diff --git a/ld/pe-dll.c b/ld/pe-dll.c index 93229e02a6e..c2ccf98abe0 100644 --- a/ld/pe-dll.c +++ b/ld/pe-dll.c @@ -2125,12 +2125,7 @@ make_head (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2219,12 +2214,7 @@ make_tail (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2412,12 +2402,7 @@ make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub) } } - if (asprintf (&oname, "%s_d%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2600,12 +2585,7 @@ make_singleton_name_thunk (const char *import, bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_nmth%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_nmth%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2681,12 +2661,7 @@ make_import_fixup_entry (const char *name, char *oname; bfd *abfd; - if (asprintf (&oname, "%s_fu%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_fu%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2740,12 +2715,7 @@ make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED, bfd *abfd; bfd_size_type size; - if (asprintf (&oname, "%s_rtr%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_rtr%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent); @@ -2833,12 +2803,7 @@ pe_create_runtime_relocator_reference (bfd *parent) char *oname; bfd *abfd; - if (asprintf (&oname, "%s_ertr%06d.o", dll_symname, tmp_seq) < 4) - /* In theory we should return NULL here at let our caller decide what to - do. But currently the return value is not checked, just used, and - besides, this condition only happens when the system has run out of - memory. So just give up. */ - exit (EXIT_FAILURE); + oname = xasprintf ("%s_ertr%06d.o", dll_symname, tmp_seq); tmp_seq++; abfd = bfd_create (oname, parent);