strace/SOURCES/0146-tests-use-xasprintf-in...

892 lines
26 KiB
Diff

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