From a48ab78dc8272675caf7bebf17d2112aaaf6332f 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. --- 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 65ca4f2..5ac4dcf 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -237,6 +237,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); @@ -269,6 +271,7 @@ extern void guestfs___rollback_cmdline (guestfs_h *g, int pos); #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 b151226..75228d3 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -427,6 +427,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