From 2b6a340cb7cd6fd7a41db817e24aada826e2368c Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 13 Nov 2010 14:37:53 +0000 Subject: [PATCH] lib: Add safe_asprintf function. (cherry picked from commit a48ab78dc8272675caf7bebf17d2112aaaf6332f) --- src/guestfs-internal.h | 3 +++ src/guestfs.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 19d3258..93ad299 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -210,6 +210,8 @@ extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes); extern char *guestfs_safe_strdup (guestfs_h *g, const char *str); extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n); extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size); +extern char *guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...) + __attribute__((format (printf,2,3))); extern void guestfs___print_timestamped_argv (guestfs_h *g, const char *argv[]); extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...); extern void guestfs___free_inspect_info (guestfs_h *g); @@ -240,6 +242,7 @@ extern void guestfs___free_string_list (char **); #define safe_strdup guestfs_safe_strdup #define safe_strndup guestfs_safe_strndup #define safe_memdup guestfs_safe_memdup +#define safe_asprintf guestfs_safe_asprintf #ifdef HAVE_PCRE #define match guestfs___match #define match1 guestfs___match1 diff --git a/src/guestfs.c b/src/guestfs.c index c457b8c..d65bb9b 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -414,6 +414,22 @@ guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size) return p; } +char * +guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...) +{ + va_list args; + char *msg; + + va_start (args, fs); + int err = vasprintf (&msg, fs, args); + va_end (args); + + if (err == -1) + g->abort_cb (); + + return msg; +} + void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb) { -- 1.8.3.1