Blame SOURCES/0146-tests-use-xasprintf-instead-of-asprintf.patch

242c1e
From bcd0f3ef964aead4b9e95c10f4c5c75f58ba102c Mon Sep 17 00:00:00 2001
242c1e
From: "Dmitry V. Levin" <ldv@strace.io>
242c1e
Date: Mon, 15 Mar 2021 08:00:00 +0000
242c1e
Subject: [PATCH 146/149] tests: use xasprintf instead of asprintf
242c1e
242c1e
* tests/clone-flags.c: Include "xmalloc.h", replace all asprintf
242c1e
invocations followed by perror_msg_and_fail with xasprintf.
242c1e
* tests/epoll_pwait2.c: Likewise.
242c1e
* tests/faccessat.c: Likewise.
242c1e
* tests/faccessat2.c: Likewise.
242c1e
* tests/ioctl_kvm_run_common.c: Likewise.
242c1e
* tests/keyctl.c: Likewise.
242c1e
* tests/lock_file.c: Likewise.
242c1e
* tests/mq.c: Likewise.
242c1e
* tests/mq_sendrecv.c: Likewise.
242c1e
* tests/netlink_protocol.c: Likewise.
242c1e
* tests/old_mmap.c: Likewise.
242c1e
* tests/tracer_ppid_pgid_sid.c: Likewise.
242c1e
242c1e
Conflicts:
242c1e
	tests/epoll_pwait2.c
242c1e
	tests/faccessat2.c
242c1e
	tests/mq_sendrecv.c
242c1e
---
242c1e
 tests/clone-flags.c          |  5 ++---
242c1e
 tests/faccessat.c            | 17 +++++------------
242c1e
 tests/ioctl_kvm_run_common.c |  6 ++----
242c1e
 tests/keyctl.c               | 29 +++++++++++++----------------
242c1e
 tests/lock_file.c            |  5 ++---
242c1e
 tests/mq.c                   |  5 ++---
242c1e
 tests/mq_sendrecv.c          |  4 ++--
242c1e
 tests/netlink_protocol.c     |  5 ++---
242c1e
 tests/old_mmap.c             |  5 ++---
242c1e
 tests/tracer_ppid_pgid_sid.c |  6 ++----
242c1e
 10 files changed, 34 insertions(+), 53 deletions(-)
242c1e
242c1e
diff --git a/tests/clone-flags.c b/tests/clone-flags.c
242c1e
index 04bb2c7..aa7d48c 100644
242c1e
--- a/tests/clone-flags.c
242c1e
+++ b/tests/clone-flags.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <errno.h>
242c1e
 #include <limits.h>
242c1e
@@ -134,9 +135,7 @@ main(void)
242c1e
 		*ptid = 0;
242c1e
 		pid = do_clone(child, child_stack, child_stack_size,
242c1e
 			       CLONE_PIDFD|SIGCHLD, 0, ptid);
242c1e
-		char *fname = 0;
242c1e
-		if (asprintf(&fname, "/proc/self/fd/%d", *ptid) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *fname = xasprintf("/proc/self/fd/%d", *ptid);
242c1e
 		int rc = readlink(fname, buf, sizeof(buf) - 1);
242c1e
 		if ((unsigned int) rc >= sizeof(buf))
242c1e
 			perror_msg_and_fail("readlink");
242c1e
diff --git a/tests/faccessat.c b/tests/faccessat.c
242c1e
index c42aa2d..8cda6f3 100644
242c1e
--- a/tests/faccessat.c
242c1e
+++ b/tests/faccessat.c
242c1e
@@ -12,6 +12,7 @@
242c1e
 
242c1e
 #ifdef __NR_faccessat
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include <fcntl.h>
242c1e
 # include <stdio.h>
242c1e
 # include <unistd.h>
242c1e
@@ -48,13 +49,9 @@ main(void)
242c1e
 	SKIP_IF_PROC_IS_UNAVAILABLE;
242c1e
 
242c1e
 	TAIL_ALLOC_OBJECT_CONST_PTR(const char, unterminated);
242c1e
-	char *unterminated_str;
242c1e
-	if (asprintf(&unterminated_str, "%p", unterminated) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *unterminated_str = xasprintf("%p", unterminated);
242c1e
 	const void *const efault = unterminated + 1;
242c1e
-	char *efault_str;
242c1e
-	if (asprintf(&efault_str, "%p", efault) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *efault_str = xasprintf("%p", efault);
242c1e
 
242c1e
 	typedef struct {
242c1e
 		char sym;
242c1e
@@ -75,12 +72,8 @@ main(void)
242c1e
         int fd = open(path, O_WRONLY);
242c1e
         if (fd < 0)
242c1e
                 perror_msg_and_fail("open: %s", path);
242c1e
-	char *fd_str;
242c1e
-	if (asprintf(&fd_str, "%d%s", fd, FD_PATH) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
-	char *path_quoted;
242c1e
-	if (asprintf(&path_quoted, "\"%s\"", path) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *fd_str = xasprintf("%d%s", fd, FD_PATH);
242c1e
+	char *path_quoted = xasprintf("\"%s\"", path);
242c1e
 
242c1e
 	struct {
242c1e
 		int val;
242c1e
diff --git a/tests/ioctl_kvm_run_common.c b/tests/ioctl_kvm_run_common.c
242c1e
index 9107c30..be1190e 100644
242c1e
--- a/tests/ioctl_kvm_run_common.c
242c1e
+++ b/tests/ioctl_kvm_run_common.c
242c1e
@@ -48,6 +48,7 @@
242c1e
 #  define KVM_MAX_CPUID_ENTRIES 80
242c1e
 # endif
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "xlat.h"
242c1e
 # include "xlat/kvm_cpuid_flags.h"
242c1e
 
242c1e
@@ -254,12 +255,9 @@ static int
242c1e
 vcpu_dev_should_have_cpuid(int fd)
242c1e
 {
242c1e
 	int r = 0;
242c1e
-	char *filename = NULL;
242c1e
+	char *filename = xasprintf("/proc/%d/fd/%d", getpid(), fd);
242c1e
 	char buf[sizeof(vcpu_dev)];
242c1e
 
242c1e
-	if (asprintf(&filename, "/proc/%d/fd/%d", getpid(), fd) < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
-
242c1e
 	if (readlink(filename, buf, sizeof(buf)) == sizeof(buf) - 1
242c1e
 	    && (memcmp(buf, vcpu_dev, sizeof(buf) - 1) == 0))
242c1e
 		r = 1;
242c1e
diff --git a/tests/keyctl.c b/tests/keyctl.c
242c1e
index 6dc30fb..96ac197 100644
242c1e
--- a/tests/keyctl.c
242c1e
+++ b/tests/keyctl.c
242c1e
@@ -11,6 +11,7 @@
242c1e
 #include "tests.h"
242c1e
 
242c1e
 #include "scno.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #ifdef __NR_keyctl
242c1e
 
242c1e
@@ -445,7 +446,6 @@ main(void)
242c1e
 	struct iovec *key_iov = tail_alloc(sizeof(*key_iov) * IOV_SIZE);
242c1e
 	char *bogus_buf1 = tail_alloc(9);
242c1e
 	char *bogus_buf2 = tail_alloc(256);
242c1e
-	char *key_iov_str1;
242c1e
 	char *key_iov_str2 = tail_alloc(4096);
242c1e
 	const char *errstr;
242c1e
 	ssize_t ret;
242c1e
@@ -472,21 +472,18 @@ main(void)
242c1e
 			0x100000001ULL * i);
242c1e
 	}
242c1e
 
242c1e
-	ret = asprintf(&key_iov_str1, "[{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}]",
242c1e
-		       key_iov[IOV_SIZE - 4].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 4].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_len);
242c1e
-
242c1e
-	if (ret < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
+	char *key_iov_str1 = xasprintf("[{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}]",
242c1e
+				       key_iov[IOV_SIZE - 4].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 4].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_len);
242c1e
 
242c1e
 	ret = snprintf(key_iov_str2, IOV_STR_SIZE,
242c1e
 		       "[{iov_base=\"%s\\0\", iov_len=%zu}, "
242c1e
diff --git a/tests/lock_file.c b/tests/lock_file.c
242c1e
index 56cf112..7618552 100644
242c1e
--- a/tests/lock_file.c
242c1e
+++ b/tests/lock_file.c
242c1e
@@ -6,6 +6,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <fcntl.h>
242c1e
 #include <stdio.h>
242c1e
@@ -21,9 +22,7 @@ lock_file_by_dirname(const char *path_name, const char *lock_name)
242c1e
 	const char *slash = path_name ? strrchr(path_name, '/') : NULL;
242c1e
 	const int plen = slash ? (int) (slash - path_name) + 1 : 0;
242c1e
 
242c1e
-	char *lock_file = NULL;
242c1e
-	if (asprintf(&lock_file, "%.*s%s", plen, path_name, lock_name) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *lock_file = xasprintf("%.*s%s", plen, path_name, lock_name);
242c1e
 
242c1e
 	int lock_fd = open(lock_file, O_RDONLY);
242c1e
 	if (lock_fd < 0)
242c1e
diff --git a/tests/mq.c b/tests/mq.c
242c1e
index a083e5a..7aa0914 100644
242c1e
--- a/tests/mq.c
242c1e
+++ b/tests/mq.c
242c1e
@@ -17,6 +17,7 @@
242c1e
 # include <stdlib.h>
242c1e
 # include <unistd.h>
242c1e
 # include <sys/stat.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 int
242c1e
 main(void)
242c1e
@@ -24,9 +25,7 @@ main(void)
242c1e
 	struct mq_attr attr;
242c1e
 	(void) close(0);
242c1e
 
242c1e
-	char *name;
242c1e
-	if (asprintf(&name, "/strace-mq-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *name = xasprintf("/strace-mq-%u.sample", getpid());
242c1e
 
242c1e
 	if (mq_open(name, O_CREAT, 0700, NULL))
242c1e
 		perror_msg_and_skip("mq_open");
242c1e
diff --git a/tests/mq_sendrecv.c b/tests/mq_sendrecv.c
242c1e
index 45ddf5e..5a919c4 100644
242c1e
--- a/tests/mq_sendrecv.c
242c1e
+++ b/tests/mq_sendrecv.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <time.h>
242c1e
 # include <unistd.h>
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "sigevent.h"
242c1e
 
242c1e
 # ifndef DUMPIO_READ
242c1e
@@ -407,8 +408,7 @@ main(void)
242c1e
 
242c1e
 	/* Sending and receiving test */
242c1e
 
242c1e
-	if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	mq_name = xasprintf("strace-mq_sendrecv-%u.sample", getpid());
242c1e
 
242c1e
 # if DUMPIO_READ || DUMPIO_WRITE
242c1e
 	close(0);
242c1e
diff --git a/tests/netlink_protocol.c b/tests/netlink_protocol.c
242c1e
index e632ba3..c37489f 100644
242c1e
--- a/tests/netlink_protocol.c
242c1e
+++ b/tests/netlink_protocol.c
242c1e
@@ -22,6 +22,7 @@
242c1e
 # include "netlink.h"
242c1e
 # include <linux/sock_diag.h>
242c1e
 # include <linux/netlink_diag.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 static void
242c1e
 send_query(const int fd)
242c1e
@@ -388,9 +389,7 @@ int main(void)
242c1e
 {
242c1e
 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
242c1e
 
242c1e
-	char *path;
242c1e
-	if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *path = xasprintf("/proc/self/fd/%u", fd);
242c1e
 	char buf[256];
242c1e
 	if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
242c1e
 		perror_msg_and_skip("getxattr");
242c1e
diff --git a/tests/old_mmap.c b/tests/old_mmap.c
242c1e
index f095bc4..bb70359 100644
242c1e
--- a/tests/old_mmap.c
242c1e
+++ b/tests/old_mmap.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <string.h>
242c1e
 # include <sys/mman.h>
242c1e
 # include <unistd.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 # ifndef TEST_FD
242c1e
 #  define TEST_FD -2LU
242c1e
@@ -82,9 +83,7 @@ main(void)
242c1e
 # ifndef PATH_TRACING
242c1e
 	const char *errstr;
242c1e
 	if (implemented) {
242c1e
-		char *str;
242c1e
-		if (asprintf(&str, "%#lx", rc) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *str = xasprintf("%#lx", rc);
242c1e
 		errstr = str;
242c1e
 	} else {
242c1e
 		errstr = sprintrc(rc);
242c1e
diff --git a/tests/tracer_ppid_pgid_sid.c b/tests/tracer_ppid_pgid_sid.c
242c1e
index 69687fa..ce9936d 100644
242c1e
--- a/tests/tracer_ppid_pgid_sid.c
242c1e
+++ b/tests/tracer_ppid_pgid_sid.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 #include <ctype.h>
242c1e
 #include <stdio.h>
242c1e
 #include <stdlib.h>
242c1e
@@ -55,10 +56,7 @@ get_tracer_pid(void)
242c1e
 static void
242c1e
 get_ppid_pgid_sid(int pid, int *ppid, int *pgid, int *sid)
242c1e
 {
242c1e
-	char *stat;
242c1e
-	if (asprintf(&stat, "/proc/%d/stat", pid) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
-
242c1e
+	char *stat = xasprintf("/proc/%d/stat", pid);
242c1e
 	FILE *fp = fopen(stat, "r");
242c1e
 	if (!fp)
242c1e
 		perror_msg_and_fail("fopen: %s", stat);
242c1e
diff --git a/tests-m32/clone-flags.c b/tests-m32/clone-flags.c
242c1e
index 04bb2c7..aa7d48c 100644
242c1e
--- a/tests-m32/clone-flags.c
242c1e
+++ b/tests-m32/clone-flags.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <errno.h>
242c1e
 #include <limits.h>
242c1e
@@ -134,9 +135,7 @@ main(void)
242c1e
 		*ptid = 0;
242c1e
 		pid = do_clone(child, child_stack, child_stack_size,
242c1e
 			       CLONE_PIDFD|SIGCHLD, 0, ptid);
242c1e
-		char *fname = 0;
242c1e
-		if (asprintf(&fname, "/proc/self/fd/%d", *ptid) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *fname = xasprintf("/proc/self/fd/%d", *ptid);
242c1e
 		int rc = readlink(fname, buf, sizeof(buf) - 1);
242c1e
 		if ((unsigned int) rc >= sizeof(buf))
242c1e
 			perror_msg_and_fail("readlink");
242c1e
diff --git a/tests-m32/faccessat.c b/tests-m32/faccessat.c
242c1e
index c42aa2d..8cda6f3 100644
242c1e
--- a/tests-m32/faccessat.c
242c1e
+++ b/tests-m32/faccessat.c
242c1e
@@ -12,6 +12,7 @@
242c1e
 
242c1e
 #ifdef __NR_faccessat
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include <fcntl.h>
242c1e
 # include <stdio.h>
242c1e
 # include <unistd.h>
242c1e
@@ -48,13 +49,9 @@ main(void)
242c1e
 	SKIP_IF_PROC_IS_UNAVAILABLE;
242c1e
 
242c1e
 	TAIL_ALLOC_OBJECT_CONST_PTR(const char, unterminated);
242c1e
-	char *unterminated_str;
242c1e
-	if (asprintf(&unterminated_str, "%p", unterminated) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *unterminated_str = xasprintf("%p", unterminated);
242c1e
 	const void *const efault = unterminated + 1;
242c1e
-	char *efault_str;
242c1e
-	if (asprintf(&efault_str, "%p", efault) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *efault_str = xasprintf("%p", efault);
242c1e
 
242c1e
 	typedef struct {
242c1e
 		char sym;
242c1e
@@ -75,12 +72,8 @@ main(void)
242c1e
         int fd = open(path, O_WRONLY);
242c1e
         if (fd < 0)
242c1e
                 perror_msg_and_fail("open: %s", path);
242c1e
-	char *fd_str;
242c1e
-	if (asprintf(&fd_str, "%d%s", fd, FD_PATH) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
-	char *path_quoted;
242c1e
-	if (asprintf(&path_quoted, "\"%s\"", path) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *fd_str = xasprintf("%d%s", fd, FD_PATH);
242c1e
+	char *path_quoted = xasprintf("\"%s\"", path);
242c1e
 
242c1e
 	struct {
242c1e
 		int val;
242c1e
diff --git a/tests-m32/ioctl_kvm_run_common.c b/tests-m32/ioctl_kvm_run_common.c
242c1e
index 9107c30..be1190e 100644
242c1e
--- a/tests-m32/ioctl_kvm_run_common.c
242c1e
+++ b/tests-m32/ioctl_kvm_run_common.c
242c1e
@@ -48,6 +48,7 @@
242c1e
 #  define KVM_MAX_CPUID_ENTRIES 80
242c1e
 # endif
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "xlat.h"
242c1e
 # include "xlat/kvm_cpuid_flags.h"
242c1e
 
242c1e
@@ -254,12 +255,9 @@ static int
242c1e
 vcpu_dev_should_have_cpuid(int fd)
242c1e
 {
242c1e
 	int r = 0;
242c1e
-	char *filename = NULL;
242c1e
+	char *filename = xasprintf("/proc/%d/fd/%d", getpid(), fd);
242c1e
 	char buf[sizeof(vcpu_dev)];
242c1e
 
242c1e
-	if (asprintf(&filename, "/proc/%d/fd/%d", getpid(), fd) < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
-
242c1e
 	if (readlink(filename, buf, sizeof(buf)) == sizeof(buf) - 1
242c1e
 	    && (memcmp(buf, vcpu_dev, sizeof(buf) - 1) == 0))
242c1e
 		r = 1;
242c1e
diff --git a/tests-m32/keyctl.c b/tests-m32/keyctl.c
242c1e
index 6dc30fb..96ac197 100644
242c1e
--- a/tests-m32/keyctl.c
242c1e
+++ b/tests-m32/keyctl.c
242c1e
@@ -11,6 +11,7 @@
242c1e
 #include "tests.h"
242c1e
 
242c1e
 #include "scno.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #ifdef __NR_keyctl
242c1e
 
242c1e
@@ -445,7 +446,6 @@ main(void)
242c1e
 	struct iovec *key_iov = tail_alloc(sizeof(*key_iov) * IOV_SIZE);
242c1e
 	char *bogus_buf1 = tail_alloc(9);
242c1e
 	char *bogus_buf2 = tail_alloc(256);
242c1e
-	char *key_iov_str1;
242c1e
 	char *key_iov_str2 = tail_alloc(4096);
242c1e
 	const char *errstr;
242c1e
 	ssize_t ret;
242c1e
@@ -472,21 +472,18 @@ main(void)
242c1e
 			0x100000001ULL * i);
242c1e
 	}
242c1e
 
242c1e
-	ret = asprintf(&key_iov_str1, "[{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}]",
242c1e
-		       key_iov[IOV_SIZE - 4].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 4].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_len);
242c1e
-
242c1e
-	if (ret < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
+	char *key_iov_str1 = xasprintf("[{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}]",
242c1e
+				       key_iov[IOV_SIZE - 4].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 4].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_len);
242c1e
 
242c1e
 	ret = snprintf(key_iov_str2, IOV_STR_SIZE,
242c1e
 		       "[{iov_base=\"%s\\0\", iov_len=%zu}, "
242c1e
diff --git a/tests-m32/lock_file.c b/tests-m32/lock_file.c
242c1e
index 56cf112..7618552 100644
242c1e
--- a/tests-m32/lock_file.c
242c1e
+++ b/tests-m32/lock_file.c
242c1e
@@ -6,6 +6,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <fcntl.h>
242c1e
 #include <stdio.h>
242c1e
@@ -21,9 +22,7 @@ lock_file_by_dirname(const char *path_name, const char *lock_name)
242c1e
 	const char *slash = path_name ? strrchr(path_name, '/') : NULL;
242c1e
 	const int plen = slash ? (int) (slash - path_name) + 1 : 0;
242c1e
 
242c1e
-	char *lock_file = NULL;
242c1e
-	if (asprintf(&lock_file, "%.*s%s", plen, path_name, lock_name) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *lock_file = xasprintf("%.*s%s", plen, path_name, lock_name);
242c1e
 
242c1e
 	int lock_fd = open(lock_file, O_RDONLY);
242c1e
 	if (lock_fd < 0)
242c1e
diff --git a/tests-m32/mq.c b/tests-m32/mq.c
242c1e
index a083e5a..7aa0914 100644
242c1e
--- a/tests-m32/mq.c
242c1e
+++ b/tests-m32/mq.c
242c1e
@@ -17,6 +17,7 @@
242c1e
 # include <stdlib.h>
242c1e
 # include <unistd.h>
242c1e
 # include <sys/stat.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 int
242c1e
 main(void)
242c1e
@@ -24,9 +25,7 @@ main(void)
242c1e
 	struct mq_attr attr;
242c1e
 	(void) close(0);
242c1e
 
242c1e
-	char *name;
242c1e
-	if (asprintf(&name, "/strace-mq-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *name = xasprintf("/strace-mq-%u.sample", getpid());
242c1e
 
242c1e
 	if (mq_open(name, O_CREAT, 0700, NULL))
242c1e
 		perror_msg_and_skip("mq_open");
242c1e
diff --git a/tests-m32/mq_sendrecv.c b/tests-m32/mq_sendrecv.c
242c1e
index 45ddf5e..5a919c4 100644
242c1e
--- a/tests-m32/mq_sendrecv.c
242c1e
+++ b/tests-m32/mq_sendrecv.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <time.h>
242c1e
 # include <unistd.h>
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "sigevent.h"
242c1e
 
242c1e
 # ifndef DUMPIO_READ
242c1e
@@ -407,8 +408,7 @@ main(void)
242c1e
 
242c1e
 	/* Sending and receiving test */
242c1e
 
242c1e
-	if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	mq_name = xasprintf("strace-mq_sendrecv-%u.sample", getpid());
242c1e
 
242c1e
 # if DUMPIO_READ || DUMPIO_WRITE
242c1e
 	close(0);
242c1e
diff --git a/tests-m32/netlink_protocol.c b/tests-m32/netlink_protocol.c
242c1e
index e632ba3..c37489f 100644
242c1e
--- a/tests-m32/netlink_protocol.c
242c1e
+++ b/tests-m32/netlink_protocol.c
242c1e
@@ -22,6 +22,7 @@
242c1e
 # include "netlink.h"
242c1e
 # include <linux/sock_diag.h>
242c1e
 # include <linux/netlink_diag.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 static void
242c1e
 send_query(const int fd)
242c1e
@@ -388,9 +389,7 @@ int main(void)
242c1e
 {
242c1e
 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
242c1e
 
242c1e
-	char *path;
242c1e
-	if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *path = xasprintf("/proc/self/fd/%u", fd);
242c1e
 	char buf[256];
242c1e
 	if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
242c1e
 		perror_msg_and_skip("getxattr");
242c1e
diff --git a/tests-m32/old_mmap.c b/tests-m32/old_mmap.c
242c1e
index f095bc4..bb70359 100644
242c1e
--- a/tests-m32/old_mmap.c
242c1e
+++ b/tests-m32/old_mmap.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <string.h>
242c1e
 # include <sys/mman.h>
242c1e
 # include <unistd.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 # ifndef TEST_FD
242c1e
 #  define TEST_FD -2LU
242c1e
@@ -82,9 +83,7 @@ main(void)
242c1e
 # ifndef PATH_TRACING
242c1e
 	const char *errstr;
242c1e
 	if (implemented) {
242c1e
-		char *str;
242c1e
-		if (asprintf(&str, "%#lx", rc) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *str = xasprintf("%#lx", rc);
242c1e
 		errstr = str;
242c1e
 	} else {
242c1e
 		errstr = sprintrc(rc);
242c1e
diff --git a/tests-m32/tracer_ppid_pgid_sid.c b/tests-m32/tracer_ppid_pgid_sid.c
242c1e
index 69687fa..ce9936d 100644
242c1e
--- a/tests-m32/tracer_ppid_pgid_sid.c
242c1e
+++ b/tests-m32/tracer_ppid_pgid_sid.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 #include <ctype.h>
242c1e
 #include <stdio.h>
242c1e
 #include <stdlib.h>
242c1e
@@ -55,10 +56,7 @@ get_tracer_pid(void)
242c1e
 static void
242c1e
 get_ppid_pgid_sid(int pid, int *ppid, int *pgid, int *sid)
242c1e
 {
242c1e
-	char *stat;
242c1e
-	if (asprintf(&stat, "/proc/%d/stat", pid) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
-
242c1e
+	char *stat = xasprintf("/proc/%d/stat", pid);
242c1e
 	FILE *fp = fopen(stat, "r");
242c1e
 	if (!fp)
242c1e
 		perror_msg_and_fail("fopen: %s", stat);
242c1e
diff --git a/tests-mx32/clone-flags.c b/tests-mx32/clone-flags.c
242c1e
index 04bb2c7..aa7d48c 100644
242c1e
--- a/tests-mx32/clone-flags.c
242c1e
+++ b/tests-mx32/clone-flags.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <errno.h>
242c1e
 #include <limits.h>
242c1e
@@ -134,9 +135,7 @@ main(void)
242c1e
 		*ptid = 0;
242c1e
 		pid = do_clone(child, child_stack, child_stack_size,
242c1e
 			       CLONE_PIDFD|SIGCHLD, 0, ptid);
242c1e
-		char *fname = 0;
242c1e
-		if (asprintf(&fname, "/proc/self/fd/%d", *ptid) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *fname = xasprintf("/proc/self/fd/%d", *ptid);
242c1e
 		int rc = readlink(fname, buf, sizeof(buf) - 1);
242c1e
 		if ((unsigned int) rc >= sizeof(buf))
242c1e
 			perror_msg_and_fail("readlink");
242c1e
diff --git a/tests-mx32/faccessat.c b/tests-mx32/faccessat.c
242c1e
index c42aa2d..8cda6f3 100644
242c1e
--- a/tests-mx32/faccessat.c
242c1e
+++ b/tests-mx32/faccessat.c
242c1e
@@ -12,6 +12,7 @@
242c1e
 
242c1e
 #ifdef __NR_faccessat
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include <fcntl.h>
242c1e
 # include <stdio.h>
242c1e
 # include <unistd.h>
242c1e
@@ -48,13 +49,9 @@ main(void)
242c1e
 	SKIP_IF_PROC_IS_UNAVAILABLE;
242c1e
 
242c1e
 	TAIL_ALLOC_OBJECT_CONST_PTR(const char, unterminated);
242c1e
-	char *unterminated_str;
242c1e
-	if (asprintf(&unterminated_str, "%p", unterminated) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *unterminated_str = xasprintf("%p", unterminated);
242c1e
 	const void *const efault = unterminated + 1;
242c1e
-	char *efault_str;
242c1e
-	if (asprintf(&efault_str, "%p", efault) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *efault_str = xasprintf("%p", efault);
242c1e
 
242c1e
 	typedef struct {
242c1e
 		char sym;
242c1e
@@ -75,12 +72,8 @@ main(void)
242c1e
         int fd = open(path, O_WRONLY);
242c1e
         if (fd < 0)
242c1e
                 perror_msg_and_fail("open: %s", path);
242c1e
-	char *fd_str;
242c1e
-	if (asprintf(&fd_str, "%d%s", fd, FD_PATH) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
-	char *path_quoted;
242c1e
-	if (asprintf(&path_quoted, "\"%s\"", path) < 0)
242c1e
-                perror_msg_and_fail("asprintf");
242c1e
+	char *fd_str = xasprintf("%d%s", fd, FD_PATH);
242c1e
+	char *path_quoted = xasprintf("\"%s\"", path);
242c1e
 
242c1e
 	struct {
242c1e
 		int val;
242c1e
diff --git a/tests-mx32/ioctl_kvm_run_common.c b/tests-mx32/ioctl_kvm_run_common.c
242c1e
index 9107c30..be1190e 100644
242c1e
--- a/tests-mx32/ioctl_kvm_run_common.c
242c1e
+++ b/tests-mx32/ioctl_kvm_run_common.c
242c1e
@@ -48,6 +48,7 @@
242c1e
 #  define KVM_MAX_CPUID_ENTRIES 80
242c1e
 # endif
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "xlat.h"
242c1e
 # include "xlat/kvm_cpuid_flags.h"
242c1e
 
242c1e
@@ -254,12 +255,9 @@ static int
242c1e
 vcpu_dev_should_have_cpuid(int fd)
242c1e
 {
242c1e
 	int r = 0;
242c1e
-	char *filename = NULL;
242c1e
+	char *filename = xasprintf("/proc/%d/fd/%d", getpid(), fd);
242c1e
 	char buf[sizeof(vcpu_dev)];
242c1e
 
242c1e
-	if (asprintf(&filename, "/proc/%d/fd/%d", getpid(), fd) < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
-
242c1e
 	if (readlink(filename, buf, sizeof(buf)) == sizeof(buf) - 1
242c1e
 	    && (memcmp(buf, vcpu_dev, sizeof(buf) - 1) == 0))
242c1e
 		r = 1;
242c1e
diff --git a/tests-mx32/keyctl.c b/tests-mx32/keyctl.c
242c1e
index 6dc30fb..96ac197 100644
242c1e
--- a/tests-mx32/keyctl.c
242c1e
+++ b/tests-mx32/keyctl.c
242c1e
@@ -11,6 +11,7 @@
242c1e
 #include "tests.h"
242c1e
 
242c1e
 #include "scno.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #ifdef __NR_keyctl
242c1e
 
242c1e
@@ -445,7 +446,6 @@ main(void)
242c1e
 	struct iovec *key_iov = tail_alloc(sizeof(*key_iov) * IOV_SIZE);
242c1e
 	char *bogus_buf1 = tail_alloc(9);
242c1e
 	char *bogus_buf2 = tail_alloc(256);
242c1e
-	char *key_iov_str1;
242c1e
 	char *key_iov_str2 = tail_alloc(4096);
242c1e
 	const char *errstr;
242c1e
 	ssize_t ret;
242c1e
@@ -472,21 +472,18 @@ main(void)
242c1e
 			0x100000001ULL * i);
242c1e
 	}
242c1e
 
242c1e
-	ret = asprintf(&key_iov_str1, "[{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}, "
242c1e
-		       "{iov_base=%p, iov_len=%zu}]",
242c1e
-		       key_iov[IOV_SIZE - 4].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 4].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 3].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 2].iov_len,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_base,
242c1e
-		       key_iov[IOV_SIZE - 1].iov_len);
242c1e
-
242c1e
-	if (ret < 0)
242c1e
-		error_msg_and_fail("asprintf");
242c1e
+	char *key_iov_str1 = xasprintf("[{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}, "
242c1e
+				       "{iov_base=%p, iov_len=%zu}]",
242c1e
+				       key_iov[IOV_SIZE - 4].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 4].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 3].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 2].iov_len,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_base,
242c1e
+				       key_iov[IOV_SIZE - 1].iov_len);
242c1e
 
242c1e
 	ret = snprintf(key_iov_str2, IOV_STR_SIZE,
242c1e
 		       "[{iov_base=\"%s\\0\", iov_len=%zu}, "
242c1e
diff --git a/tests-mx32/lock_file.c b/tests-mx32/lock_file.c
242c1e
index 56cf112..7618552 100644
242c1e
--- a/tests-mx32/lock_file.c
242c1e
+++ b/tests-mx32/lock_file.c
242c1e
@@ -6,6 +6,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 
242c1e
 #include <fcntl.h>
242c1e
 #include <stdio.h>
242c1e
@@ -21,9 +22,7 @@ lock_file_by_dirname(const char *path_name, const char *lock_name)
242c1e
 	const char *slash = path_name ? strrchr(path_name, '/') : NULL;
242c1e
 	const int plen = slash ? (int) (slash - path_name) + 1 : 0;
242c1e
 
242c1e
-	char *lock_file = NULL;
242c1e
-	if (asprintf(&lock_file, "%.*s%s", plen, path_name, lock_name) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *lock_file = xasprintf("%.*s%s", plen, path_name, lock_name);
242c1e
 
242c1e
 	int lock_fd = open(lock_file, O_RDONLY);
242c1e
 	if (lock_fd < 0)
242c1e
diff --git a/tests-mx32/mq.c b/tests-mx32/mq.c
242c1e
index a083e5a..7aa0914 100644
242c1e
--- a/tests-mx32/mq.c
242c1e
+++ b/tests-mx32/mq.c
242c1e
@@ -17,6 +17,7 @@
242c1e
 # include <stdlib.h>
242c1e
 # include <unistd.h>
242c1e
 # include <sys/stat.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 int
242c1e
 main(void)
242c1e
@@ -24,9 +25,7 @@ main(void)
242c1e
 	struct mq_attr attr;
242c1e
 	(void) close(0);
242c1e
 
242c1e
-	char *name;
242c1e
-	if (asprintf(&name, "/strace-mq-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *name = xasprintf("/strace-mq-%u.sample", getpid());
242c1e
 
242c1e
 	if (mq_open(name, O_CREAT, 0700, NULL))
242c1e
 		perror_msg_and_skip("mq_open");
242c1e
diff --git a/tests-mx32/mq_sendrecv.c b/tests-mx32/mq_sendrecv.c
242c1e
index 45ddf5e..5a919c4 100644
242c1e
--- a/tests-mx32/mq_sendrecv.c
242c1e
+++ b/tests-mx32/mq_sendrecv.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <time.h>
242c1e
 # include <unistd.h>
242c1e
 
242c1e
+# include "xmalloc.h"
242c1e
 # include "sigevent.h"
242c1e
 
242c1e
 # ifndef DUMPIO_READ
242c1e
@@ -407,8 +408,7 @@ main(void)
242c1e
 
242c1e
 	/* Sending and receiving test */
242c1e
 
242c1e
-	if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	mq_name = xasprintf("strace-mq_sendrecv-%u.sample", getpid());
242c1e
 
242c1e
 # if DUMPIO_READ || DUMPIO_WRITE
242c1e
 	close(0);
242c1e
diff --git a/tests-mx32/netlink_protocol.c b/tests-mx32/netlink_protocol.c
242c1e
index e632ba3..c37489f 100644
242c1e
--- a/tests-mx32/netlink_protocol.c
242c1e
+++ b/tests-mx32/netlink_protocol.c
242c1e
@@ -22,6 +22,7 @@
242c1e
 # include "netlink.h"
242c1e
 # include <linux/sock_diag.h>
242c1e
 # include <linux/netlink_diag.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 static void
242c1e
 send_query(const int fd)
242c1e
@@ -388,9 +389,7 @@ int main(void)
242c1e
 {
242c1e
 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
242c1e
 
242c1e
-	char *path;
242c1e
-	if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
+	char *path = xasprintf("/proc/self/fd/%u", fd);
242c1e
 	char buf[256];
242c1e
 	if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
242c1e
 		perror_msg_and_skip("getxattr");
242c1e
diff --git a/tests-mx32/old_mmap.c b/tests-mx32/old_mmap.c
242c1e
index f095bc4..bb70359 100644
242c1e
--- a/tests-mx32/old_mmap.c
242c1e
+++ b/tests-mx32/old_mmap.c
242c1e
@@ -27,6 +27,7 @@
242c1e
 # include <string.h>
242c1e
 # include <sys/mman.h>
242c1e
 # include <unistd.h>
242c1e
+# include "xmalloc.h"
242c1e
 
242c1e
 # ifndef TEST_FD
242c1e
 #  define TEST_FD -2LU
242c1e
@@ -82,9 +83,7 @@ main(void)
242c1e
 # ifndef PATH_TRACING
242c1e
 	const char *errstr;
242c1e
 	if (implemented) {
242c1e
-		char *str;
242c1e
-		if (asprintf(&str, "%#lx", rc) < 0)
242c1e
-			perror_msg_and_fail("asprintf");
242c1e
+		char *str = xasprintf("%#lx", rc);
242c1e
 		errstr = str;
242c1e
 	} else {
242c1e
 		errstr = sprintrc(rc);
242c1e
diff --git a/tests-mx32/tracer_ppid_pgid_sid.c b/tests-mx32/tracer_ppid_pgid_sid.c
242c1e
index 69687fa..ce9936d 100644
242c1e
--- a/tests-mx32/tracer_ppid_pgid_sid.c
242c1e
+++ b/tests-mx32/tracer_ppid_pgid_sid.c
242c1e
@@ -8,6 +8,7 @@
242c1e
  */
242c1e
 
242c1e
 #include "tests.h"
242c1e
+#include "xmalloc.h"
242c1e
 #include <ctype.h>
242c1e
 #include <stdio.h>
242c1e
 #include <stdlib.h>
242c1e
@@ -55,10 +56,7 @@ get_tracer_pid(void)
242c1e
 static void
242c1e
 get_ppid_pgid_sid(int pid, int *ppid, int *pgid, int *sid)
242c1e
 {
242c1e
-	char *stat;
242c1e
-	if (asprintf(&stat, "/proc/%d/stat", pid) < 0)
242c1e
-		perror_msg_and_fail("asprintf");
242c1e
-
242c1e
+	char *stat = xasprintf("/proc/%d/stat", pid);
242c1e
 	FILE *fp = fopen(stat, "r");
242c1e
 	if (!fp)
242c1e
 		perror_msg_and_fail("fopen: %s", stat);
242c1e
-- 
242c1e
2.1.4
242c1e