aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2008-12-31 07:13:17 -0500
committerMike Frysinger <vapier@gentoo.org>2008-12-31 09:52:26 -0500
commitc9f9ef7dc32c851b44f51b67912cf2d9a48b108f (patch)
tree4dc4420892900cd2799918055ccd517fb28eb039 /libsbutil
parentlibsandbox/libsbutil: link with -no-undefined (diff)
downloadsandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.tar.gz
sandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.tar.bz2
sandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.zip
libsbutil: cleanup x* memory functions
Pull the x* memory functions out of rcscripts and into libsbutil and change their style to match the rest of sbutil. Also add xzalloc() and xstrdup(), and convert pointless strndup() usage to strdup(). Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'libsbutil')
-rw-r--r--libsbutil/Makefile.am1
-rw-r--r--libsbutil/include/rcscripts/rcutil.h2
-rw-r--r--libsbutil/include/rcscripts/util/debug.h25
-rw-r--r--libsbutil/include/rcscripts/util/str_list.h2
-rw-r--r--libsbutil/include/rcscripts/util/string.h3
-rw-r--r--libsbutil/sb_memory.c88
-rw-r--r--libsbutil/sbutil.h19
-rw-r--r--libsbutil/src/config.c16
-rw-r--r--libsbutil/src/debug.c92
-rw-r--r--libsbutil/src/dynbuf.c14
-rw-r--r--libsbutil/src/string.c26
11 files changed, 113 insertions, 175 deletions
diff --git a/libsbutil/Makefile.am b/libsbutil/Makefile.am
index df2e10f..9de8b0a 100644
--- a/libsbutil/Makefile.am
+++ b/libsbutil/Makefile.am
@@ -25,6 +25,7 @@ libsbutil_la_SOURCES = \
sb_write.c \
sb_close.c \
sb_printf.c \
+ sb_memory.c \
include/rcscripts/rcutil.h \
include/rcscripts/util/str_list.h \
include/rcscripts/util/debug.h \
diff --git a/libsbutil/include/rcscripts/rcutil.h b/libsbutil/include/rcscripts/rcutil.h
index 895a141..248b84a 100644
--- a/libsbutil/include/rcscripts/rcutil.h
+++ b/libsbutil/include/rcscripts/rcutil.h
@@ -18,4 +18,6 @@
#include "rcscripts/util/str_list.h" /* used by libsandbox/execve wrapper */
+#include "sbutil.h"
+
#endif /* __RCUTIL_H__ */
diff --git a/libsbutil/include/rcscripts/util/debug.h b/libsbutil/include/rcscripts/util/debug.h
index a9f3e7b..8bc2371 100644
--- a/libsbutil/include/rcscripts/util/debug.h
+++ b/libsbutil/include/rcscripts/util/debug.h
@@ -14,10 +14,6 @@
#include <errno.h>
#include <stdio.h>
-#define save_errno() int old_errno = errno;
-#define restore_errno() errno = old_errno;
-#define saved_errno old_errno
-
void
rc_log_domain (const char *new_domain);
void
@@ -115,25 +111,4 @@ bool __check_arg_fp (FILE * fp, const char *file, const char *func, size_t line)
#define check_arg_fp(_fp) \
__check_arg_fp (_fp, __FILE__, __func__, __LINE__)
-/*
- * Various memory allocation functions and macro's.
- * They set errno to ENOMEM and print debug info.
- */
-
-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 *__xrealloc (void *ptr, size_t size, const char *file, const char *func, size_t line);
-
-#define xcalloc(_nmemb, _size) \
- __xcalloc (_nmemb, _size, __FILE__, __func__, __LINE__)
-#define xmalloc(_size) \
- __xmalloc (_size, __FILE__, __func__, __LINE__)
-#define xrealloc(_ptr, _size) \
- __xrealloc (_ptr, _size, __FILE__, __func__, __LINE__)
-
-char *__xstrndup (const char *str, size_t size, const char *file, const char *func, size_t line);
-
-#define xstrndup(_str, _size) \
- __xstrndup (_str, _size, __FILE__, __func__, __LINE__)
-
#endif /* __RC_DEBUG_H__ */
diff --git a/libsbutil/include/rcscripts/util/str_list.h b/libsbutil/include/rcscripts/util/str_list.h
index 068f902..ac287db 100644
--- a/libsbutil/include/rcscripts/util/str_list.h
+++ b/libsbutil/include/rcscripts/util/str_list.h
@@ -64,7 +64,7 @@
goto _error; \
} \
_string_list = _tmp_p; \
- _tmp_str = xstrndup (_item, strlen (_item)); \
+ _tmp_str = xstrdup (_item); \
if (NULL == _tmp_str) \
{ \
goto _error; \
diff --git a/libsbutil/include/rcscripts/util/string.h b/libsbutil/include/rcscripts/util/string.h
index 982d9a7..e7c1b2d 100644
--- a/libsbutil/include/rcscripts/util/string.h
+++ b/libsbutil/include/rcscripts/util/string.h
@@ -15,7 +15,4 @@
* with the malloc() call. */
char *rc_strcatpaths (const char *pathname1, const char *pathname2);
-/* Compat functions for GNU extensions */
-char *rc_strndup (const char *str, size_t size);
-
#endif /* __RC_STRING_H__ */
diff --git a/libsbutil/sb_memory.c b/libsbutil/sb_memory.c
new file mode 100644
index 0000000..23d74af
--- /dev/null
+++ b/libsbutil/sb_memory.c
@@ -0,0 +1,88 @@
+/*
+ * debug.c
+ *
+ * Simle debugging/logging macro's and functions.
+ *
+ * Copyright 1999-2008 Gentoo Foundation
+ * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
+ * Licensed under the GPL-2
+ */
+
+#include "headers.h"
+#include "sbutil.h"
+
+void *
+__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, size_t line)
+{
+ void *ret = calloc(nmemb, size);
+
+ if (ret == NULL) {
+ SB_EERROR("calloc()", " %s:%s():%zu: calloc(%zu, %zu) failed: %s\n",
+ file, func, line, nmemb, size, strerror(errno));
+ abort();
+ }
+
+ return ret;
+}
+
+void *
+__xmalloc(size_t size, const char *file, const char *func, size_t line)
+{
+ void *ret = malloc(size);
+
+ if (ret == NULL) {
+ SB_EERROR("malloc()", " %s:%s():%zu: malloc(%zu) failed: %s\n",
+ file, func, line, size, strerror(errno));
+ abort();
+ }
+
+ return ret;
+}
+
+void *
+__xzalloc(size_t size /*, const char *file, const char *func, size_t line */)
+{
+ return memset(xmalloc(size), 0x00, size);
+}
+
+void *
+__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line)
+{
+ void *ret = realloc(ptr, size);
+
+ if (ret == NULL) {
+ SB_EERROR("realloc()", " %s:%s():%zu: realloc(%p, %zu) failed: %s\n",
+ file, func, line, ptr, size, strerror(errno));
+ abort();
+ }
+
+ return ret;
+}
+
+char *
+__xstrdup(const char *str, const char *file, const char *func, size_t line)
+{
+ char *ret = strdup(str);
+
+ if (ret == NULL) {
+ SB_EERROR("strdup()", " %s:%s():%zu: strdup(%p) failed: %s\n",
+ file, func, line, str, strerror(errno));
+ abort();
+ }
+
+ return ret;
+}
+
+char *
+__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line)
+{
+ char *ret = strndup(str, size);
+
+ if (ret == NULL) {
+ SB_EERROR("strndup()", " %s:%s():%zu: strndup(%p, %zu) failed: %s\n",
+ file, func, line, str, size, strerror(errno));
+ abort();
+ }
+
+ return ret;
+}
diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h
index 6863288..9707215 100644
--- a/libsbutil/sbutil.h
+++ b/libsbutil/sbutil.h
@@ -110,4 +110,23 @@ __attribute__((__format__(__printf__, 3, 4))) void sb_efunc(const char *color, c
goto _error; \
} while (0)
+/* Memory functions */
+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 */);
+void *__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line);
+char *__xstrdup(const char *str, const char *file, const char *func, size_t line);
+char *__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line);
+#define xcalloc(_nmemb, _size) __xcalloc(_nmemb, _size, __FILE__, __func__, __LINE__)
+#define xmalloc(_size) __xmalloc(_size, __FILE__, __func__, __LINE__)
+#define xzalloc(_size) __xzalloc(_size /*, __FILE__, __func__, __LINE__ */)
+#define xrealloc(_ptr, _size) __xrealloc(_ptr, _size, __FILE__, __func__, __LINE__)
+#define xstrdup(_str) __xstrdup(_str, __FILE__, __func__, __LINE__)
+#define xstrndup(_str, _size) __xstrndup(_str, _size, __FILE__, __func__, __LINE__)
+
+/* errno helpers */
+#define save_errno() int old_errno = errno;
+#define restore_errno() errno = old_errno;
+#define saved_errno old_errno
+
#endif /* __SBUTIL_H__ */
diff --git a/libsbutil/src/config.c b/libsbutil/src/config.c
index b0284bf..29e333f 100644
--- a/libsbutil/src/config.c
+++ b/libsbutil/src/config.c
@@ -100,26 +100,12 @@ rc_get_cnf_entry (const char *pathname, const char *entry, const char *sep)
if (NULL != value)
free (value);
- value = xstrndup (token, strlen (token));
- if (NULL == value)
- {
- rc_dynbuf_free (dynbuf);
- free (buf);
-
- return NULL;
- }
+ value = xstrdup (token);
}
else
{
value = xrealloc (value, strlen(value) + strlen(token) +
strlen(sep) + 1);
- if (NULL == value)
- {
- rc_dynbuf_free (dynbuf);
- free (buf);
-
- return NULL;
- }
snprintf(value + strlen(value), strlen(token) + strlen(sep) + 1,
"%s%s", sep, token);
}
diff --git a/libsbutil/src/debug.c b/libsbutil/src/debug.c
index a8c8790..4806e51 100644
--- a/libsbutil/src/debug.c
+++ b/libsbutil/src/debug.c
@@ -57,17 +57,7 @@ debug_message (const char *file, const char *func, int line,
int length;
length = strlen (log_domain) + strlen ("(): ") + 1;
- /* Do not use xmalloc() here, else we may have recursive issues */
- format_str = malloc (length);
- if (NULL == format_str)
- {
- fprintf (stderr, "(%s) error: in %s, function %s(), line %i:\n",
- log_domain, __FILE__, __func__, __LINE__);
- fprintf (stderr, "(%s) Failed to allocate buffer!\n",
- log_domain);
- abort ();
- }
-
+ format_str = xmalloc (length);
snprintf (format_str, length, "(%s) ", log_domain);
va_start (arg, format);
@@ -219,83 +209,3 @@ __check_arg_fp (FILE *fp, const char *file, const char *func, size_t line)
return true;
}
-
-void *
-__xcalloc(size_t nmemb, size_t size, const char *file,
- const char *func, size_t line)
-{
- void *new_ptr;
-
- new_ptr = calloc (nmemb, size);
- if (NULL == new_ptr)
- {
- /* Set errno in case specific malloc() implementation does not */
- rc_errno_set (ENOMEM);
-
- debug_message (file, func, line, "Failed to allocate buffer!\n");
-
- return NULL;
- }
-
- return new_ptr;
-}
-
-void *
-__xmalloc (size_t size, const char *file, const char *func, size_t line)
-{
- void *new_ptr;
-
- new_ptr = malloc (size);
- if (NULL == new_ptr)
- {
- /* Set errno in case specific malloc() implementation does not */
- rc_errno_set (ENOMEM);
-
- debug_message (file, func, line, "Failed to allocate buffer!\n");
-
- return NULL;
- }
-
- return new_ptr;
-}
-
-void *
-__xrealloc (void *ptr, size_t size, const char *file,
- const char *func, size_t line)
-{
- void *new_ptr;
-
- new_ptr = realloc (ptr, size);
- if (NULL == new_ptr)
- {
- /* Set errno in case specific realloc() implementation does not */
- rc_errno_set (ENOMEM);
-
- debug_message (file, func, line, "Failed to reallocate buffer!\n");
-
- return NULL;
- }
-
- return new_ptr;
-}
-
-char *
-__xstrndup (const char *str, size_t size, const char *file,
- const char *func, size_t line)
-{
- char *new_ptr;
-
- new_ptr = rc_strndup (str, size);
- if (NULL == new_ptr)
- {
- /* Set errno in case specific realloc() implementation does not */
- rc_errno_set (ENOMEM);
-
- debug_message (file, func, line,
- "Failed to duplicate string via rc_strndup() !\n");
-
- return NULL;
- }
-
- return new_ptr;
-}
diff --git a/libsbutil/src/dynbuf.c b/libsbutil/src/dynbuf.c
index 3bd021b..604d85e 100644
--- a/libsbutil/src/dynbuf.c
+++ b/libsbutil/src/dynbuf.c
@@ -21,15 +21,7 @@ rc_dynbuf_new (void)
rc_dynbuf_t *dynbuf = NULL;
dynbuf = xmalloc (sizeof (rc_dynbuf_t));
- if (NULL == dynbuf)
- return NULL;
-
dynbuf->data = xmalloc (DYNAMIC_BUFFER_SIZE);
- if (NULL == dynbuf->data)
- {
- free (dynbuf);
- return NULL;
- }
dynbuf->length = DYNAMIC_BUFFER_SIZE;
dynbuf->rd_index = 0;
@@ -45,8 +37,6 @@ rc_dynbuf_new_mmap_file (const char *name)
rc_dynbuf_t *dynbuf = NULL;
dynbuf = xmalloc (sizeof (rc_dynbuf_t));
- if (NULL == dynbuf)
- return NULL;
if (-1 == rc_file_map (name, &dynbuf->data, &dynbuf->length))
{
@@ -90,8 +80,6 @@ rc_dynbuf_reallocate (rc_dynbuf_t *dynbuf, size_t needed)
len = dynbuf->length + DYNAMIC_BUFFER_SIZE;
new_ptr = xrealloc (dynbuf->data, len);
- if (NULL == new_ptr)
- return NULL;
dynbuf->data = new_ptr;
dynbuf->length = len;
@@ -393,8 +381,6 @@ rc_dynbuf_read_line (rc_dynbuf_t *dynbuf)
{
buf = xstrndup ((dynbuf->data + dynbuf->rd_index),
(count - dynbuf->rd_index));
- if (NULL == buf)
- return NULL;
dynbuf->rd_index = count;
diff --git a/libsbutil/src/string.c b/libsbutil/src/string.c
index 71e7213..eec6f85 100644
--- a/libsbutil/src/string.c
+++ b/libsbutil/src/string.c
@@ -24,8 +24,6 @@ rc_strcatpaths (const char *pathname1, const char *pathname2)
lenght = strlen (pathname1) + strlen (pathname2) + 2;
/* lenght + '\0' */
new_path = xmalloc (lenght);
- if (NULL == new_path)
- return NULL;
snprintf (new_path, lenght, "%s%s%s", pathname1,
(pathname1[strlen (pathname1) - 1] != '/') ? "/" : "",
@@ -33,27 +31,3 @@ rc_strcatpaths (const char *pathname1, const char *pathname2)
return new_path;
}
-
-char *
-rc_strndup (const char *str, size_t size)
-{
- char *new_str = NULL;
- size_t len;
-
- /* We cannot check if its a valid string here, as it might
- * not be '\0' terminated ... */
- if (!check_arg_ptr (str))
- return NULL;
-
- /* Check lenght of str without breaching the size limit */
- for (len = 0; (len < size) && ('\0' != str[len]); len++);
-
- new_str = xmalloc (len + 1);
- if (NULL == new_str)
- return NULL;
-
- /* Make sure our string is NULL terminated */
- new_str[len] = '\0';
-
- return (char *) memcpy (new_str, str, len);
-}