Dustin Kirkland: Fail overtly when asprintf fails to allocate memory

Martin F. Krafft madduck at alioth.debian.org
Tue Feb 3 10:10:09 UTC 2009


Module: mdadm
Branch: upstream
Commit: 1a0ee0baf049809f9e67d60bb8158801eada3bd5
URL:    http://git.debian.org/?p=pkg-mdadm/mdadm.git;a=commit;h=1a0ee0baf049809f9e67d60bb8158801eada3bd5

Author: Dustin Kirkland <kirkland at canonical.com>
Date:   Thu Jan  8 09:25:33 2009 +1100

Fail overtly when asprintf fails to allocate memory

.. rather that causing a less-obvious violation of segments.

Signed-off-by: NeilBrown <neilb at suse.de>

---

 Assemble.c |    4 ++--
 config.c   |    2 +-
 mdadm.h    |   11 +++++++++++
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/Assemble.c b/Assemble.c
index 0cdeeda..ab8faed 100644
--- a/Assemble.c
+++ b/Assemble.c
@@ -386,9 +386,9 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
 		if (c) c++; else c= info.name;
 		if (isdigit(*c) && ((ident->autof & 7)==4 || (ident->autof&7)==6))
 			/* /dev/md/d0 style for partitionable */
-			asprintf(&mddev, "/dev/md/d%s", c);
+			xasprintf(&mddev, "/dev/md/d%s", c);
 		else
-			asprintf(&mddev, "/dev/md/%s", c);
+			xasprintf(&mddev, "/dev/md/%s", c);
 		mdfd = open_mddev(mddev, ident->autof);
 		if (mdfd < 0) {
 			st->ss->free_super(st);
diff --git a/config.c b/config.c
index 121b337..78bbb9d 100644
--- a/config.c
+++ b/config.c
@@ -559,7 +559,7 @@ void mailfromline(char *line)
 			alert_mail_from = strdup(w);
 		else {
 			char *t= NULL;
-			asprintf(&t, "%s %s", alert_mail_from, w);
+			xasprintf(&t, "%s %s", alert_mail_from, w);
 			free(alert_mail_from);
 			alert_mail_from = t;
 		}
diff --git a/mdadm.h b/mdadm.h
index bc4b38e..b0a8c5e 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -528,6 +528,17 @@ extern int open_mddev(char *dev, int autof);
 extern int open_mddev_devnum(char *devname, int devnum, char *name,
 			     char *chosen_name, int parts);
 
+#include <assert.h>
+#include <stdarg.h>
+static inline int xasprintf(char **strp, const char *fmt, ...) {
+	va_list ap;
+	int ret;
+	va_start(ap, fmt);
+	ret = asprintf(strp, fmt, ap);
+	va_end(ap);
+	assert(ret >= 0);
+	return ret;
+}
 
 #define	LEVEL_MULTIPATH		(-4)
 #define	LEVEL_LINEAR		(-1)




More information about the pkg-mdadm-commits mailing list