From 3905cc7ab496549b6ca0a0f67ec61d1d16968c33 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 17 Aug 2010 11:26:26 +0100 Subject: [PATCH] Add safe_strndup call. --- src/guestfs-internal.h | 2 ++ src/guestfs.c | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 12ca0ec..d0fc3ca 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -141,6 +141,7 @@ extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...) __attribute__((format (printf,2,3))); 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 int guestfs___set_busy (guestfs_h *g); extern int guestfs___end_busy (guestfs_h *g); @@ -157,6 +158,7 @@ extern int guestfs___accept_from_daemon (guestfs_h *g); #define safe_malloc guestfs_safe_malloc #define safe_realloc guestfs_safe_realloc #define safe_strdup guestfs_safe_strdup +#define safe_strndup guestfs_safe_strndup #define safe_memdup guestfs_safe_memdup #endif /* GUESTFS_INTERNAL_H_ */ diff --git a/src/guestfs.c b/src/guestfs.c index c54462d..871d713 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -397,6 +397,14 @@ guestfs_safe_strdup (guestfs_h *g, const char *str) return s; } +char * +guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n) +{ + char *s = strndup (str, n); + if (!s) g->abort_cb (); + return s; +} + void * guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size) { -- 1.8.3.1