Blame SOURCES/0145-xmalloc-introduce-xasprintf.patch

6f827f
From f2b8769e4a9aa99cd5e0aef41a0408c34de3cc8e Mon Sep 17 00:00:00 2001
6f827f
From: "Dmitry V. Levin" <ldv@strace.io>
6f827f
Date: Mon, 15 Mar 2021 08:00:00 +0000
6f827f
Subject: [PATCH 145/149] xmalloc: introduce xasprintf
6f827f
6f827f
xasprintf is a trivial wrapper around vasprintf,
6f827f
the primary purpose of adding it is to simplify tests.
6f827f
6f827f
* xmalloc.h (xasprintf): New function declaration.
6f827f
* xmalloc.c: Include <stdarg.h> and <stdio.h>
6f827f
(xasprintf): New function.
6f827f
---
6f827f
 xmalloc.c | 16 ++++++++++++++++
6f827f
 xmalloc.h |  6 ++++++
6f827f
 2 files changed, 22 insertions(+)
6f827f
6f827f
diff --git a/xmalloc.c b/xmalloc.c
6f827f
index 8688f91..75019e9 100644
6f827f
--- a/xmalloc.c
6f827f
+++ b/xmalloc.c
6f827f
@@ -10,6 +10,8 @@
6f827f
 # include "config.h"
6f827f
 #endif
6f827f
 
6f827f
+#include <stdarg.h>
6f827f
+#include <stdio.h>
6f827f
 #include <stdlib.h>
6f827f
 #include <string.h>
6f827f
 
6f827f
@@ -146,3 +148,17 @@ xstrndup(const char *str, size_t n)
6f827f
 
6f827f
 	return p;
6f827f
 }
6f827f
+
6f827f
+char *
6f827f
+xasprintf(const char *fmt, ...)
6f827f
+{
6f827f
+	va_list ap;
6f827f
+	va_start(ap, fmt);
6f827f
+
6f827f
+	char *res;
6f827f
+	if (vasprintf(&res, fmt, ap) < 0)
6f827f
+		die_out_of_memory();
6f827f
+
6f827f
+	va_end(ap);
6f827f
+	return res;
6f827f
+}
6f827f
diff --git a/xmalloc.h b/xmalloc.h
6f827f
index 4bf1e2c..1305a1e 100644
6f827f
--- a/xmalloc.h
6f827f
+++ b/xmalloc.h
6f827f
@@ -77,4 +77,10 @@ void *xgrowarray(void *ptr, size_t *nmemb, size_t memb_size);
6f827f
 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
6f827f
 char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
6f827f
 
6f827f
+/**
6f827f
+ * Analogous to asprintf, die in case of an error.
6f827f
+ */
6f827f
+char *xasprintf(const char *fmt, ...)
6f827f
+	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_MALLOC;
6f827f
+
6f827f
 #endif /* !STRACE_XMALLOC_H */
6f827f
-- 
6f827f
2.1.4
6f827f