From 1eeed64dd0559bb2abd7e445418898beccf32198 Mon Sep 17 00:00:00 2001 From: Otavio Pontes Date: Tue, 26 Jun 2018 15:40:52 +0000 Subject: [PATCH] helpers: Add a strdup_or_die() function Add a helper similar to string_or_die() that performs a strdup and abort on errors. This function have the same behaviour as string_or_die with format set to "%s". --- src/helpers.c | 15 +++++++++++++++ src/swupd.h | 1 + 2 files changed, 16 insertions(+) diff --git a/src/helpers.c b/src/helpers.c index 5e648f85..5f551b33 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -584,6 +584,21 @@ out_fds: return ret; } +/* Return a duplicated copy of the string using strdup(). + * Abort if there's no memory to allocate the new string. +*/ +char *strdup_or_die(const char *const str) +{ + char *result; + + result = strdup(str); + if (!result) { + abort(); + } + + return result; +} + void string_or_die(char **strp, const char *fmt, ...) { va_list ap; diff --git a/src/swupd.h b/src/swupd.h index d31273a1..dc7406b7 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -357,6 +357,7 @@ extern void print_manifest_files(struct manifest *m); extern void swupd_deinit(int lock_fd, struct list **subs); extern int swupd_init(int *lock_fd); extern void string_or_die(char **strp, const char *fmt, ...); +char *strdup_or_die(const char *const str); extern void free_string(char **s); void update_motd(int new_release); void delete_motd(void);