* [PATCH 1/2] Introduce xasprintf
@ 2021-09-06 10:00 Dmitry V. Levin
2021-09-06 10:00 ` [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE) Dmitry V. Levin
2021-09-08 22:14 ` [PATCH 1/2] Introduce xasprintf Mark Wielaard
0 siblings, 2 replies; 4+ messages in thread
From: Dmitry V. Levin @ 2021-09-06 10:00 UTC (permalink / raw)
To: elfutils-devel
Similar to other x* functions, xasprintf is like asprintf except that
it dies in case of an error.
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
lib/ChangeLog | 6 ++++++
lib/Makefile.am | 2 +-
lib/libeu.h | 2 ++
lib/xasprintf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 61 insertions(+), 1 deletion(-)
create mode 100644 lib/xasprintf.c
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 60d32082..59d1d51c 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,9 @@
+2021-09-06 Dmitry V. Levin <ldv@altlinux.org>
+
+ * xasprintf.c: New file.
+ * Makefile.am (libeu_a_SOURCES): Add it.
+ * libeu.h (xasprintf): New prototype.
+
2021-08-23 Saleem Abdulrasool <abdulras@google.com>
* system.h: Remove inline definition for error and error_message_count
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 766fbcd7..42ddf5ae 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -33,7 +33,7 @@ AM_CPPFLAGS += -I$(srcdir)/../libelf
noinst_LIBRARIES = libeu.a
-libeu_a_SOURCES = xstrdup.c xstrndup.c xmalloc.c next_prime.c \
+libeu_a_SOURCES = xasprintf.c xstrdup.c xstrndup.c xmalloc.c next_prime.c \
crc32.c crc32_file.c \
color.c error.c printversion.c
diff --git a/lib/libeu.h b/lib/libeu.h
index ecb4d011..e849a79e 100644
--- a/lib/libeu.h
+++ b/lib/libeu.h
@@ -39,6 +39,8 @@ extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
extern char *xstrdup (const char *) __attribute__ ((__malloc__));
extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
+extern char *xasprintf(const char *fmt, ...)
+ __attribute__ ((format (printf, 1, 2))) __attribute__ ((__malloc__));
extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
extern int crc32_file (int fd, uint32_t *resp);
diff --git a/lib/xasprintf.c b/lib/xasprintf.c
new file mode 100644
index 00000000..179ea2e8
--- /dev/null
+++ b/lib/xasprintf.c
@@ -0,0 +1,52 @@
+/* A wrapper around vasprintf that dies in case of an error.
+ Copyright (c) 2021 Dmitry V. Levin <ldv@altlinux.org>
+ This file is part of elfutils.
+
+ This file is free software; you can redistribute it and/or modify
+ it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at
+ your option) any later version
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at
+ your option) any later version
+
+ or both in parallel, as here.
+
+ elfutils is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <libintl.h>
+#include "libeu.h"
+#include "system.h"
+
+char *
+xasprintf (const char *fmt, ...)
+{
+ char *res;
+ va_list ap;
+
+ va_start (ap, fmt);
+ if (unlikely (vasprintf (&res, fmt, ap) < 0))
+ error (EXIT_FAILURE, 0, _("memory exhausted"));
+ va_end(ap);
+
+ return res;
+}
--
ldv
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE)
2021-09-06 10:00 [PATCH 1/2] Introduce xasprintf Dmitry V. Levin
@ 2021-09-06 10:00 ` Dmitry V. Levin
2021-09-08 22:19 ` Mark Wielaard
2021-09-08 22:14 ` [PATCH 1/2] Introduce xasprintf Mark Wielaard
1 sibling, 1 reply; 4+ messages in thread
From: Dmitry V. Levin @ 2021-09-06 10:00 UTC (permalink / raw)
To: elfutils-devel
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
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 <ldv@altlinux.org>
+ * 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 <ldv@altlinux.org>
+
+ * 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 <abdulras@google.com>
* 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] Introduce xasprintf
2021-09-06 10:00 [PATCH 1/2] Introduce xasprintf Dmitry V. Levin
2021-09-06 10:00 ` [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE) Dmitry V. Levin
@ 2021-09-08 22:14 ` Mark Wielaard
1 sibling, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2021-09-08 22:14 UTC (permalink / raw)
To: Dmitry V. Levin; +Cc: elfutils-devel
Hi Dmitry,
On Mon, Sep 06, 2021 at 10:00:00AM +0000, Dmitry V. Levin wrote:
> Similar to other x* functions, xasprintf is like asprintf except that
> it dies in case of an error.
Looks useful and the implementation seems correct.
Please apply.
Thanks,
Mark
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE)
2021-09-06 10:00 ` [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE) Dmitry V. Levin
@ 2021-09-08 22:19 ` Mark Wielaard
0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2021-09-08 22:19 UTC (permalink / raw)
To: Dmitry V. Levin; +Cc: elfutils-devel
Hi Dmitry,
On Mon, Sep 06, 2021 at 10:00:00AM +0000, Dmitry V. Levin wrote:
> + * color.c (parse_opt): Replace asprintf followed by error(EXIT_FAILURE)
> + with xasprintf.
> + * 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.
All these replacements look correct. Please apply.
Thanks,
Mark
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-09-08 22:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-06 10:00 [PATCH 1/2] Introduce xasprintf Dmitry V. Levin
2021-09-06 10:00 ` [PATCH 2/2] Use xasprintf instead of asprintf followed by error(EXIT_FAILURE) Dmitry V. Levin
2021-09-08 22:19 ` Mark Wielaard
2021-09-08 22:14 ` [PATCH 1/2] Introduce xasprintf Mark Wielaard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).