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/audio.c | 6 ++-- src/audioOutput.c | 2 +- src/audioOutput.h | 1 + src/audioOutputs/audioOutput_alsa.c | 6 ++-- src/audioOutputs/audioOutput_ao.c | 6 ++-- src/audioOutputs/audioOutput_mvp.c | 2 +- src/audioOutputs/audioOutput_oss.c | 10 +++---- src/audioOutputs/audioOutput_osx.c | 4 +-- src/audioOutputs/audioOutput_pulse.c | 6 ++-- src/audioOutputs/audioOutput_shout.c | 2 +- src/buffer2array.c | 12 ++++---- src/charConv.c | 19 ++++++------ src/command.c | 3 +- src/compress.c | 3 +- src/conf.c | 14 ++++----- src/dbUtils.c | 10 +++---- src/decode.c | 4 +-- src/directory.c | 36 +++++++++++----------- src/gcc.h | 1 - src/inputPlugins/aac_plugin.c | 2 +- src/inputPlugins/mod_plugin.c | 6 ++-- src/inputPlugins/mp3_plugin.c | 6 ++-- src/inputPlugins/mp4_plugin.c | 6 ++-- src/inputStream_http.c | 40 ++++++++++++------------- src/interface.c | 6 ++-- src/list.c | 29 +++++++++--------- src/listen.c | 2 +- src/log.h | 3 +- src/ls.c | 3 +- src/main.c | 2 +- src/outputBuffer.c | 2 +- src/path.c | 19 ++++++------ src/path.h | 2 +- src/pcm_utils.c | 7 +++-- src/permission.c | 3 +- src/player.c | 8 ++--- src/playlist.c | 22 +++++++------- src/replayGain.c | 3 +- src/sllist.c | 6 ++-- src/sllist.h | 2 +- src/song.c | 11 +++---- src/tag.c | 14 ++++----- src/tagTracker.c | 6 ++-- src/tree.c | 5 ++-- src/utf8.c | 9 +++--- src/utils.c | 58 ++++++++++++++++++++++++++++++++++-- src/utils.h | 9 ++++++ src/volume.c | 2 +- 48 files changed, 257 insertions(+), 183 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index a5b5b60b6..4eb1c4a38 100644 --- a/src/audio.c +++ b/src/audio.c @@ -119,7 +119,7 @@ void initAudioDriver(void) audioOutputArraySize = audio_device_count(); audioDeviceStates = (getPlayerData())->audioDeviceStates; - audioOutputArray = malloc(sizeof(AudioOutput) * audioOutputArraySize); + audioOutputArray = xmalloc(sizeof(AudioOutput) * audioOutputArraySize); i = 0; param = getNextConfigParam(CONF_AUDIO_OUTPUT, param); @@ -162,7 +162,7 @@ void initAudioConfig(void) if (NULL == param || NULL == param->value) return; - audio_configFormat = malloc(sizeof(AudioFormat)); + audio_configFormat = xmalloc(sizeof(AudioFormat)); if (0 != parseAudioConfig(audio_configFormat, param->value)) { ERROR("error parsing \"%s\" at line %i\n", @@ -335,7 +335,7 @@ int openAudioDevice(AudioFormat * audioFormat) audioBufferSize = (audio_format.bits >> 3) * audio_format.channels; audioBufferSize *= audio_format.sampleRate >> 5; - audioBuffer = realloc(audioBuffer, audioBufferSize); + audioBuffer = xrealloc(audioBuffer, audioBufferSize); } syncAudioDeviceStates(); diff --git a/src/audioOutput.c b/src/audioOutput.c index 60ae9b772..f4750b8df 100644 --- a/src/audioOutput.c +++ b/src/audioOutput.c @@ -198,7 +198,7 @@ static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr, if (size > audioOutput->convBufferLen) { audioOutput->convBuffer = - realloc(audioOutput->convBuffer, size); + xrealloc(audioOutput->convBuffer, size); audioOutput->convBufferLen = size; } diff --git a/src/audioOutput.h b/src/audioOutput.h index 1dbcbcfd8..bc318dea2 100644 --- a/src/audioOutput.h +++ b/src/audioOutput.h @@ -25,6 +25,7 @@ #include "audio.h" #include "tag.h" #include "conf.h" +#include "utils.h" #define DISABLED_AUDIO_OUTPUT_PLUGIN(plugin) \ AudioOutputPlugin plugin = { \ diff --git a/src/audioOutputs/audioOutput_alsa.c b/src/audioOutputs/audioOutput_alsa.c index 31601b37c..85872c09a 100644 --- a/src/audioOutputs/audioOutput_alsa.c +++ b/src/audioOutputs/audioOutput_alsa.c @@ -59,7 +59,7 @@ typedef struct _AlsaData { static AlsaData *newAlsaData(void) { - AlsaData *ret = malloc(sizeof(AlsaData)); + AlsaData *ret = xmalloc(sizeof(AlsaData)); ret->device = NULL; ret->pcmHandle = NULL; @@ -85,7 +85,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param) if (param) { BlockParam *bp = getBlockParam(param, "device"); - ad->device = bp ? strdup(bp->value) : strdup("default"); + ad->device = bp ? xstrdup(bp->value) : xstrdup("default"); if ((bp = getBlockParam(param, "use_mmap")) && (!strcasecmp(bp->value, "yes") || @@ -96,7 +96,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param) if ((bp = getBlockParam(param, "period_time"))) ad->period_time = atoi(bp->value); } else - ad->device = strdup("default"); + ad->device = xstrdup("default"); audioOutput->data = ad; return 0; diff --git a/src/audioOutputs/audioOutput_ao.c b/src/audioOutputs/audioOutput_ao.c index e94a6e6b7..bf4b99a34 100644 --- a/src/audioOutputs/audioOutput_ao.c +++ b/src/audioOutputs/audioOutput_ao.c @@ -40,7 +40,7 @@ typedef struct _AoData { static AoData *newAoData(void) { - AoData *ret = malloc(sizeof(AoData)); + AoData *ret = xmalloc(sizeof(AoData)); ret->device = NULL; ret->options = NULL; @@ -112,9 +112,9 @@ static int audioOutputAo_initDriver(AudioOutput * audioOutput, blockParam = getBlockParam(param, "options"); if (blockParam) { - dup = strdup(blockParam->value); + dup = xstrdup(blockParam->value); } else - dup = strdup(""); + dup = xstrdup(""); if (strlen(dup)) { stk1 = NULL; diff --git a/src/audioOutputs/audioOutput_mvp.c b/src/audioOutputs/audioOutput_mvp.c index b82b0a5b7..65116681a 100644 --- a/src/audioOutputs/audioOutput_mvp.c +++ b/src/audioOutputs/audioOutput_mvp.c @@ -109,7 +109,7 @@ static int mvp_testDefault(void) static int mvp_initDriver(AudioOutput * audioOutput, ConfigParam * param) { - MvpData *md = malloc(sizeof(MvpData)); + MvpData *md = xmalloc(sizeof(MvpData)); md->fd = -1; audioOutput->data = md; diff --git a/src/audioOutputs/audioOutput_oss.c b/src/audioOutputs/audioOutput_oss.c index bd66bb050..ddb1b1788 100644 --- a/src/audioOutputs/audioOutput_oss.c +++ b/src/audioOutputs/audioOutput_oss.c @@ -164,7 +164,7 @@ static void addSupportedParam(OssData * od, int param, int val) int index = getIndexForParam(param); od->numSupported[index]++; - od->supported[index] = realloc(od->supported[index], + od->supported[index] = xrealloc(od->supported[index], od->numSupported[index] * sizeof(int)); od->supported[index][od->numSupported[index] - 1] = val; } @@ -174,7 +174,7 @@ static void addUnsupportedParam(OssData * od, int param, int val) int index = getIndexForParam(param); od->numUnsupported[index]++; - od->unsupported[index] = realloc(od->unsupported[index], + od->unsupported[index] = xrealloc(od->unsupported[index], od->numUnsupported[index] * sizeof(int)); od->unsupported[index][od->numUnsupported[index] - 1] = val; @@ -193,7 +193,7 @@ static void removeSupportedParam(OssData * od, int param, int val) } od->numSupported[index]--; - od->supported[index] = realloc(od->supported[index], + od->supported[index] = xrealloc(od->supported[index], od->numSupported[index] * sizeof(int)); } @@ -210,7 +210,7 @@ static void removeUnsupportedParam(OssData * od, int param, int val) } od->numUnsupported[index]--; - od->unsupported[index] = realloc(od->unsupported[index], + od->unsupported[index] = xrealloc(od->unsupported[index], od->numUnsupported[index] * sizeof(int)); } @@ -254,7 +254,7 @@ static void unsupportParam(OssData * od, int param, int val) static OssData *newOssData(void) { - OssData *ret = malloc(sizeof(OssData)); + OssData *ret = xmalloc(sizeof(OssData)); ret->device = NULL; ret->fd = -1; diff --git a/src/audioOutputs/audioOutput_osx.c b/src/audioOutputs/audioOutput_osx.c index d386bcbfc..439aefab3 100644 --- a/src/audioOutputs/audioOutput_osx.c +++ b/src/audioOutputs/audioOutput_osx.c @@ -40,7 +40,7 @@ typedef struct _OsxData { static OsxData *newOsxData() { - OsxData *ret = malloc(sizeof(OsxData)); + OsxData *ret = xmalloc(sizeof(OsxData)); pthread_mutex_init(&ret->mutex, NULL); pthread_cond_init(&ret->condition, NULL); @@ -284,7 +284,7 @@ static int osx_openDevice(AudioOutput * audioOutput) /* create a buffer of 1s */ od->bufferSize = (audioFormat->sampleRate) * (audioFormat->bits >> 3) * (audioFormat->channels); - od->buffer = realloc(od->buffer, od->bufferSize); + od->buffer = xrealloc(od->buffer, od->bufferSize); od->pos = 0; od->len = 0; diff --git a/src/audioOutputs/audioOutput_pulse.c b/src/audioOutputs/audioOutput_pulse.c index d7c544cfe..3dade76b6 100644 --- a/src/audioOutputs/audioOutput_pulse.c +++ b/src/audioOutputs/audioOutput_pulse.c @@ -46,7 +46,7 @@ static PulseData *newPulseData(void) { PulseData *ret; - ret = malloc(sizeof(PulseData)); + ret = xmalloc(sizeof(PulseData)); ret->s = NULL; ret->server = NULL; @@ -78,8 +78,8 @@ static int pulse_initDriver(AudioOutput * audioOutput, ConfigParam * param) } pd = newPulseData(); - pd->server = server ? strdup(server->value) : NULL; - pd->sink = sink ? strdup(sink->value) : NULL; + pd->server = server ? xstrdup(server->value) : NULL; + pd->sink = sink ? xstrdup(sink->value) : NULL; audioOutput->data = pd; return 0; diff --git a/src/audioOutputs/audioOutput_shout.c b/src/audioOutputs/audioOutput_shout.c index 5c268930a..2cf2ba624 100644 --- a/src/audioOutputs/audioOutput_shout.c +++ b/src/audioOutputs/audioOutput_shout.c @@ -74,7 +74,7 @@ typedef struct _ShoutData { static ShoutData *newShoutData(void) { - ShoutData *ret = malloc(sizeof(ShoutData)); + ShoutData *ret = xmalloc(sizeof(ShoutData)); ret->shoutConn = shout_new(); ret->opened = 0; diff --git a/src/buffer2array.c b/src/buffer2array.c index 38ced19dc..5784ac931 100644 --- a/src/buffer2array.c +++ b/src/buffer2array.c @@ -85,37 +85,37 @@ int main() char *b; int i, max; - b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\""); + b = xstrdup("lsinfo \"/some/dir/name \\\"test\\\"\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir/name \"test\"", a[1]) ); assert( !a[2] ); - b = strdup("lsinfo \"/some/dir/name \\\"test\\\" something else\""); + b = xstrdup("lsinfo \"/some/dir/name \\\"test\\\" something else\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir/name \"test\" something else", a[1]) ); assert( !a[2] ); - b = strdup("lsinfo \"/some/dir\\\\name\""); + b = xstrdup("lsinfo \"/some/dir\\\\name\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir\\name", a[1]) ); assert( !a[2] ); - b = strdup("lsinfo \"/some/dir name\""); + b = xstrdup("lsinfo \"/some/dir name\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("/some/dir name", a[1]) ); assert( !a[2] ); - b = strdup("lsinfo \"\\\"/some/dir\\\"\""); + b = xstrdup("lsinfo \"\\\"/some/dir\\\"\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("\"/some/dir\"", a[1]) ); assert( !a[2] ); - b = strdup("lsinfo \"\\\"/some/dir\\\" x\""); + b = xstrdup("lsinfo \"\\\"/some/dir\\\" x\""); max = buffer2array(b, a, 4); assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("\"/some/dir\" x", a[1]) ); diff --git a/src/charConv.c b/src/charConv.c index 62fc5f68e..8e614f3a0 100644 --- a/src/charConv.c +++ b/src/charConv.c @@ -19,6 +19,7 @@ #include "charConv.h" #include "mpd_types.h" #include "utf8.h" +#include "utils.h" #include #include @@ -64,8 +65,8 @@ int setCharSetConversion(char *to, char *from) if (0 == strcmp(to, from)) { char_conv_same = 1; - char_conv_to = strdup(to); - char_conv_from = strdup(from); + char_conv_to = xstrdup(to); + char_conv_from = xstrdup(from); return 0; } @@ -76,16 +77,16 @@ int setCharSetConversion(char *to, char *from) } if (char_conv_latin1ToUtf8 != 0) { - char_conv_to = strdup(to); - char_conv_from = strdup(from); + char_conv_to = xstrdup(to); + char_conv_from = xstrdup(from); return 0; } #ifdef HAVE_ICONV if ((char_conv_iconv = iconv_open(to, from)) == (iconv_t) (-1)) return -1; - char_conv_to = strdup(to); - char_conv_from = strdup(from); + char_conv_to = xstrdup(to); + char_conv_from = xstrdup(from); char_conv_use_iconv = 1; return 0; @@ -100,7 +101,7 @@ char *convStrDup(char *string) return NULL; if (char_conv_same) - return strdup(string); + return xstrdup(string); #ifdef HAVE_ICONV if (char_conv_use_iconv) { @@ -112,7 +113,7 @@ char *convStrDup(char *string) size_t err; char *bufferPtr; - ret = malloc(1); + ret = xmalloc(1); ret[0] = '\0'; while (inleft) { @@ -127,7 +128,7 @@ char *convStrDup(char *string) return NULL; } - ret = realloc(ret, retlen + BUFFER_SIZE - outleft + 1); + ret = xrealloc(ret, retlen + BUFFER_SIZE - outleft + 1); memcpy(ret + retlen, buffer, BUFFER_SIZE - outleft); retlen += BUFFER_SIZE - outleft; ret[retlen] = '\0'; diff --git a/src/command.c b/src/command.c index 855b28b08..f1b98a9f3 100644 --- a/src/command.c +++ b/src/command.c @@ -30,6 +30,7 @@ #include "log.h" #include "dbUtils.h" #include "tag.h" +#include "utils.h" #include #include @@ -140,7 +141,7 @@ static List *commandList; static CommandEntry *newCommandEntry(void) { - CommandEntry *cmd = malloc(sizeof(CommandEntry)); + CommandEntry *cmd = xmalloc(sizeof(CommandEntry)); cmd->cmd = NULL; cmd->min = 0; cmd->max = 0; diff --git a/src/compress.c b/src/compress.c index a29d4ef78..e05265ed7 100644 --- a/src/compress.c +++ b/src/compress.c @@ -26,6 +26,7 @@ #include #include "compress.h" +#include "utils.h" #ifdef USE_X #include @@ -67,7 +68,7 @@ void CompressCfg(int show_mon, int anticlip, int target, int gainmax, prefs.buckets = buckets; /* Allocate the peak structure */ - peaks = realloc(peaks, sizeof(int)*prefs.buckets); + peaks = xrealloc(peaks, sizeof(int)*prefs.buckets); if (prefs.buckets > lastsize) memset(peaks + lastsize, 0, sizeof(int)*(prefs.buckets diff --git a/src/conf.c b/src/conf.c index c12a6f292..b5bbd8e92 100644 --- a/src/conf.c +++ b/src/conf.c @@ -52,12 +52,12 @@ static List *configEntriesList = NULL; static ConfigParam *newConfigParam(char *value, int line) { - ConfigParam *ret = malloc(sizeof(ConfigParam)); + ConfigParam *ret = xmalloc(sizeof(ConfigParam)); if (!value) ret->value = NULL; else - ret->value = strdup(value); + ret->value = xstrdup(value); ret->line = line; @@ -91,7 +91,7 @@ static void freeConfigParam(ConfigParam * param) static ConfigEntry *newConfigEntry(int repeatable, int block) { - ConfigEntry *ret = malloc(sizeof(ConfigEntry)); + ConfigEntry *ret = xmalloc(sizeof(ConfigEntry)); ret->mask = 0; ret->configParamList = @@ -180,13 +180,13 @@ static void addBlockParam(ConfigParam * param, char *name, char *value, { param->numberOfBlockParams++; - param->blockParams = realloc(param->blockParams, + param->blockParams = xrealloc(param->blockParams, param->numberOfBlockParams * sizeof(BlockParam)); - param->blockParams[param->numberOfBlockParams - 1].name = strdup(name); + param->blockParams[param->numberOfBlockParams - 1].name = xstrdup(name); param->blockParams[param->numberOfBlockParams - 1].value = - strdup(value); + xstrdup(value); param->blockParams[param->numberOfBlockParams - 1].line = line; } @@ -452,7 +452,7 @@ ConfigParam *parseConfigFilePath(char *name, int force) if (foundSlash) *ch = '/'; } - newPath = malloc(strlen(pwd->pw_dir) + strlen(path + pos) + 1); + newPath = xmalloc(strlen(pwd->pw_dir) + strlen(path + pos) + 1); strcpy(newPath, pwd->pw_dir); strcat(newPath, path + pos); free(param->value); diff --git a/src/dbUtils.c b/src/dbUtils.c index 212ea83d2..c8b637b88 100644 --- a/src/dbUtils.c +++ b/src/dbUtils.c @@ -69,14 +69,14 @@ static int initLocateTagItem(LocateTagItem * item, char *typeStr, char *needle) if (item->tagType < 0) return -1; - item->needle = strdup(needle); + item->needle = xstrdup(needle); return 0; } LocateTagItem *newLocateTagItem(char *typeStr, char *needle) { - LocateTagItem *ret = malloc(sizeof(LocateTagItem)); + LocateTagItem *ret = xmalloc(sizeof(LocateTagItem)); if (initLocateTagItem(ret, typeStr, needle) < 0) { free(ret); @@ -108,7 +108,7 @@ int newLocateTagItemArrayFromArgArray(char *argArray[], if (numArgs % 2 != 0) return -1; - *arrayRet = malloc(sizeof(LocateTagItem) * numArgs / 2); + *arrayRet = xmalloc(sizeof(LocateTagItem) * numArgs / 2); for (i = 0, item = *arrayRet; i < numArgs / 2; i++, item++) { if (initLocateTagItem @@ -214,7 +214,7 @@ int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items) int ret = -1; int i; - char **originalNeedles = malloc(numItems * sizeof(char *)); + char **originalNeedles = xmalloc(numItems * sizeof(char *)); LocateTagItemArray array; for (i = 0; i < numItems; i++) { @@ -347,7 +347,7 @@ unsigned long sumSongTimesIn(int fd, char *name) static ListCommandItem *newListCommandItem(int tagType, int numConditionals, LocateTagItem * conditionals) { - ListCommandItem *item = malloc(sizeof(ListCommandItem)); + ListCommandItem *item = xmalloc(sizeof(ListCommandItem)); item->tagType = tagType; item->numConditionals = numConditionals; diff --git a/src/decode.c b/src/decode.c index 1cfa03e5a..b25cff105 100644 --- a/src/decode.c +++ b/src/decode.c @@ -264,7 +264,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb, if (isRemoteUrl(pc->utf8url)) path = utf8StrToLatin1Dup(pc->utf8url); else - path = strdup(rmp2amp(utf8ToFsCharset(pc->utf8url))); + path = xstrdup(rmp2amp(utf8ToFsCharset(pc->utf8url))); if (!path) { dc->error = DECODE_ERROR_FILE; @@ -304,7 +304,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb, /*if(inStream.metaName) { MpdTag * tag = newMpdTag(); - tag->name = strdup(inStream.metaName); + tag->name = xstrdup(inStream.metaName); copyMpdTagToOutputBuffer(cb, tag); freeMpdTag(tag); } */ diff --git a/src/directory.c b/src/directory.c index 0457f8bbc..03ae16699 100644 --- a/src/directory.c +++ b/src/directory.c @@ -236,7 +236,7 @@ int updateInit(int fd, List * pathList) static DirectoryStat *newDirectoryStat(struct stat *st) { - DirectoryStat *ret = malloc(sizeof(DirectoryStat)); + DirectoryStat *ret = xmalloc(sizeof(DirectoryStat)); ret->inode = st->st_ino; ret->device = st->st_dev; return ret; @@ -258,10 +258,10 @@ static Directory *newDirectory(char *dirname, Directory * parent) { Directory *directory; - directory = malloc(sizeof(Directory)); + directory = xmalloc(sizeof(Directory)); if (dirname && strlen(dirname)) - directory->path = strdup(dirname); + directory->path = xstrdup(dirname); else directory->path = NULL; directory->subDirectories = newDirectoryList(); @@ -392,11 +392,11 @@ static int removeDeletedFromDirectory(Directory * directory, DIR * dir) continue; if (directory->path) { - s = malloc(strlen(getDirectoryPath(directory)) + s = xmalloc(strlen(getDirectoryPath(directory)) + strlen(utf8) + 2); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); } else - s = strdup(utf8); + s = xstrdup(utf8); insertInList(entList, utf8, s); } @@ -445,7 +445,7 @@ static Directory *addDirectoryPathToDB(char *utf8path, char **shortname) Directory *parentDirectory; void *directory; - parent = strdup(parentPath(utf8path)); + parent = xstrdup(parentPath(utf8path)); if (strlen(parent) == 0) parentDirectory = (void *)mp3rootDirectory; @@ -489,7 +489,7 @@ static Directory *addParentPathToDB(char *utf8path, char **shortname) char *parent; Directory *parentDirectory; - parent = strdup(parentPath(utf8path)); + parent = xstrdup(parentPath(utf8path)); if (strlen(parent) == 0) parentDirectory = (void *)mp3rootDirectory; @@ -653,14 +653,14 @@ static int updateDirectory(Directory * directory) if (!utf8) continue; - utf8 = strdup(utf8); + utf8 = xstrdup(utf8); if (directory->path) { - s = malloc(strlen(getDirectoryPath(directory)) + + s = xmalloc(strlen(getDirectoryPath(directory)) + strlen(utf8) + 2); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); } else - s = strdup(utf8); + s = xstrdup(utf8); if (updateInDirectory(directory, utf8, s) > 0) ret = 1; free(utf8); @@ -708,16 +708,16 @@ static int exploreDirectory(Directory * directory) if (!utf8) continue; - utf8 = strdup(utf8); + utf8 = xstrdup(utf8); DEBUG("explore: found: %s (%s)\n", ent->d_name, utf8); if (directory->path) { - s = malloc(strlen(getDirectoryPath(directory)) + + s = xmalloc(strlen(getDirectoryPath(directory)) + strlen(utf8) + 2); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); } else - s = strdup(utf8); + s = xstrdup(utf8); if (addToDirectory(directory, utf8, s) > 0) ret = 1; free(utf8); @@ -817,7 +817,7 @@ void closeMp3Directory(void) static Directory *findSubDirectory(Directory * directory, char *name) { void *subDirectory; - char *dup = strdup(name); + char *dup = xstrdup(name); char *key; key = strtok(dup, "/"); @@ -942,7 +942,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) while (myFgets(buffer, bufferSize, fp) && 0 != strncmp(DIRECTORY_END, buffer, strlen(DIRECTORY_END))) { if (0 == strncmp(DIRECTORY_DIR, buffer, strlen(DIRECTORY_DIR))) { - key = strdup(&(buffer[strlen(DIRECTORY_DIR)])); + key = xstrdup(&(buffer[strlen(DIRECTORY_DIR)])); if (!myFgets(buffer, bufferSize, fp)) { ERROR("Error reading db, fgets\n"); exit(EXIT_FAILURE); @@ -961,7 +961,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) ERROR("Error reading db at line: %s\n", buffer); exit(EXIT_FAILURE); } - name = strdup(&(buffer[strlen(DIRECTORY_BEGIN)])); + name = xstrdup(&(buffer[strlen(DIRECTORY_BEGIN)])); while (nextDirNode && (strcmpRet = strcmp(key, @@ -1034,7 +1034,7 @@ int checkDirectoryDB(void) /* If the file doesn't exist, we can't check if we can write * it, so we are going to try to get the directory path, and * see if we can write a file in that */ - dbPath = strdup(dbFile); + dbPath = xstrdup(dbFile); dirPath = dirname(dbPath); /* Check that the parent part of the path is a directory */ @@ -1329,7 +1329,7 @@ static Song *getSongDetails(char *file, char **shortnameRet, void *song = NULL; Directory *directory; char *dir = NULL; - char *dup = strdup(file); + char *dup = xstrdup(file); char *shortname = dup; char *c = strtok(dup, "/"); diff --git a/src/gcc.h b/src/gcc.h index 4b6335e2c..bd55f732e 100644 --- a/src/gcc.h +++ b/src/gcc.h @@ -25,7 +25,6 @@ * example taken from: http://rlove.org/log/2005102601 */ -/* disabled (0) until I fix all the warnings :) */ #if __GNUC__ >= 3 # define mpd_const __attribute__ ((const)) # define mpd_deprecated __attribute__ ((deprecated)) diff --git a/src/inputPlugins/aac_plugin.c b/src/inputPlugins/aac_plugin.c index 3213bb68f..07906152c 100644 --- a/src/inputPlugins/aac_plugin.c +++ b/src/inputPlugins/aac_plugin.c @@ -162,7 +162,7 @@ static void initAacBuffer(InputStream * inStream, AacBuffer * b, float *length, fileread = inStream->size; - b->buffer = malloc(FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS); + b->buffer = xmalloc(FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS); memset(b->buffer, 0, FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS); bread = readFromInputStream(inStream, b->buffer, 1, diff --git a/src/inputPlugins/mod_plugin.c b/src/inputPlugins/mod_plugin.c index 31d7b20de..a73c267be 100644 --- a/src/inputPlugins/mod_plugin.c +++ b/src/inputPlugins/mod_plugin.c @@ -143,9 +143,9 @@ static mod_Data *mod_open(char *path) if (!(moduleHandle = Player_Load(path, 128, 0))) return NULL; - data = malloc(sizeof(mod_Data)); + data = xmalloc(sizeof(mod_Data)); - data->audio_buffer = malloc(MIKMOD_FRAME_SIZE); + data->audio_buffer = xmalloc(MIKMOD_FRAME_SIZE); data->moduleHandle = moduleHandle; Player_Start(data->moduleHandle); @@ -243,7 +243,7 @@ static MpdTag *modTagDup(char *file) ret = newMpdTag(); ret->time = 0; - title = strdup(Player_LoadTitle(file)); + title = xstrdup(Player_LoadTitle(file)); if (title) addItemToMpdTag(ret, TAG_ITEM_TITLE, title); diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c index 098b64da3..f0744f22c 100644 --- a/src/inputPlugins/mp3_plugin.c +++ b/src/inputPlugins/mp3_plugin.c @@ -293,7 +293,7 @@ static void mp3_parseId3Tag(mp3DecodeData * data, signed long tagsize, id3_data = data->stream.this_frame; mad_stream_skip(&(data->stream), tagsize); } else { - allocated = malloc(tagsize); + allocated = xmalloc(tagsize); if (!allocated) goto fail; @@ -682,8 +682,8 @@ static int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc, } } - data->frameOffset = malloc(sizeof(long) * data->maxFrames); - data->times = malloc(sizeof(mad_timer_t) * data->maxFrames); + data->frameOffset = xmalloc(sizeof(long) * data->maxFrames); + data->times = xmalloc(sizeof(mad_timer_t) * data->maxFrames); return 0; } diff --git a/src/inputPlugins/mp4_plugin.c b/src/inputPlugins/mp4_plugin.c index 0cfa7f959..6c659cbb6 100644 --- a/src/inputPlugins/mp4_plugin.c +++ b/src/inputPlugins/mp4_plugin.c @@ -123,7 +123,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path) return -1; } - mp4cb = malloc(sizeof(mp4ff_callback_t)); + mp4cb = xmalloc(sizeof(mp4ff_callback_t)); mp4cb->read = mp4_inputStreamReadCallback; mp4cb->seek = mp4_inputStreamSeekCallback; mp4cb->user_data = &inStream; @@ -195,7 +195,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path) time = 0.0; - seekTable = malloc(sizeof(float) * numSamples); + seekTable = xmalloc(sizeof(float) * numSamples); for (sampleId = 0; sampleId < numSamples && !eof; sampleId++) { if (dc->seek) @@ -341,7 +341,7 @@ static MpdTag *mp4DataDup(char *file, int *mp4MetadataFound) return NULL; } - cb = malloc(sizeof(mp4ff_callback_t)); + cb = xmalloc(sizeof(mp4ff_callback_t)); cb->read = mp4_inputStreamReadCallback; cb->seek = mp4_inputStreamSeekCallback; cb->user_data = &inStream; diff --git a/src/inputStream_http.c b/src/inputStream_http.c index 969d2a0ea..f56417ca0 100644 --- a/src/inputStream_http.c +++ b/src/inputStream_http.c @@ -172,7 +172,7 @@ static char *base64Dup(char *s) { int i; int len = strlen(s); - char *ret = calloc(BASE64_LENGTH(len) + 1, 1); + char *ret = xcalloc(BASE64_LENGTH(len) + 1, 1); unsigned char *p = (unsigned char *)ret; char tbl[64] = { @@ -216,14 +216,14 @@ static char *authString(char *header, char *user, char *password) return NULL; templen = strlen(user) + strlen(password) + 2; - temp = malloc(templen); + temp = xmalloc(templen); strcpy(temp, user); strcat(temp, ":"); strcat(temp, password); temp64 = base64Dup(temp); free(temp); - ret = malloc(strlen(temp64) + strlen(header) + 3); + ret = xmalloc(strlen(temp64) + strlen(header) + 3); strcpy(ret, header); strcat(ret, temp64); strcat(ret, "\r\n"); @@ -240,7 +240,7 @@ static char *authString(char *header, char *user, char *password) static InputStreamHTTPData *newInputStreamHTTPData(void) { - InputStreamHTTPData *ret = malloc(sizeof(InputStreamHTTPData)); + InputStreamHTTPData *ret = xmalloc(sizeof(InputStreamHTTPData)); if (proxyHost) { ret->proxyAuth = proxyAuthString(proxyUser, proxyPassword); @@ -256,7 +256,7 @@ static InputStreamHTTPData *newInputStreamHTTPData(void) ret->icyMetaint = 0; ret->prebuffer = 0; ret->icyOffset = 0; - ret->buffer = malloc(bufferSize); + ret->buffer = xmalloc(bufferSize); return ret; } @@ -305,19 +305,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url) char *passwd; if (colon && colon < at) { - user = malloc(colon - temp + 1); + user = xmalloc(colon - temp + 1); memcpy(user, temp, colon - temp); user[colon - temp] = '\0'; - passwd = malloc(at - colon); + passwd = xmalloc(at - colon); memcpy(passwd, colon + 1, at - colon - 1); passwd[at - colon - 1] = '\0'; } else { - user = malloc(at - temp + 1); + user = xmalloc(at - temp + 1); memcpy(user, temp, at - temp); user[at - temp] = '\0'; - passwd = strdup(""); + passwd = xstrdup(""); } data->httpAuth = httpAuthString(user, passwd); @@ -345,7 +345,7 @@ static int parseUrl(InputStreamHTTPData * data, char *url) if (len <= 1) return -1; - data->host = malloc(len); + data->host = xmalloc(len); memcpy(data->host, temp, len - 1); data->host[len - 1] = '\0'; /* fetch the port */ @@ -353,19 +353,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url) len = strlen(colon) - 1; if (slash) len -= strlen(slash); - data->port = malloc(len + 1); + data->port = xmalloc(len + 1); memcpy(data->port, colon + 1, len); data->port[len] = '\0'; DEBUG(__FILE__ ": Port: %s\n", data->port); } else { - data->port = strdup("80"); + data->port = xstrdup("80"); } /* fetch the path */ if (proxyHost) - data->path = strdup(url); + data->path = xstrdup(url); else - data->path = strdup(slash ? slash : "/"); + data->path = xstrdup(slash ? slash : "/"); return 0; } @@ -591,7 +591,7 @@ static int getHTTPHello(InputStream * inStream) && *(cur + curlen) != '\r') { curlen++; } - url = malloc(curlen + 1); + url = xmalloc(curlen + 1); memcpy(url, cur, curlen); url[curlen] = '\0'; ret = parseUrl(data, url); @@ -634,7 +634,7 @@ static int getHTTPHello(InputStream * inStream) free(inStream->metaName); while (*(incr + cur) == ' ') incr++; - inStream->metaName = strdup(cur + incr); + inStream->metaName = xstrdup(cur + incr); *temp = '\r'; DEBUG("inputStream_http: metaName: %s\n", inStream->metaName); @@ -648,7 +648,7 @@ static int getHTTPHello(InputStream * inStream) free(inStream->metaName); while (*(incr + cur) == ' ') incr++; - inStream->metaName = strdup(cur + incr); + inStream->metaName = xstrdup(cur + incr); *temp = '\r'; DEBUG("inputStream_http: metaName: %s\n", inStream->metaName); @@ -662,7 +662,7 @@ static int getHTTPHello(InputStream * inStream) free(inStream->mime); while (*(incr + cur) == ' ') incr++; - inStream->mime = strdup(cur + incr); + inStream->mime = xstrdup(cur + incr); *temp = '\r'; } @@ -735,7 +735,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size) { char *r; char *s; - char *temp = malloc(size + 1); + char *temp = xmalloc(size + 1); memcpy(temp, metadata, size); temp[size] = '\0'; s = strtok_r(temp, ";", &r); @@ -749,7 +749,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size) if (s[strlen(s) - 1] == '\'') { s[strlen(s) - 1] = '\0'; } - inStream->metaTitle = strdup(s + cur); + inStream->metaTitle = xstrdup(s + cur); DEBUG("inputStream_http: metaTitle: %s\n", inStream->metaTitle); } diff --git a/src/interface.c b/src/interface.c index 3f803d057..4220935ed 100644 --- a/src/interface.c +++ b/src/interface.c @@ -132,7 +132,7 @@ static void set_send_buf_size(Interface * interface) if (interface->send_buf_alloc < new_size) { if (interface->send_buf) free(interface->send_buf); - interface->send_buf = malloc(new_size); + interface->send_buf = xmalloc(new_size); interface->send_buf_alloc = new_size; } } @@ -593,9 +593,9 @@ void initInterfaces(void) interface_max_output_buffer_size *= 1024; } - interfaces = malloc(sizeof(Interface) * interface_max_connections); + interfaces = xmalloc(sizeof(Interface) * interface_max_connections); - list_cache = calloc(interface_list_cache_size, sizeof(struct strnode)); + list_cache = xcalloc(interface_list_cache_size, sizeof(struct strnode)); list_cache_head = &(list_cache[0]); list_cache_tail = &(list_cache[interface_list_cache_size - 1]); diff --git a/src/list.c b/src/list.c index 1bec3deab..c47398200 100644 --- a/src/list.c +++ b/src/list.c @@ -17,6 +17,7 @@ */ #include "list.h" +#include "utils.h" #include #include @@ -32,7 +33,7 @@ static void makeListNodesArray(List * list) if (!list->numberOfNodes) return; - list->nodesArray = realloc(list->nodesArray, + list->nodesArray = xrealloc(list->nodesArray, sizeof(ListNode *) * list->numberOfNodes); for (i = 0; i < list->numberOfNodes; i++) { @@ -51,7 +52,7 @@ static void freeListNodesArray(List * list) List *makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys) { - List *list = malloc(sizeof(List)); + List *list = xmalloc(sizeof(List)); assert(list != NULL); @@ -75,7 +76,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, assert(key != NULL); /*assert(data!=NULL); */ - node = malloc(sizeof(ListNode)); + node = xmalloc(sizeof(ListNode)); assert(node != NULL); node->nextNode = beforeNode; @@ -102,7 +103,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, } if (list->strdupKeys) - node->key = strdup(key); + node->key = xstrdup(key); else node->key = key; @@ -111,7 +112,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, list->numberOfNodes++; if (list->sorted) { - list->nodesArray = realloc(list->nodesArray, + list->nodesArray = xrealloc(list->nodesArray, list->numberOfNodes * sizeof(ListNode *)); if (node == list->lastNode) { @@ -138,7 +139,7 @@ ListNode *insertInList(List * list, char *key, void *data) assert(key != NULL); /*assert(data!=NULL); */ - node = malloc(sizeof(ListNode)); + node = xmalloc(sizeof(ListNode)); assert(node != NULL); if (list->nodesArray) @@ -154,7 +155,7 @@ ListNode *insertInList(List * list, char *key, void *data) } if (list->strdupKeys) - node->key = strdup(key); + node->key = xstrdup(key); else node->key = key; @@ -176,7 +177,7 @@ int insertInListWithoutKey(List * list, void *data) assert(list != NULL); assert(data != NULL); - node = malloc(sizeof(ListNode)); + node = xmalloc(sizeof(ListNode)); assert(node != NULL); if (list->nodesArray) @@ -416,8 +417,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) List *startList = makeList(free, 0); List *endList = makeList(free, 0); - long *startPtr = malloc(sizeof(long)); - long *endPtr = malloc(sizeof(long)); + long *startPtr = xmalloc(sizeof(long)); + long *endPtr = xmalloc(sizeof(long)); *startPtr = start; *endPtr = end; insertInListWithoutKey(startList, (void *)startPtr); @@ -471,8 +472,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) deleteNodeFromList(endList, endList->lastNode); if (pivot - 1 - start > 0) { - startPtr = malloc(sizeof(long)); - endPtr = malloc(sizeof(long)); + startPtr = xmalloc(sizeof(long)); + endPtr = xmalloc(sizeof(long)); *startPtr = start; *endPtr = pivot - 1; insertInListWithoutKey(startList, @@ -483,8 +484,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) } if (end - pivot - 1 > 0) { - startPtr = malloc(sizeof(long)); - endPtr = malloc(sizeof(long)); + startPtr = xmalloc(sizeof(long)); + endPtr = xmalloc(sizeof(long)); *startPtr = pivot + 1; *endPtr = end; insertInListWithoutKey(startList, diff --git a/src/listen.c b/src/listen.c index 6015bbc4f..7cc16a2d3 100644 --- a/src/listen.c +++ b/src/listen.c @@ -101,7 +101,7 @@ static int establishListen(unsigned int port, numberOfListenSockets++; listenSockets = - realloc(listenSockets, sizeof(int) * numberOfListenSockets); + xrealloc(listenSockets, sizeof(int) * numberOfListenSockets); listenSockets[numberOfListenSockets - 1] = sock; diff --git a/src/log.h b/src/log.h index ee9942133..bbe02aecc 100644 --- a/src/log.h +++ b/src/log.h @@ -20,8 +20,7 @@ #define LOG_H #include "../config.h" - -#include "myfprintf.h" +#include "gcc.h" #include diff --git a/src/ls.c b/src/ls.c index 9a4c07eca..6c9e9ba48 100644 --- a/src/ls.c +++ b/src/ls.c @@ -22,6 +22,7 @@ #include "myfprintf.h" #include "log.h" #include "utf8.h" +#include "utils.h" #include #include @@ -154,7 +155,7 @@ int lsPlaylists(int fd, char *utf8path) int i; sortList(list); - dup = malloc(strlen(utf8path) + 2); + dup = xmalloc(strlen(utf8path) + 2); strcpy(dup, utf8path); for (i = strlen(dup) - 1; i >= 0 && dup[i] == '/'; i--) { dup[i] = '\0'; diff --git a/src/main.c b/src/main.c index 6c175fbd7..04f8a6ed5 100644 --- a/src/main.c +++ b/src/main.c @@ -90,7 +90,7 @@ static int setenv(const char *name, const char *value, int replace) namelen = strlen(name); valuelen = strlen(value); - envstr = malloc((namelen + valuelen + 2)); + envstr = xmalloc((namelen + valuelen + 2)); if (!envstr) return -1; diff --git a/src/outputBuffer.c b/src/outputBuffer.c index fe868d54e..2b06ae529 100644 --- a/src/outputBuffer.c +++ b/src/outputBuffer.c @@ -91,7 +91,7 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream, &(cb-> audioFormat)); if (datalen > convBufferLen) { - convBuffer = realloc(convBuffer, datalen); + convBuffer = xrealloc(convBuffer, datalen); convBufferLen = datalen; } data = convBuffer; 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); } diff --git a/src/path.h b/src/path.h index 63267b730..5a17ab11e 100644 --- a/src/path.h +++ b/src/path.h @@ -34,7 +34,7 @@ void finishPaths(void); * which means: * - Do not manually free the return value of these functions, it'll be * automatically freed the next time it is called. - * - They are not reentrant, strdup the return value immediately if + * - They are not reentrant, xstrdup the return value immediately if * you expect to call one of these functions again, but still need the * previous result. * - The static pointer is unique to each function. diff --git a/src/pcm_utils.c b/src/pcm_utils.c index 0210c290f..c041cbfb8 100644 --- a/src/pcm_utils.c +++ b/src/pcm_utils.c @@ -20,6 +20,7 @@ #include "mpd_types.h" #include "log.h" +#include "utils.h" #include #include @@ -153,7 +154,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t case 8: dataBitLen = inSize << 1; if (dataBitLen > bitConvBufferLength) { - bitConvBuffer = realloc(bitConvBuffer, dataBitLen); + bitConvBuffer = xrealloc(bitConvBuffer, dataBitLen); bitConvBufferLength = dataBitLen; } dataBitConv = bitConvBuffer; @@ -187,7 +188,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t case 1: dataChannelLen = (dataBitLen >> 1) << 2; if (dataChannelLen > channelConvBufferLength) { - channelConvBuffer = realloc(channelConvBuffer, + channelConvBuffer = xrealloc(channelConvBuffer, dataChannelLen); channelConvBufferLength = dataChannelLen; } @@ -207,7 +208,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t case 2: dataChannelLen = dataBitLen >> 1; if (dataChannelLen > channelConvBufferLength) { - channelConvBuffer = realloc(channelConvBuffer, + channelConvBuffer = xrealloc(channelConvBuffer, dataChannelLen); channelConvBufferLength = dataChannelLen; } diff --git a/src/permission.c b/src/permission.c index 15a336a08..dcd8a4791 100644 --- a/src/permission.c +++ b/src/permission.c @@ -21,6 +21,7 @@ #include "conf.h" #include "list.h" #include "log.h" +#include "utils.h" #include @@ -103,7 +104,7 @@ void initPermissions(void) password = temp; - permission = malloc(sizeof(int)); + permission = xmalloc(sizeof(int)); *permission = parsePermissions(strtok_r(NULL, "", &cp2)); diff --git a/src/player.c b/src/player.c index 4f8066eed..6e2e11afe 100644 --- a/src/player.c +++ b/src/player.c @@ -301,7 +301,7 @@ char *getPlayerErrorStr(void) int errorlen = MAXPATHLEN + 1024; PlayerControl *pc = &(getPlayerData()->playerControl); - error = realloc(error, errorlen + 1); + error = xrealloc(error, errorlen + 1); memset(error, 0, errorlen + 1); switch (pc->error) { @@ -328,7 +328,7 @@ char *getPlayerErrorStr(void) } errorlen = strlen(error); - error = realloc(error, errorlen + 1); + error = xrealloc(error, errorlen + 1); if (errorlen) return error; @@ -513,12 +513,12 @@ Song *playerCurrentDecodeSong(void) DEBUG("playerCurrentDecodeSong: caught new metadata!\n"); if (prev) free(prev); - prev = malloc(sizeof(MetadataChunk)); + prev = xmalloc(sizeof(MetadataChunk)); memcpy(prev, &(pc->metadataChunk), sizeof(MetadataChunk)); if (song) freeJustSong(song); song = newNullSong(); - song->url = strdup(pc->currentUrl); + song->url = xstrdup(pc->currentUrl); song->tag = metadataChunkToMpdTagDup(prev); ret = song; resetPlayerMetadata(); diff --git a/src/playlist.c b/src/playlist.c index 8671e6596..c977d0e4f 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -161,12 +161,12 @@ void initPlaylist(void) if (playlist_saveAbsolutePaths == -1) playlist_saveAbsolutePaths = 0; else if (playlist_saveAbsolutePaths < 0) exit(EXIT_FAILURE); - playlist.songs = malloc(sizeof(Song *) * playlist_max_length); - playlist.songMod = malloc(sizeof(mpd_uint32) * playlist_max_length); - playlist.order = malloc(sizeof(int) * playlist_max_length); - playlist.idToPosition = malloc(sizeof(int) * playlist_max_length * + playlist.songs = xmalloc(sizeof(Song *) * playlist_max_length); + playlist.songMod = xmalloc(sizeof(mpd_uint32) * playlist_max_length); + playlist.order = xmalloc(sizeof(int) * playlist_max_length); + playlist.idToPosition = xmalloc(sizeof(int) * playlist_max_length * PLAYLIST_HASH_MULT); - playlist.positionToId = malloc(sizeof(int) * playlist_max_length); + playlist.positionToId = xmalloc(sizeof(int) * playlist_max_length); memset(playlist.songs, 0, sizeof(char *) * playlist_max_length); @@ -1264,7 +1264,7 @@ int shufflePlaylist(int fd) int deletePlaylist(int fd, char *utf8file) { char *file = utf8ToFsCharset(utf8file); - char *rfile = malloc(strlen(file) + strlen(".") + + char *rfile = xmalloc(strlen(file) + strlen(".") + strlen(PLAYLIST_FILE_SUFFIX) + 1); char *actualFile; @@ -1308,7 +1308,7 @@ int savePlaylist(int fd, char *utf8file) file = utf8ToFsCharset(utf8file); - rfile = malloc(strlen(file) + strlen(".") + + rfile = xmalloc(strlen(file) + strlen(".") + strlen(PLAYLIST_FILE_SUFFIX) + 1); strcpy(rfile, file); @@ -1421,7 +1421,7 @@ static int PlaylistIterFunc(int fd, char *utf8file, char s[MAXPATHLEN + 1]; int slength = 0; char *temp = utf8ToFsCharset(utf8file); - char *rfile = malloc(strlen(temp) + strlen(".") + + char *rfile = xmalloc(strlen(temp) + strlen(".") + strlen(PLAYLIST_FILE_SUFFIX) + 1); char *actualFile; char *parent = parentPath(temp); @@ -1461,7 +1461,7 @@ static int PlaylistIterFunc(int fd, char *utf8file, if (strncmp(s, musicDir, strlen(musicDir)) == 0) { strcpy(s, &(s[strlen(musicDir)])); } else if (parentlen) { - temp = strdup(s); + temp = xstrdup(s); memset(s, 0, MAXPATHLEN + 1); strcpy(s, parent); strncat(s, "/", MAXPATHLEN - parentlen); @@ -1536,7 +1536,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile) } else if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) { /* for windows compatibilit, convert slashes */ - char *temp2 = strdup(temp); + char *temp2 = xstrdup(temp); char *p = temp2; while (*p) { if (*p == '\\') @@ -1545,7 +1545,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile) } if ((addToPlaylist(STDERR_FILENO, temp2, 0)) < 0) { if (!*erroredFile) { - *erroredFile = strdup(temp); + *erroredFile = xstrdup(temp); } } free(temp2); diff --git a/src/replayGain.c b/src/replayGain.c index 52362946e..eb084e755 100644 --- a/src/replayGain.c +++ b/src/replayGain.c @@ -18,6 +18,7 @@ */ #include "replayGain.h" +#include "utils.h" #include "log.h" #include "conf.h" @@ -89,7 +90,7 @@ static float computeReplayGainScale(float gain, float peak) ReplayGainInfo *newReplayGainInfo(void) { - ReplayGainInfo *ret = malloc(sizeof(ReplayGainInfo)); + ReplayGainInfo *ret = xmalloc(sizeof(ReplayGainInfo)); ret->albumGain = 0.0; ret->albumPeak = 0.0; diff --git a/src/sllist.c b/src/sllist.c index e87be6ad5..9d581ef5d 100644 --- a/src/sllist.c +++ b/src/sllist.c @@ -30,14 +30,14 @@ static void init_strnode(struct strnode *x, char *s) struct strnode *new_strnode(char *s) { - struct strnode *x = malloc(sizeof(struct strnode)); + struct strnode *x = xmalloc(sizeof(struct strnode)); init_strnode(x, s); return x; } struct strnode *new_strnode_dup(char *s, const size_t size) { - struct strnode *x = malloc(sizeof(struct strnode) + size); + struct strnode *x = xmalloc(sizeof(struct strnode) + size); x->next = NULL; x->data = ((char *)x + sizeof(struct strnode)); memcpy((void *)x->data, (void*)s, size); @@ -46,7 +46,7 @@ struct strnode *new_strnode_dup(char *s, const size_t size) struct sllnode *new_sllnode(void *s, const size_t size) { - struct sllnode *x = malloc(sizeof(struct sllnode) + size); + struct sllnode *x = xmalloc(sizeof(struct sllnode) + size); x->next = NULL; x->size = size; x->data = ((char *)x + sizeof(struct sllnode)); diff --git a/src/sllist.h b/src/sllist.h index 9089e2f5a..7b6415172 100644 --- a/src/sllist.h +++ b/src/sllist.h @@ -27,7 +27,7 @@ * should _NEVER_ be explicitly freed * * there's no free command, iterate through them yourself and just - * call free() on it iff you malloc'd them */ + * call free() on it iff you xmalloc'd them */ struct strnode { struct strnode *next; diff --git a/src/song.c b/src/song.c index 62e699839..1197a436f 100644 --- a/src/song.c +++ b/src/song.c @@ -25,6 +25,7 @@ #include "path.h" #include "playlist.h" #include "inputPlugin.h" +#include "myfprintf.h" #define SONG_KEY "key: " #define SONG_FILE "file: " @@ -37,7 +38,7 @@ Song *newNullSong(void) { - Song *song = malloc(sizeof(Song)); + Song *song = xmalloc(sizeof(Song)); song->tag = NULL; song->url = NULL; @@ -58,7 +59,7 @@ Song *newSong(char *url, int type, Directory * parentDir) song = newNullSong(); - song->url = strdup(url); + song->url = xstrdup(url); song->type = type; song->parentDir = parentDir; @@ -248,7 +249,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) } song = newNullSong(); - song->url = strdup(buffer + strlen(SONG_KEY)); + song->url = xstrdup(buffer + strlen(SONG_KEY)); song->type = SONG_TYPE_FILE; song->parentDir = parentDir; } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { @@ -257,7 +258,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) exit(EXIT_FAILURE); } /* we don't need this info anymore - song->url = strdup(&(buffer[strlen(SONG_FILE)])); + song->url = xstrdup(&(buffer[strlen(SONG_FILE)])); */ } else if (matchesAnMpdTagItemKey(buffer, &itemType)) { if (!song->tag) @@ -346,7 +347,7 @@ char *getSongUrl(Song * song) size = slen + dlen + 2; if (size > bufferSize) { - buffer = realloc(buffer, size); + buffer = xrealloc(buffer, size); bufferSize = size; } diff --git a/src/tag.c b/src/tag.c index 68fd2dbab..697efbcbe 100644 --- a/src/tag.c +++ b/src/tag.c @@ -93,7 +93,7 @@ void initTagConfig(void) if (0 == strcasecmp(param->value, "none")) return; - temp = c = s = strdup(param->value); + temp = c = s = xstrdup(param->value); while (!quit) { if (*s == ',' || *s == '\0') { if (*s == '\0') @@ -250,7 +250,7 @@ static struct id3_tag *getId3Tag(FILE * stream, long offset, int whence) if (tagSize <= 0) return NULL; /* Found a tag. Allocate a buffer and read it in. */ - tagBuf = malloc(tagSize); + tagBuf = xmalloc(tagSize); if (!tagBuf) return NULL; tagBufSize = fillBuffer(tagBuf, tagSize, stream, offset, whence); @@ -428,7 +428,7 @@ MpdTag *apeDup(char *file) /* read tag into buffer */ tagLen -= sizeof(footer); - buffer = malloc(tagLen); + buffer = xmalloc(tagLen); if (fread(buffer, 1, tagLen, fp) != tagLen) goto fail; @@ -481,7 +481,7 @@ fail: MpdTag *newMpdTag(void) { - MpdTag *ret = malloc(sizeof(MpdTag)); + MpdTag *ret = xmalloc(sizeof(MpdTag)); ret->items = NULL; ret->time = -1; ret->numOfItems = 0; @@ -503,7 +503,7 @@ static void deleteItem(MpdTag * tag, int index) } if (tag->numOfItems > 0) { - tag->items = realloc(tag->items, + tag->items = xrealloc(tag->items, tag->numOfItems * sizeof(MpdTagItem)); } else { free(tag->items); @@ -605,7 +605,7 @@ int mpdTagsAreEqual(MpdTag * tag1, MpdTag * tag2) static void appendToTagItems(MpdTag * tag, int type, char *value, int len) { int i = tag->numOfItems; - char *dup = malloc(len + 1); + char *dup = xmalloc(len + 1); memcpy(dup, value, len); dup[len] = '\0'; @@ -614,7 +614,7 @@ static void appendToTagItems(MpdTag * tag, int type, char *value, int len) stripReturnChar(dup); tag->numOfItems++; - tag->items = realloc(tag->items, tag->numOfItems * sizeof(MpdTagItem)); + tag->items = xrealloc(tag->items, tag->numOfItems * sizeof(MpdTagItem)); tag->items[i].type = type; tag->items[i].value = getTagItemString(type, dup); diff --git a/src/tagTracker.c b/src/tagTracker.c index 6007c396e..93d3d1928 100644 --- a/src/tagTracker.c +++ b/src/tagTracker.c @@ -20,6 +20,8 @@ #include "tree.h" #include "log.h" +#include "utils.h" +#include "myfprintf.h" #include #include @@ -57,8 +59,8 @@ char *getTagItemString(int type, char *string) } else { - TagTrackerItem *item = malloc(sizeof(TagTrackerItem)); - char *key = strdup(string); + TagTrackerItem *item = xmalloc(sizeof(TagTrackerItem)); + char *key = xstrdup(string); item->count = 1; item->visited = 0; InsertInTree(tagTrees[type], key, item); diff --git a/src/tree.c b/src/tree.c index d624daadc..466ff05ab 100644 --- a/src/tree.c +++ b/src/tree.c @@ -17,6 +17,7 @@ */ #include "tree.h" +#include "utils.h" #include #include @@ -59,7 +60,7 @@ static TreeNode * _MakeNode(void) { - TreeNode * ret = malloc(sizeof(TreeNode)); + TreeNode * ret = xmalloc(sizeof(TreeNode)); memset(ret, 0, sizeof(TreeNode)); return ret; } @@ -571,7 +572,7 @@ MakeTree(TreeCompareKeyFunction compareKey, TreeFreeFunction freeKey, TreeFreeFunction freeData) { - Tree * ret = malloc(sizeof(Tree)); + Tree * ret = xmalloc(sizeof(Tree)); ret->compareKey = compareKey; ret->freeKey = freeKey; ret->freeData = freeData; diff --git a/src/utf8.c b/src/utf8.c index 4ac41516b..f6331f9f1 100644 --- a/src/utf8.c +++ b/src/utf8.c @@ -17,6 +17,7 @@ */ #include "utf8.h" +#include "utils.h" #include #include @@ -46,7 +47,7 @@ char *latin1StrToUtf8Dup(char *latin1) { /* utf8 should have at most two char's per latin1 char */ int len = strlen(latin1) * 2 + 1; - char *ret = malloc(len); + char *ret = xmalloc(len); char *cp = ret; char *utf8; @@ -63,7 +64,7 @@ char *latin1StrToUtf8Dup(char *latin1) latin1++; } - return realloc(ret, len + 1); + return xrealloc(ret, len + 1); } static char utf8ToLatin1(char *inUtf8) @@ -124,7 +125,7 @@ char *utf8StrToLatin1Dup(char *utf8) { /* utf8 should have at most two char's per latin1 char */ int len = strlen(utf8) + 1; - char *ret = malloc(len); + char *ret = xmalloc(len); char *cp = ret; int count; @@ -143,5 +144,5 @@ char *utf8StrToLatin1Dup(char *utf8) len++; } - return realloc(ret, len + 1); + return xrealloc(ret, len + 1); } diff --git a/src/utils.c b/src/utils.c index 772c25a0a..60fa3f135 100644 --- a/src/utils.c +++ b/src/utils.c @@ -17,6 +17,7 @@ */ #include "utils.h" +#include "log.h" #include #include @@ -26,6 +27,7 @@ #include #include #include +#include char *myFgets(char *buffer, int bufferSize, FILE * fp) { @@ -41,7 +43,7 @@ char *myFgets(char *buffer, int bufferSize, FILE * fp) char *strDupToUpper(char *str) { - char *ret = strdup(str); + char *ret = xstrdup(str); int i; for (i = 0; i < strlen(str); i++) @@ -86,12 +88,12 @@ char *appendToString(char *dest, const char *src) int srclen = strlen(src); if (dest == NULL) { - dest = malloc(srclen + 1); + dest = xmalloc(srclen + 1); memset(dest, 0, srclen + 1); destlen = 0; } else { destlen = strlen(dest); - dest = realloc(dest, destlen + srclen + 1); + dest = xrealloc(dest, destlen + srclen + 1); } memcpy(dest + destlen, src, srclen); @@ -106,3 +108,53 @@ unsigned long readLEuint32(const unsigned char *p) ((unsigned long)p[1] << 8) | ((unsigned long)p[2] << 16) | ((unsigned long)p[3] << 24); } + +mpd_malloc char *xstrdup(const char *s) +{ + char *ret = strdup(s); + if (mpd_unlikely(!ret)) + FATAL("OOM: strdup\n"); + return ret; +} + +/* borrowed from git :) */ + +mpd_malloc void *xmalloc(size_t size) +{ + void *ret; + + assert(mpd_likely(size)); + + ret = malloc(size); + if (mpd_unlikely(!ret)) + FATAL("OOM: malloc\n"); + return ret; +} + +mpd_malloc void *xrealloc(void *ptr, size_t size) +{ + void *ret; + + /* hmm... realloc to 0 isn't uncommon..., is it? this check + * may be too extreme... (eric) */ + assert((mpd_likely(size))); + + ret = realloc(ptr, size); + if (mpd_unlikely(!ret)) + FATAL("OOM: realloc\n"); + return ret; +} + +mpd_malloc void *xcalloc(size_t nmemb, size_t size) +{ + void *ret; + + assert(mpd_likely(nmemb && size)); + + ret = calloc(nmemb, size); + if (mpd_unlikely(!ret)) + FATAL("OOM: calloc\n"); + return ret; +} + + diff --git a/src/utils.h b/src/utils.h index 28b2845c6..ae66e3be2 100644 --- a/src/utils.h +++ b/src/utils.h @@ -20,6 +20,7 @@ #define UTILS_H #include "../config.h" +#include "gcc.h" #include #include @@ -73,4 +74,12 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len) } } +mpd_malloc char *xstrdup(const char *s); + +mpd_malloc void *xmalloc(size_t size); + +mpd_malloc void *xrealloc(void *ptr, size_t size); + +mpd_malloc void *xcalloc(size_t nmemb, size_t size); + #endif diff --git a/src/volume.c b/src/volume.c index ea9d1dd71..cd09966cc 100644 --- a/src/volume.c +++ b/src/volume.c @@ -112,7 +112,7 @@ static int prepOssMixer(char *device) } for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { - dup = strdup(labels[i]); + dup = xstrdup(labels[i]); /* eliminate spaces at the end */ j = strlen(dup) - 1; while (j >= 0 && dup[j] == ' ') -- cgit v1.2.3