Add strdup_or_error()

Add a helper function to handle the impossible event that strdup fails.

Signed-off-by: Andy Grover <agrover@versity.com>
This commit is contained in:
Andy Grover
2021-01-04 11:49:31 -08:00
parent 9e47a32257
commit a3035582d3
+10
View File
@@ -2,6 +2,7 @@
#define _PARSE_H_
#include <sys/time.h>
#include <argp.h>
int parse_human(char* str, u64 *val_ret);
int parse_u64(char *str, u64 *val_ret);
@@ -9,4 +10,13 @@ int parse_s64(char *str, s64 *val_ret);
int parse_u32(char *str, u32 *val_ret);
int parse_timespec(char *str, struct timespec *ts);
static inline char* strdup_or_error(const struct argp_state *state, char *str)
{
char *new = strdup(str);
if (!new)
argp_error(state, "memory allocation failed");
return new;
}
#endif