From 90847fc8818836a296e9d500725c0eb154a4d3c5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 26 Aug 2006 06:25:57 +0000 Subject: Replace strdup and {c,re,m}alloc with x* variants to check for OOM errors I'm checking for zero-size allocations and assert()-ing them, so we can more easily get backtraces and debug problems, but we'll also allow -DNDEBUG people to live on the edge if they wish. We do not rely on errno when checking for OOM errors because some implementations of malloc do not set it, and malloc is commonly overridden by userspace wrappers. I've spent some time looking through the source and didn't find any obvious places where we would explicitly allocate 0 bytes, so we shouldn't trip any of those assertions. We also avoid allocating zero bytes because C libraries don't handle this consistently (some return NULL, some not); and it's dangerous either way. git-svn-id: https://svn.musicpd.org/mpd/trunk@4690 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/path.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index b05843514..600660abc 100644 --- a/src/path.c +++ b/src/path.c @@ -21,6 +21,7 @@ #include "charConv.h" #include "conf.h" #include "utf8.h" +#include "utils.h" #include #include @@ -69,7 +70,7 @@ char *utf8ToFsCharset(char *str) ret = pathConvCharset(fsCharset, "UTF-8", str, ret); if (!ret) - ret = strdup(str); + ret = xstrdup(str); return ret; } @@ -81,7 +82,7 @@ void setFsCharset(char *charset) if (fsCharset) free(fsCharset); - fsCharset = strdup(charset); + fsCharset = xstrdup(charset); DEBUG("setFsCharset: fs charset is: %s\n", fsCharset); @@ -101,7 +102,7 @@ void setFsCharset(char *charset) if (error) { free(fsCharset); WARNING("setting fs charset to ISO-8859-1!\n"); - fsCharset = strdup("ISO-8859-1"); + fsCharset = xstrdup("ISO-8859-1"); } } @@ -116,7 +117,7 @@ static char *appendSlash(char **path) int len = strlen(temp); if (temp[len - 1] != '/') { - temp = malloc(len + 2); + temp = xmalloc(len + 2); memset(temp, 0, len + 2); memcpy(temp, *path, len); temp[len] = '/'; @@ -157,14 +158,14 @@ void initPaths(void) closedir(dir); if (fsCharsetParam) { - charset = strdup(fsCharsetParam->value); + charset = xstrdup(fsCharsetParam->value); } #ifdef HAVE_LOCALE #ifdef HAVE_LANGINFO_CODESET else if ((originalLocale = setlocale(LC_CTYPE, NULL))) { char *temp; char *currentLocale; - originalLocale = strdup(originalLocale); + originalLocale = xstrdup(originalLocale); if (!(currentLocale = setlocale(LC_CTYPE, ""))) { WARNING("problems setting current locale with " @@ -175,7 +176,7 @@ void initPaths(void) WARNING("current locale is \"%s\"\n", currentLocale); } else if ((temp = nl_langinfo(CODESET))) { - charset = strdup(temp); + charset = xstrdup(temp); } else WARNING ("problems getting charset for locale\n"); @@ -273,7 +274,7 @@ char *parentPath(char *path) char *sanitizePathDup(char *path) { int len = strlen(path) + 1; - char *ret = malloc(len); + char *ret = xmalloc(len); char *cp = ret; memset(ret, 0, len); @@ -307,5 +308,5 @@ char *sanitizePathDup(char *path) DEBUG("sanitized: %s\n", ret); - return realloc(ret, len + 1); + return xrealloc(ret, len + 1); } -- cgit v1.2.3