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/src
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/src')
-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
4 files changed, 2 insertions, 146 deletions
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);
-}