summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry V. Levin <ldv@strace.io>2021-03-15 08:00:00 +0000
committerDmitry V. Levin <ldv@strace.io>2021-03-15 08:00:00 +0000
commit0a9a3ecb56786ccbaec7c631016be6ca909aa7e9 (patch)
treed20483ef2ea50a625bd67d0bb2e626928f70863d
parentac47e2391c80f7caf10e8e89a11ee2ae8940447a (diff)
downloadstrace-0a9a3ecb56786ccbaec7c631016be6ca909aa7e9.tar.gz
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.
-rw-r--r--tests/clone-flags.c5
-rw-r--r--tests/epoll_pwait2.c11
-rw-r--r--tests/faccessat.c17
-rw-r--r--tests/faccessat2.c17
-rw-r--r--tests/ioctl_kvm_run_common.c6
-rw-r--r--tests/keyctl.c29
-rw-r--r--tests/lock_file.c5
-rw-r--r--tests/mq.c5
-rw-r--r--tests/mq_sendrecv.c4
-rw-r--r--tests/netlink_protocol.c5
-rw-r--r--tests/old_mmap.c5
-rw-r--r--tests/tracer_ppid_pgid_sid.c6
12 files changed, 44 insertions, 71 deletions
diff --git a/tests/clone-flags.c b/tests/clone-flags.c
index 2e026fbaf..447e0850f 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>
@@ -131,9 +132,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/epoll_pwait2.c b/tests/epoll_pwait2.c
index f6a8df3fe..d88a21c02 100644
--- a/tests/epoll_pwait2.c
+++ b/tests/epoll_pwait2.c
@@ -9,6 +9,7 @@
#include "tests.h"
#include "scno.h"
+#include "xmalloc.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -55,12 +56,10 @@ main(void)
int fd = open(fd_path, O_WRONLY);
if (fd < 0)
perror_msg_and_fail("open: %s", fd_path);
- char *fd_str;
- if (asprintf(&fd_str, "%d%s%s%s", fd,
- DECODE_FDS ? "<" : "",
- DECODE_FDS ? fd_path : "",
- DECODE_FDS ? ">" : "") < 0)
- perror_msg_and_fail("asprintf");
+ char *fd_str = xasprintf("%d%s%s%s", fd,
+ DECODE_FDS ? "<" : "",
+ DECODE_FDS ? fd_path : "",
+ DECODE_FDS ? ">" : "");
TAIL_ALLOC_OBJECT_CONST_PTR(struct epoll_event, events);
TAIL_ALLOC_OBJECT_CONST_PTR(kernel_timespec64_t, timeout);
diff --git a/tests/faccessat.c b/tests/faccessat.c
index b5498bc72..670e9b21f 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/faccessat2.c b/tests/faccessat2.c
index 74d17cd59..e8d24c453 100644
--- a/tests/faccessat2.c
+++ b/tests/faccessat2.c
@@ -12,6 +12,7 @@
#ifdef __NR_faccessat2
+# include "xmalloc.h"
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
@@ -54,13 +55,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;
@@ -81,12 +78,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 9107c30c0..be1190e68 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 9f1f93cc7..ed6f7fdf9 100644
--- a/tests/keyctl.c
+++ b/tests/keyctl.c
@@ -10,6 +10,7 @@
#include "tests.h"
#include "scno.h"
+#include "xmalloc.h"
#include <assert.h>
#include <errno.h>
@@ -389,7 +390,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;
@@ -416,21 +416,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 56cf112d6..7618552ab 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 6fb624a55..cc598b38c 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 ff44782ec..64d4f6640 100644
--- a/tests/mq_sendrecv.c
+++ b/tests/mq_sendrecv.c
@@ -25,6 +25,7 @@
# include <time.h>
# include <unistd.h>
+# include "xmalloc.h"
# include "kernel_fcntl.h"
# include "sigevent.h"
@@ -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 1ae257ce8..cc2cc3000 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)
@@ -407,9 +408,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 25d01d5aa..36a14e2fb 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 151cd6704..f9dbcd6d9 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);