commit:     2f99599214eeb36b329ddc90559c17210312f7d1
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Fri Nov  5 20:56:28 2021 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Nov  5 20:56:28 2021 +0000
URL:        https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=2f995992

sandbox: move xasprintf helper here

Since this is only used by sandbox, and is not usable by libsandbox,
move it out of libsbutil.  Leave a note behind for possible future
macros too.

Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

 libsbutil/sbutil.h | 17 ++++++++---------
 src/sandbox.h      |  8 ++++++++
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h
index cf97179..c146b80 100644
--- a/libsbutil/sbutil.h
+++ b/libsbutil/sbutil.h
@@ -140,7 +140,14 @@ void sb_maybe_gdb(void);
 #define sb_fprintf(fp, ...) sb_fdprintf(fileno(fp), __VA_ARGS__)
 #define sb_vfprintf(fp, ...) sb_vfdprintf(fileno(fp), __VA_ARGS__)
 
-/* Memory functions */
+/*
+ * Memory functions.
+ *
+ * NB: These are wrappers around libsbutil functions that build off memory 
calls that we
+ * implement directly (see libsandbox/memory.c).  Do not add any helpers here 
that cannot
+ * be mirrored in libsandbox as attempts to pass memory between the two 
allocators will
+ * lead to corruption & crashes.
+ */
 void *__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, 
size_t line);
 void *__xmalloc(size_t size, const char *file, const char *func, size_t line);
 void *__xzalloc(size_t size /*, const char *file, const char *func, size_t 
line */);
@@ -155,14 +162,6 @@ char *__xstrndup(const char *str, size_t size, const char 
*file, const char *fun
 #define xstrndup(_str, _size)  __xstrndup(_str, _size, __FILE__, __func__, 
__LINE__)
 #define xalloc_die()           __sb_ebort(__FILE__, __func__, __LINE__, "out 
of memory")
 
-#define xasprintf(fmt, ...) \
-({ \
-       int _ret = asprintf(fmt, __VA_ARGS__); \
-       if (_ret == 0) \
-               sb_perr("asprintf(%s) failed", #fmt); \
-       _ret; \
-})
-
 /* string helpers */
 #define streq(s1, s2) (strcmp(s1, s2) == 0)
 

diff --git a/src/sandbox.h b/src/sandbox.h
index 28961f5..477973a 100644
--- a/src/sandbox.h
+++ b/src/sandbox.h
@@ -40,6 +40,14 @@ extern pid_t setup_namespaces(void);
 #define sb_err(fmt, args...)  _sb_err(warn, fmt, ## args)
 #define sb_perr(fmt, args...) _sb_err(pwarn, fmt, ## args)
 
+#define xasprintf(fmt, ...) \
+({ \
+       int _ret = asprintf(fmt, __VA_ARGS__); \
+       if (_ret == 0) \
+               sb_perr("asprintf(%s) failed", #fmt); \
+       _ret; \
+})
+
 /* Option parsing related code */
 extern void parseargs(int argc, char *argv[]);
 extern int opt_use_namespaces;

Reply via email to