From: Karel Zak Date: Thu, 2 Mar 2023 13:16:33 +0000 (+0100) Subject: lsfd: use xstrdup() if included xalloc.h X-Git-Tag: v2.39-rc1~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e0dc84daeb8029957de3bf0d357516e3a0275c93;p=thirdparty%2Futil-linux.git lsfd: use xstrdup() if included xalloc.h This commit improves code consistence. Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsfd-bdev.c b/misc-utils/lsfd-bdev.c index b2fecdc8dc..7be99dbde4 100644 --- a/misc-utils/lsfd-bdev.c +++ b/misc-utils/lsfd-bdev.c @@ -50,7 +50,7 @@ static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)), case COL_BLKDRV: devdrv = get_blkdrv(major(file->stat.st_rdev)); if (devdrv) - str = strdup(devdrv); + str = xstrdup(devdrv); else xasprintf(&str, "%u", major(file->stat.st_rdev)); @@ -64,7 +64,7 @@ static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)), case COL_PARTITION: partition = get_partition(file->stat.st_rdev); if (partition) { - str = strdup(partition); + str = xstrdup(partition); break; } devdrv = get_blkdrv(major(file->stat.st_rdev)); diff --git a/misc-utils/lsfd-cdev.c b/misc-utils/lsfd-cdev.c index 795536aca3..4e35f1543f 100644 --- a/misc-utils/lsfd-cdev.c +++ b/misc-utils/lsfd-cdev.c @@ -53,7 +53,7 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)), if (devdrv && strcmp(devdrv, "misc") == 0) { miscdev = get_miscdev(minor(file->stat.st_rdev)); if (miscdev) - str = strdup(miscdev); + str = xstrdup(miscdev); else xasprintf(&str, "%u", minor(file->stat.st_rdev)); @@ -68,7 +68,7 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)), case COL_CHRDRV: devdrv = get_chrdrv(major(file->stat.st_rdev)); if (devdrv) - str = strdup(devdrv); + str = xstrdup(devdrv); else xasprintf(&str, "%u", major(file->stat.st_rdev)); diff --git a/misc-utils/lsfd-fifo.c b/misc-utils/lsfd-fifo.c index 484ffd6ebc..f736192577 100644 --- a/misc-utils/lsfd-fifo.c +++ b/misc-utils/lsfd-fifo.c @@ -61,7 +61,7 @@ static bool fifo_fill_column(struct proc *proc __attribute__((__unused__)), case COL_SOURCE: if (major(file->stat.st_dev) == 0 && strncmp(file->name, "pipe:", 5) == 0) { - str = strdup("pipefs"); + str = xstrdup("pipefs"); break; } return false; diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index f62aa7cc50..bdaac3fd8d 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -190,7 +190,7 @@ static bool file_fill_column(struct proc *proc, case COL_PARTITION: partition = get_partition(file->stat.st_dev); if (partition) { - str = strdup(partition); + str = xstrdup(partition); break; } /* FALL THROUGH */ diff --git a/misc-utils/lsfd-sock-xinfo.c b/misc-utils/lsfd-sock-xinfo.c index 9b154ba19c..4e2d7cce68 100644 --- a/misc-utils/lsfd-sock-xinfo.c +++ b/misc-utils/lsfd-sock-xinfo.c @@ -310,7 +310,7 @@ static char *unix_get_type(struct sock_xinfo *sock_xinfo, struct unix_xinfo *ux = (struct unix_xinfo *)sock_xinfo; str = sock_decode_type(ux->type); - return strdup(str); + return xstrdup(str); } static char *unix_get_state(struct sock_xinfo *sock_xinfo, @@ -320,10 +320,10 @@ static char *unix_get_state(struct sock_xinfo *sock_xinfo, struct unix_xinfo *ux = (struct unix_xinfo *)sock_xinfo; if (ux->acceptcon) - return strdup("listen"); + return xstrdup("listen"); str = unix_decode_state(ux->st); - return strdup(str); + return xstrdup(str); } static bool unix_get_listening(struct sock_xinfo *sock_xinfo, @@ -347,7 +347,7 @@ static bool unix_fill_column(struct proc *proc __attribute__((__unused__)), switch (column_id) { case COL_UNIX_PATH: if (*ux->path) { - *str = strdup(ux->path); + *str = xstrdup(ux->path); return true; } break; @@ -538,7 +538,7 @@ struct l4_xinfo_class { } \ \ if (n && inet_ntop(class->family, n, s, sizeof(s))) { \ - *STR = strdup(s); \ + *STR = xstrdup(s); \ r = true; \ } \ r; \ @@ -586,13 +586,13 @@ static char *tcp_get_name(struct sock_xinfo *sock_xinfo, static char *tcp_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { - return strdup("stream"); + return xstrdup("stream"); } static char *tcp_get_state(struct sock_xinfo *sock_xinfo, struct sock *sock __attribute__((__unused__))) { - return strdup(l4_decode_state(((struct l4_xinfo *)sock_xinfo)->st)); + return xstrdup(l4_decode_state(((struct l4_xinfo *)sock_xinfo)->st)); } static bool tcp_get_listening(struct sock_xinfo *sock_xinfo, @@ -811,7 +811,7 @@ static char *udp_get_name(struct sock_xinfo *sock_xinfo, static char *udp_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { - return strdup("dgram"); + return xstrdup("dgram"); } static bool udp_fill_column(struct proc *proc __attribute__((__unused__)), @@ -934,7 +934,7 @@ static char *raw_get_name(struct sock_xinfo *sock_xinfo, static char *raw_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { - return strdup("raw"); + return xstrdup("raw"); } static bool raw_fill_column(struct proc *proc __attribute__((__unused__)), @@ -1028,7 +1028,7 @@ static char *ping_get_name(struct sock_xinfo *sock_xinfo, static char *ping_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { - return strdup("dgram"); + return xstrdup("dgram"); } static bool ping_fill_column(struct proc *proc __attribute__((__unused__)), @@ -1467,7 +1467,7 @@ static char *netlink_get_name(struct sock_xinfo *sock_xinfo, static char *netlink_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { - return strdup("raw"); + return xstrdup("raw"); } static bool netlink_fill_column(struct proc *proc __attribute__((__unused__)), @@ -1488,7 +1488,7 @@ static bool netlink_fill_column(struct proc *proc __attribute__((__unused__)), xasprintf(str, "%"PRIu32, nl->lportid); return true; case COL_NETLINK_PROTOCOL: - *str = strdup(netlink_decode_protocol(nl->protocol)); + *str = xstrdup(netlink_decode_protocol(nl->protocol)); return true; } diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index 83f35e5d4f..32645169c8 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -64,7 +64,7 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)), case COL_SOURCE: if (major(file->stat.st_dev) == 0 && strncmp(file->name, "socket:", 7) == 0) { - str = strdup("sockfs"); + str = xstrdup("sockfs"); break; } return false; diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index f4bba88178..087e31dad2 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -54,7 +54,7 @@ static char * anon_get_class(struct unkn *unkn) char *name; if (unkn->anon_ops->class) - return strdup(unkn->anon_ops->class); + return xstrdup(unkn->anon_ops->class); /* See unkn_init_content() */ name = ((struct file *)unkn)->name + 11; @@ -65,7 +65,7 @@ static char * anon_get_class(struct unkn *unkn) return strndup(name + 1, len - 1); } - return strdup(name); + return xstrdup(name); } static bool unkn_fill_column(struct proc *proc, @@ -97,7 +97,7 @@ static bool unkn_fill_column(struct proc *proc, return false; case COL_SOURCE: if (unkn->anon_ops) { - str = strdup("anon_inodefs"); + str = xstrdup("anon_inodefs"); break; } return false; @@ -232,14 +232,14 @@ static bool anon_pidfd_fill_column(struct proc *proc __attribute__((__unused__) if (pidfd_proc) pidfd_comm = pidfd_proc->command; if (pidfd_comm) { - *str = strdup(pidfd_comm); + *str = xstrdup(pidfd_comm); return true; } break; } case COL_PIDFD_NSPID: if (data->nspid) { - *str = strdup(data->nspid); + *str = xstrdup(data->nspid); return true; } break; diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index 601cd3dcc8..8a67e55681 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -736,11 +736,7 @@ static void *make_mmapped_packet_socket(const struct factory *factory, struct fd "failed to specify a buffer spec to a packet socket"); } - munmap_data = malloc(sizeof (*munmap_data)); - if (munmap_data == NULL) { - close(sd); - errx(EXIT_FAILURE, "memory exhausted"); - } + munmap_data = xmalloc(sizeof (*munmap_data)); munmap_data->len = (size_t) req.tp_block_size * req.tp_block_nr; munmap_data->ptr = mmap(NULL, munmap_data->len, PROT_WRITE, MAP_SHARED, sd, 0); if (munmap_data->ptr == MAP_FAILED) {