Commit 90847fc8 authored by Eric Wong's avatar Eric Wong

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
parent be554c25
...@@ -119,7 +119,7 @@ void initAudioDriver(void) ...@@ -119,7 +119,7 @@ void initAudioDriver(void)
audioOutputArraySize = audio_device_count(); audioOutputArraySize = audio_device_count();
audioDeviceStates = (getPlayerData())->audioDeviceStates; audioDeviceStates = (getPlayerData())->audioDeviceStates;
audioOutputArray = malloc(sizeof(AudioOutput) * audioOutputArraySize); audioOutputArray = xmalloc(sizeof(AudioOutput) * audioOutputArraySize);
i = 0; i = 0;
param = getNextConfigParam(CONF_AUDIO_OUTPUT, param); param = getNextConfigParam(CONF_AUDIO_OUTPUT, param);
...@@ -162,7 +162,7 @@ void initAudioConfig(void) ...@@ -162,7 +162,7 @@ void initAudioConfig(void)
if (NULL == param || NULL == param->value) if (NULL == param || NULL == param->value)
return; return;
audio_configFormat = malloc(sizeof(AudioFormat)); audio_configFormat = xmalloc(sizeof(AudioFormat));
if (0 != parseAudioConfig(audio_configFormat, param->value)) { if (0 != parseAudioConfig(audio_configFormat, param->value)) {
ERROR("error parsing \"%s\" at line %i\n", ERROR("error parsing \"%s\" at line %i\n",
...@@ -335,7 +335,7 @@ int openAudioDevice(AudioFormat * audioFormat) ...@@ -335,7 +335,7 @@ int openAudioDevice(AudioFormat * audioFormat)
audioBufferSize = (audio_format.bits >> 3) * audioBufferSize = (audio_format.bits >> 3) *
audio_format.channels; audio_format.channels;
audioBufferSize *= audio_format.sampleRate >> 5; audioBufferSize *= audio_format.sampleRate >> 5;
audioBuffer = realloc(audioBuffer, audioBufferSize); audioBuffer = xrealloc(audioBuffer, audioBufferSize);
} }
syncAudioDeviceStates(); syncAudioDeviceStates();
......
...@@ -198,7 +198,7 @@ static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr, ...@@ -198,7 +198,7 @@ static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
if (size > audioOutput->convBufferLen) { if (size > audioOutput->convBufferLen) {
audioOutput->convBuffer = audioOutput->convBuffer =
realloc(audioOutput->convBuffer, size); xrealloc(audioOutput->convBuffer, size);
audioOutput->convBufferLen = size; audioOutput->convBufferLen = size;
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "audio.h" #include "audio.h"
#include "tag.h" #include "tag.h"
#include "conf.h" #include "conf.h"
#include "utils.h"
#define DISABLED_AUDIO_OUTPUT_PLUGIN(plugin) \ #define DISABLED_AUDIO_OUTPUT_PLUGIN(plugin) \
AudioOutputPlugin plugin = { \ AudioOutputPlugin plugin = { \
......
...@@ -59,7 +59,7 @@ typedef struct _AlsaData { ...@@ -59,7 +59,7 @@ typedef struct _AlsaData {
static AlsaData *newAlsaData(void) static AlsaData *newAlsaData(void)
{ {
AlsaData *ret = malloc(sizeof(AlsaData)); AlsaData *ret = xmalloc(sizeof(AlsaData));
ret->device = NULL; ret->device = NULL;
ret->pcmHandle = NULL; ret->pcmHandle = NULL;
...@@ -85,7 +85,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param) ...@@ -85,7 +85,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param)
if (param) { if (param) {
BlockParam *bp = getBlockParam(param, "device"); 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")) && if ((bp = getBlockParam(param, "use_mmap")) &&
(!strcasecmp(bp->value, "yes") || (!strcasecmp(bp->value, "yes") ||
...@@ -96,7 +96,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param) ...@@ -96,7 +96,7 @@ static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param)
if ((bp = getBlockParam(param, "period_time"))) if ((bp = getBlockParam(param, "period_time")))
ad->period_time = atoi(bp->value); ad->period_time = atoi(bp->value);
} else } else
ad->device = strdup("default"); ad->device = xstrdup("default");
audioOutput->data = ad; audioOutput->data = ad;
return 0; return 0;
......
...@@ -40,7 +40,7 @@ typedef struct _AoData { ...@@ -40,7 +40,7 @@ typedef struct _AoData {
static AoData *newAoData(void) static AoData *newAoData(void)
{ {
AoData *ret = malloc(sizeof(AoData)); AoData *ret = xmalloc(sizeof(AoData));
ret->device = NULL; ret->device = NULL;
ret->options = NULL; ret->options = NULL;
...@@ -112,9 +112,9 @@ static int audioOutputAo_initDriver(AudioOutput * audioOutput, ...@@ -112,9 +112,9 @@ static int audioOutputAo_initDriver(AudioOutput * audioOutput,
blockParam = getBlockParam(param, "options"); blockParam = getBlockParam(param, "options");
if (blockParam) { if (blockParam) {
dup = strdup(blockParam->value); dup = xstrdup(blockParam->value);
} else } else
dup = strdup(""); dup = xstrdup("");
if (strlen(dup)) { if (strlen(dup)) {
stk1 = NULL; stk1 = NULL;
......
...@@ -109,7 +109,7 @@ static int mvp_testDefault(void) ...@@ -109,7 +109,7 @@ static int mvp_testDefault(void)
static int mvp_initDriver(AudioOutput * audioOutput, ConfigParam * param) static int mvp_initDriver(AudioOutput * audioOutput, ConfigParam * param)
{ {
MvpData *md = malloc(sizeof(MvpData)); MvpData *md = xmalloc(sizeof(MvpData));
md->fd = -1; md->fd = -1;
audioOutput->data = md; audioOutput->data = md;
......
...@@ -164,7 +164,7 @@ static void addSupportedParam(OssData * od, int param, int val) ...@@ -164,7 +164,7 @@ static void addSupportedParam(OssData * od, int param, int val)
int index = getIndexForParam(param); int index = getIndexForParam(param);
od->numSupported[index]++; od->numSupported[index]++;
od->supported[index] = realloc(od->supported[index], od->supported[index] = xrealloc(od->supported[index],
od->numSupported[index] * sizeof(int)); od->numSupported[index] * sizeof(int));
od->supported[index][od->numSupported[index] - 1] = val; od->supported[index][od->numSupported[index] - 1] = val;
} }
...@@ -174,7 +174,7 @@ static void addUnsupportedParam(OssData * od, int param, int val) ...@@ -174,7 +174,7 @@ static void addUnsupportedParam(OssData * od, int param, int val)
int index = getIndexForParam(param); int index = getIndexForParam(param);
od->numUnsupported[index]++; od->numUnsupported[index]++;
od->unsupported[index] = realloc(od->unsupported[index], od->unsupported[index] = xrealloc(od->unsupported[index],
od->numUnsupported[index] * od->numUnsupported[index] *
sizeof(int)); sizeof(int));
od->unsupported[index][od->numUnsupported[index] - 1] = val; od->unsupported[index][od->numUnsupported[index] - 1] = val;
...@@ -193,7 +193,7 @@ static void removeSupportedParam(OssData * od, int param, int val) ...@@ -193,7 +193,7 @@ static void removeSupportedParam(OssData * od, int param, int val)
} }
od->numSupported[index]--; od->numSupported[index]--;
od->supported[index] = realloc(od->supported[index], od->supported[index] = xrealloc(od->supported[index],
od->numSupported[index] * sizeof(int)); od->numSupported[index] * sizeof(int));
} }
...@@ -210,7 +210,7 @@ static void removeUnsupportedParam(OssData * od, int param, int val) ...@@ -210,7 +210,7 @@ static void removeUnsupportedParam(OssData * od, int param, int val)
} }
od->numUnsupported[index]--; od->numUnsupported[index]--;
od->unsupported[index] = realloc(od->unsupported[index], od->unsupported[index] = xrealloc(od->unsupported[index],
od->numUnsupported[index] * od->numUnsupported[index] *
sizeof(int)); sizeof(int));
} }
...@@ -254,7 +254,7 @@ static void unsupportParam(OssData * od, int param, int val) ...@@ -254,7 +254,7 @@ static void unsupportParam(OssData * od, int param, int val)
static OssData *newOssData(void) static OssData *newOssData(void)
{ {
OssData *ret = malloc(sizeof(OssData)); OssData *ret = xmalloc(sizeof(OssData));
ret->device = NULL; ret->device = NULL;
ret->fd = -1; ret->fd = -1;
......
...@@ -40,7 +40,7 @@ typedef struct _OsxData { ...@@ -40,7 +40,7 @@ typedef struct _OsxData {
static OsxData *newOsxData() static OsxData *newOsxData()
{ {
OsxData *ret = malloc(sizeof(OsxData)); OsxData *ret = xmalloc(sizeof(OsxData));
pthread_mutex_init(&ret->mutex, NULL); pthread_mutex_init(&ret->mutex, NULL);
pthread_cond_init(&ret->condition, NULL); pthread_cond_init(&ret->condition, NULL);
...@@ -284,7 +284,7 @@ static int osx_openDevice(AudioOutput * audioOutput) ...@@ -284,7 +284,7 @@ static int osx_openDevice(AudioOutput * audioOutput)
/* create a buffer of 1s */ /* create a buffer of 1s */
od->bufferSize = (audioFormat->sampleRate) * od->bufferSize = (audioFormat->sampleRate) *
(audioFormat->bits >> 3) * (audioFormat->channels); (audioFormat->bits >> 3) * (audioFormat->channels);
od->buffer = realloc(od->buffer, od->bufferSize); od->buffer = xrealloc(od->buffer, od->bufferSize);
od->pos = 0; od->pos = 0;
od->len = 0; od->len = 0;
......
...@@ -46,7 +46,7 @@ static PulseData *newPulseData(void) ...@@ -46,7 +46,7 @@ static PulseData *newPulseData(void)
{ {
PulseData *ret; PulseData *ret;
ret = malloc(sizeof(PulseData)); ret = xmalloc(sizeof(PulseData));
ret->s = NULL; ret->s = NULL;
ret->server = NULL; ret->server = NULL;
...@@ -78,8 +78,8 @@ static int pulse_initDriver(AudioOutput * audioOutput, ConfigParam * param) ...@@ -78,8 +78,8 @@ static int pulse_initDriver(AudioOutput * audioOutput, ConfigParam * param)
} }
pd = newPulseData(); pd = newPulseData();
pd->server = server ? strdup(server->value) : NULL; pd->server = server ? xstrdup(server->value) : NULL;
pd->sink = sink ? strdup(sink->value) : NULL; pd->sink = sink ? xstrdup(sink->value) : NULL;
audioOutput->data = pd; audioOutput->data = pd;
return 0; return 0;
......
...@@ -74,7 +74,7 @@ typedef struct _ShoutData { ...@@ -74,7 +74,7 @@ typedef struct _ShoutData {
static ShoutData *newShoutData(void) static ShoutData *newShoutData(void)
{ {
ShoutData *ret = malloc(sizeof(ShoutData)); ShoutData *ret = xmalloc(sizeof(ShoutData));
ret->shoutConn = shout_new(); ret->shoutConn = shout_new();
ret->opened = 0; ret->opened = 0;
......
...@@ -85,37 +85,37 @@ int main() ...@@ -85,37 +85,37 @@ int main()
char *b; char *b;
int i, max; int i, max;
b = strdup("lsinfo \"/some/dir/name \\\"test\\\"\""); b = xstrdup("lsinfo \"/some/dir/name \\\"test\\\"\"");
max = buffer2array(b, a, 4); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("/some/dir/name \"test\"", a[1]) ); assert( !strcmp("/some/dir/name \"test\"", a[1]) );
assert( !a[2] ); 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); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("/some/dir/name \"test\" something else", a[1]) ); assert( !strcmp("/some/dir/name \"test\" something else", a[1]) );
assert( !a[2] ); assert( !a[2] );
b = strdup("lsinfo \"/some/dir\\\\name\""); b = xstrdup("lsinfo \"/some/dir\\\\name\"");
max = buffer2array(b, a, 4); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("/some/dir\\name", a[1]) ); assert( !strcmp("/some/dir\\name", a[1]) );
assert( !a[2] ); assert( !a[2] );
b = strdup("lsinfo \"/some/dir name\""); b = xstrdup("lsinfo \"/some/dir name\"");
max = buffer2array(b, a, 4); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("/some/dir name", a[1]) ); assert( !strcmp("/some/dir name", a[1]) );
assert( !a[2] ); assert( !a[2] );
b = strdup("lsinfo \"\\\"/some/dir\\\"\""); b = xstrdup("lsinfo \"\\\"/some/dir\\\"\"");
max = buffer2array(b, a, 4); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("\"/some/dir\"", a[1]) ); assert( !strcmp("\"/some/dir\"", a[1]) );
assert( !a[2] ); assert( !a[2] );
b = strdup("lsinfo \"\\\"/some/dir\\\" x\""); b = xstrdup("lsinfo \"\\\"/some/dir\\\" x\"");
max = buffer2array(b, a, 4); max = buffer2array(b, a, 4);
assert( !strcmp("lsinfo", a[0]) ); assert( !strcmp("lsinfo", a[0]) );
assert( !strcmp("\"/some/dir\" x", a[1]) ); assert( !strcmp("\"/some/dir\" x", a[1]) );
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "charConv.h" #include "charConv.h"
#include "mpd_types.h" #include "mpd_types.h"
#include "utf8.h" #include "utf8.h"
#include "utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
...@@ -64,8 +65,8 @@ int setCharSetConversion(char *to, char *from) ...@@ -64,8 +65,8 @@ int setCharSetConversion(char *to, char *from)
if (0 == strcmp(to, from)) { if (0 == strcmp(to, from)) {
char_conv_same = 1; char_conv_same = 1;
char_conv_to = strdup(to); char_conv_to = xstrdup(to);
char_conv_from = strdup(from); char_conv_from = xstrdup(from);
return 0; return 0;
} }
...@@ -76,16 +77,16 @@ int setCharSetConversion(char *to, char *from) ...@@ -76,16 +77,16 @@ int setCharSetConversion(char *to, char *from)
} }
if (char_conv_latin1ToUtf8 != 0) { if (char_conv_latin1ToUtf8 != 0) {
char_conv_to = strdup(to); char_conv_to = xstrdup(to);
char_conv_from = strdup(from); char_conv_from = xstrdup(from);
return 0; return 0;
} }
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if ((char_conv_iconv = iconv_open(to, from)) == (iconv_t) (-1)) if ((char_conv_iconv = iconv_open(to, from)) == (iconv_t) (-1))
return -1; return -1;
char_conv_to = strdup(to); char_conv_to = xstrdup(to);
char_conv_from = strdup(from); char_conv_from = xstrdup(from);
char_conv_use_iconv = 1; char_conv_use_iconv = 1;
return 0; return 0;
...@@ -100,7 +101,7 @@ char *convStrDup(char *string) ...@@ -100,7 +101,7 @@ char *convStrDup(char *string)
return NULL; return NULL;
if (char_conv_same) if (char_conv_same)
return strdup(string); return xstrdup(string);
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if (char_conv_use_iconv) { if (char_conv_use_iconv) {
...@@ -112,7 +113,7 @@ char *convStrDup(char *string) ...@@ -112,7 +113,7 @@ char *convStrDup(char *string)
size_t err; size_t err;
char *bufferPtr; char *bufferPtr;
ret = malloc(1); ret = xmalloc(1);
ret[0] = '\0'; ret[0] = '\0';
while (inleft) { while (inleft) {
...@@ -127,7 +128,7 @@ char *convStrDup(char *string) ...@@ -127,7 +128,7 @@ char *convStrDup(char *string)
return NULL; 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); memcpy(ret + retlen, buffer, BUFFER_SIZE - outleft);
retlen += BUFFER_SIZE - outleft; retlen += BUFFER_SIZE - outleft;
ret[retlen] = '\0'; ret[retlen] = '\0';
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "log.h" #include "log.h"
#include "dbUtils.h" #include "dbUtils.h"
#include "tag.h" #include "tag.h"
#include "utils.h"
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
...@@ -140,7 +141,7 @@ static List *commandList; ...@@ -140,7 +141,7 @@ static List *commandList;
static CommandEntry *newCommandEntry(void) static CommandEntry *newCommandEntry(void)
{ {
CommandEntry *cmd = malloc(sizeof(CommandEntry)); CommandEntry *cmd = xmalloc(sizeof(CommandEntry));
cmd->cmd = NULL; cmd->cmd = NULL;
cmd->min = 0; cmd->min = 0;
cmd->max = 0; cmd->max = 0;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "compress.h" #include "compress.h"
#include "utils.h"
#ifdef USE_X #ifdef USE_X
#include <X11/Xlib.h> #include <X11/Xlib.h>
...@@ -67,7 +68,7 @@ void CompressCfg(int show_mon, int anticlip, int target, int gainmax, ...@@ -67,7 +68,7 @@ void CompressCfg(int show_mon, int anticlip, int target, int gainmax,
prefs.buckets = buckets; prefs.buckets = buckets;
/* Allocate the peak structure */ /* Allocate the peak structure */
peaks = realloc(peaks, sizeof(int)*prefs.buckets); peaks = xrealloc(peaks, sizeof(int)*prefs.buckets);
if (prefs.buckets > lastsize) if (prefs.buckets > lastsize)
memset(peaks + lastsize, 0, sizeof(int)*(prefs.buckets memset(peaks + lastsize, 0, sizeof(int)*(prefs.buckets
......
...@@ -52,12 +52,12 @@ static List *configEntriesList = NULL; ...@@ -52,12 +52,12 @@ static List *configEntriesList = NULL;
static ConfigParam *newConfigParam(char *value, int line) static ConfigParam *newConfigParam(char *value, int line)
{ {
ConfigParam *ret = malloc(sizeof(ConfigParam)); ConfigParam *ret = xmalloc(sizeof(ConfigParam));
if (!value) if (!value)
ret->value = NULL; ret->value = NULL;
else else
ret->value = strdup(value); ret->value = xstrdup(value);
ret->line = line; ret->line = line;
...@@ -91,7 +91,7 @@ static void freeConfigParam(ConfigParam * param) ...@@ -91,7 +91,7 @@ static void freeConfigParam(ConfigParam * param)
static ConfigEntry *newConfigEntry(int repeatable, int block) static ConfigEntry *newConfigEntry(int repeatable, int block)
{ {
ConfigEntry *ret = malloc(sizeof(ConfigEntry)); ConfigEntry *ret = xmalloc(sizeof(ConfigEntry));
ret->mask = 0; ret->mask = 0;
ret->configParamList = ret->configParamList =
...@@ -180,13 +180,13 @@ static void addBlockParam(ConfigParam * param, char *name, char *value, ...@@ -180,13 +180,13 @@ static void addBlockParam(ConfigParam * param, char *name, char *value,
{ {
param->numberOfBlockParams++; param->numberOfBlockParams++;
param->blockParams = realloc(param->blockParams, param->blockParams = xrealloc(param->blockParams,
param->numberOfBlockParams * param->numberOfBlockParams *
sizeof(BlockParam)); sizeof(BlockParam));
param->blockParams[param->numberOfBlockParams - 1].name = strdup(name); param->blockParams[param->numberOfBlockParams - 1].name = xstrdup(name);
param->blockParams[param->numberOfBlockParams - 1].value = param->blockParams[param->numberOfBlockParams - 1].value =
strdup(value); xstrdup(value);
param->blockParams[param->numberOfBlockParams - 1].line = line; param->blockParams[param->numberOfBlockParams - 1].line = line;
} }
...@@ -452,7 +452,7 @@ ConfigParam *parseConfigFilePath(char *name, int force) ...@@ -452,7 +452,7 @@ ConfigParam *parseConfigFilePath(char *name, int force)
if (foundSlash) if (foundSlash)
*ch = '/'; *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); strcpy(newPath, pwd->pw_dir);
strcat(newPath, path + pos); strcat(newPath, path + pos);
free(param->value); free(param->value);
......
...@@ -69,14 +69,14 @@ static int initLocateTagItem(LocateTagItem * item, char *typeStr, char *needle) ...@@ -69,14 +69,14 @@ static int initLocateTagItem(LocateTagItem * item, char *typeStr, char *needle)
if (item->tagType < 0) if (item->tagType < 0)
return -1; return -1;
item->needle = strdup(needle); item->needle = xstrdup(needle);
return 0; return 0;
} }
LocateTagItem *newLocateTagItem(char *typeStr, char *needle) LocateTagItem *newLocateTagItem(char *typeStr, char *needle)
{ {
LocateTagItem *ret = malloc(sizeof(LocateTagItem)); LocateTagItem *ret = xmalloc(sizeof(LocateTagItem));
if (initLocateTagItem(ret, typeStr, needle) < 0) { if (initLocateTagItem(ret, typeStr, needle) < 0) {
free(ret); free(ret);
...@@ -108,7 +108,7 @@ int newLocateTagItemArrayFromArgArray(char *argArray[], ...@@ -108,7 +108,7 @@ int newLocateTagItemArrayFromArgArray(char *argArray[],
if (numArgs % 2 != 0) if (numArgs % 2 != 0)
return -1; return -1;
*arrayRet = malloc(sizeof(LocateTagItem) * numArgs / 2); *arrayRet = xmalloc(sizeof(LocateTagItem) * numArgs / 2);
for (i = 0, item = *arrayRet; i < numArgs / 2; i++, item++) { for (i = 0, item = *arrayRet; i < numArgs / 2; i++, item++) {
if (initLocateTagItem if (initLocateTagItem
...@@ -214,7 +214,7 @@ int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items) ...@@ -214,7 +214,7 @@ int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
int ret = -1; int ret = -1;
int i; int i;
char **originalNeedles = malloc(numItems * sizeof(char *)); char **originalNeedles = xmalloc(numItems * sizeof(char *));
LocateTagItemArray array; LocateTagItemArray array;
for (i = 0; i < numItems; i++) { for (i = 0; i < numItems; i++) {
...@@ -347,7 +347,7 @@ unsigned long sumSongTimesIn(int fd, char *name) ...@@ -347,7 +347,7 @@ unsigned long sumSongTimesIn(int fd, char *name)
static ListCommandItem *newListCommandItem(int tagType, int numConditionals, static ListCommandItem *newListCommandItem(int tagType, int numConditionals,
LocateTagItem * conditionals) LocateTagItem * conditionals)
{ {
ListCommandItem *item = malloc(sizeof(ListCommandItem)); ListCommandItem *item = xmalloc(sizeof(ListCommandItem));
item->tagType = tagType; item->tagType = tagType;
item->numConditionals = numConditionals; item->numConditionals = numConditionals;
......
...@@ -264,7 +264,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb, ...@@ -264,7 +264,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb,
if (isRemoteUrl(pc->utf8url)) if (isRemoteUrl(pc->utf8url))
path = utf8StrToLatin1Dup(pc->utf8url); path = utf8StrToLatin1Dup(pc->utf8url);
else else
path = strdup(rmp2amp(utf8ToFsCharset(pc->utf8url))); path = xstrdup(rmp2amp(utf8ToFsCharset(pc->utf8url)));
if (!path) { if (!path) {
dc->error = DECODE_ERROR_FILE; dc->error = DECODE_ERROR_FILE;
...@@ -304,7 +304,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb, ...@@ -304,7 +304,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb,
/*if(inStream.metaName) { /*if(inStream.metaName) {
MpdTag * tag = newMpdTag(); MpdTag * tag = newMpdTag();
tag->name = strdup(inStream.metaName); tag->name = xstrdup(inStream.metaName);
copyMpdTagToOutputBuffer(cb, tag); copyMpdTagToOutputBuffer(cb, tag);
freeMpdTag(tag); freeMpdTag(tag);
} */ } */
......
...@@ -236,7 +236,7 @@ int updateInit(int fd, List * pathList) ...@@ -236,7 +236,7 @@ int updateInit(int fd, List * pathList)
static DirectoryStat *newDirectoryStat(struct stat *st) static DirectoryStat *newDirectoryStat(struct stat *st)
{ {
DirectoryStat *ret = malloc(sizeof(DirectoryStat)); DirectoryStat *ret = xmalloc(sizeof(DirectoryStat));
ret->inode = st->st_ino; ret->inode = st->st_ino;
ret->device = st->st_dev; ret->device = st->st_dev;
return ret; return ret;
...@@ -258,10 +258,10 @@ static Directory *newDirectory(char *dirname, Directory * parent) ...@@ -258,10 +258,10 @@ static Directory *newDirectory(char *dirname, Directory * parent)
{ {
Directory *directory; Directory *directory;
directory = malloc(sizeof(Directory)); directory = xmalloc(sizeof(Directory));
if (dirname && strlen(dirname)) if (dirname && strlen(dirname))
directory->path = strdup(dirname); directory->path = xstrdup(dirname);
else else
directory->path = NULL; directory->path = NULL;
directory->subDirectories = newDirectoryList(); directory->subDirectories = newDirectoryList();
...@@ -392,11 +392,11 @@ static int removeDeletedFromDirectory(Directory * directory, DIR * dir) ...@@ -392,11 +392,11 @@ static int removeDeletedFromDirectory(Directory * directory, DIR * dir)
continue; continue;
if (directory->path) { if (directory->path) {
s = malloc(strlen(getDirectoryPath(directory)) s = xmalloc(strlen(getDirectoryPath(directory))
+ strlen(utf8) + 2); + strlen(utf8) + 2);
sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8);
} else } else
s = strdup(utf8); s = xstrdup(utf8);
insertInList(entList, utf8, s); insertInList(entList, utf8, s);
} }
...@@ -445,7 +445,7 @@ static Directory *addDirectoryPathToDB(char *utf8path, char **shortname) ...@@ -445,7 +445,7 @@ static Directory *addDirectoryPathToDB(char *utf8path, char **shortname)
Directory *parentDirectory; Directory *parentDirectory;
void *directory; void *directory;
parent = strdup(parentPath(utf8path)); parent = xstrdup(parentPath(utf8path));
if (strlen(parent) == 0) if (strlen(parent) == 0)
parentDirectory = (void *)mp3rootDirectory; parentDirectory = (void *)mp3rootDirectory;
...@@ -489,7 +489,7 @@ static Directory *addParentPathToDB(char *utf8path, char **shortname) ...@@ -489,7 +489,7 @@ static Directory *addParentPathToDB(char *utf8path, char **shortname)
char *parent; char *parent;
Directory *parentDirectory; Directory *parentDirectory;
parent = strdup(parentPath(utf8path)); parent = xstrdup(parentPath(utf8path));
if (strlen(parent) == 0) if (strlen(parent) == 0)
parentDirectory = (void *)mp3rootDirectory; parentDirectory = (void *)mp3rootDirectory;
...@@ -653,14 +653,14 @@ static int updateDirectory(Directory * directory) ...@@ -653,14 +653,14 @@ static int updateDirectory(Directory * directory)
if (!utf8) if (!utf8)
continue; continue;
utf8 = strdup(utf8); utf8 = xstrdup(utf8);
if (directory->path) { if (directory->path) {
s = malloc(strlen(getDirectoryPath(directory)) + s = xmalloc(strlen(getDirectoryPath(directory)) +
strlen(utf8) + 2); strlen(utf8) + 2);
sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8);
} else } else
s = strdup(utf8); s = xstrdup(utf8);
if (updateInDirectory(directory, utf8, s) > 0) if (updateInDirectory(directory, utf8, s) > 0)
ret = 1; ret = 1;
free(utf8); free(utf8);
...@@ -708,16 +708,16 @@ static int exploreDirectory(Directory * directory) ...@@ -708,16 +708,16 @@ static int exploreDirectory(Directory * directory)
if (!utf8) if (!utf8)
continue; continue;
utf8 = strdup(utf8); utf8 = xstrdup(utf8);
DEBUG("explore: found: %s (%s)\n", ent->d_name, utf8); DEBUG("explore: found: %s (%s)\n", ent->d_name, utf8);
if (directory->path) { if (directory->path) {
s = malloc(strlen(getDirectoryPath(directory)) + s = xmalloc(strlen(getDirectoryPath(directory)) +
strlen(utf8) + 2); strlen(utf8) + 2);
sprintf(s, "%s/%s", getDirectoryPath(directory), utf8); sprintf(s, "%s/%s", getDirectoryPath(directory), utf8);
} else } else
s = strdup(utf8); s = xstrdup(utf8);
if (addToDirectory(directory, utf8, s) > 0) if (addToDirectory(directory, utf8, s) > 0)
ret = 1; ret = 1;
free(utf8); free(utf8);
...@@ -817,7 +817,7 @@ void closeMp3Directory(void) ...@@ -817,7 +817,7 @@ void closeMp3Directory(void)
static Directory *findSubDirectory(Directory * directory, char *name) static Directory *findSubDirectory(Directory * directory, char *name)
{ {
void *subDirectory; void *subDirectory;
char *dup = strdup(name); char *dup = xstrdup(name);
char *key; char *key;
key = strtok(dup, "/"); key = strtok(dup, "/");
...@@ -942,7 +942,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) ...@@ -942,7 +942,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory)
while (myFgets(buffer, bufferSize, fp) while (myFgets(buffer, bufferSize, fp)
&& 0 != strncmp(DIRECTORY_END, buffer, strlen(DIRECTORY_END))) { && 0 != strncmp(DIRECTORY_END, buffer, strlen(DIRECTORY_END))) {
if (0 == strncmp(DIRECTORY_DIR, buffer, strlen(DIRECTORY_DIR))) { 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)) { if (!myFgets(buffer, bufferSize, fp)) {
ERROR("Error reading db, fgets\n"); ERROR("Error reading db, fgets\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -961,7 +961,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) ...@@ -961,7 +961,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory)
ERROR("Error reading db at line: %s\n", buffer); ERROR("Error reading db at line: %s\n", buffer);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
name = strdup(&(buffer[strlen(DIRECTORY_BEGIN)])); name = xstrdup(&(buffer[strlen(DIRECTORY_BEGIN)]));
while (nextDirNode && (strcmpRet = while (nextDirNode && (strcmpRet =
strcmp(key, strcmp(key,
...@@ -1034,7 +1034,7 @@ int checkDirectoryDB(void) ...@@ -1034,7 +1034,7 @@ int checkDirectoryDB(void)
/* If the file doesn't exist, we can't check if we can write /* 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 * it, so we are going to try to get the directory path, and
* see if we can write a file in that */ * see if we can write a file in that */
dbPath = strdup(dbFile); dbPath = xstrdup(dbFile);
dirPath = dirname(dbPath); dirPath = dirname(dbPath);
/* Check that the parent part of the path is a directory */ /* Check that the parent part of the path is a directory */
...@@ -1329,7 +1329,7 @@ static Song *getSongDetails(char *file, char **shortnameRet, ...@@ -1329,7 +1329,7 @@ static Song *getSongDetails(char *file, char **shortnameRet,
void *song = NULL; void *song = NULL;
Directory *directory; Directory *directory;
char *dir = NULL; char *dir = NULL;
char *dup = strdup(file); char *dup = xstrdup(file);
char *shortname = dup; char *shortname = dup;
char *c = strtok(dup, "/"); char *c = strtok(dup, "/");
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
* example taken from: http://rlove.org/log/2005102601 * example taken from: http://rlove.org/log/2005102601
*/ */
/* disabled (0) until I fix all the warnings :) */
#if __GNUC__ >= 3 #if __GNUC__ >= 3
# define mpd_const __attribute__ ((const)) # define mpd_const __attribute__ ((const))
# define mpd_deprecated __attribute__ ((deprecated)) # define mpd_deprecated __attribute__ ((deprecated))
......
...@@ -162,7 +162,7 @@ static void initAacBuffer(InputStream * inStream, AacBuffer * b, float *length, ...@@ -162,7 +162,7 @@ static void initAacBuffer(InputStream * inStream, AacBuffer * b, float *length,
fileread = inStream->size; 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); memset(b->buffer, 0, FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS);
bread = readFromInputStream(inStream, b->buffer, 1, bread = readFromInputStream(inStream, b->buffer, 1,
......
...@@ -143,9 +143,9 @@ static mod_Data *mod_open(char *path) ...@@ -143,9 +143,9 @@ static mod_Data *mod_open(char *path)
if (!(moduleHandle = Player_Load(path, 128, 0))) if (!(moduleHandle = Player_Load(path, 128, 0)))
return NULL; 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; data->moduleHandle = moduleHandle;
Player_Start(data->moduleHandle); Player_Start(data->moduleHandle);
...@@ -243,7 +243,7 @@ static MpdTag *modTagDup(char *file) ...@@ -243,7 +243,7 @@ static MpdTag *modTagDup(char *file)
ret = newMpdTag(); ret = newMpdTag();
ret->time = 0; ret->time = 0;
title = strdup(Player_LoadTitle(file)); title = xstrdup(Player_LoadTitle(file));
if (title) if (title)
addItemToMpdTag(ret, TAG_ITEM_TITLE, title); addItemToMpdTag(ret, TAG_ITEM_TITLE, title);
......
...@@ -293,7 +293,7 @@ static void mp3_parseId3Tag(mp3DecodeData * data, signed long tagsize, ...@@ -293,7 +293,7 @@ static void mp3_parseId3Tag(mp3DecodeData * data, signed long tagsize,
id3_data = data->stream.this_frame; id3_data = data->stream.this_frame;
mad_stream_skip(&(data->stream), tagsize); mad_stream_skip(&(data->stream), tagsize);
} else { } else {
allocated = malloc(tagsize); allocated = xmalloc(tagsize);
if (!allocated) if (!allocated)
goto fail; goto fail;
...@@ -682,8 +682,8 @@ static int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc, ...@@ -682,8 +682,8 @@ static int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc,
} }
} }
data->frameOffset = malloc(sizeof(long) * data->maxFrames); data->frameOffset = xmalloc(sizeof(long) * data->maxFrames);
data->times = malloc(sizeof(mad_timer_t) * data->maxFrames); data->times = xmalloc(sizeof(mad_timer_t) * data->maxFrames);
return 0; return 0;
} }
......
...@@ -123,7 +123,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path) ...@@ -123,7 +123,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
return -1; return -1;
} }
mp4cb = malloc(sizeof(mp4ff_callback_t)); mp4cb = xmalloc(sizeof(mp4ff_callback_t));
mp4cb->read = mp4_inputStreamReadCallback; mp4cb->read = mp4_inputStreamReadCallback;
mp4cb->seek = mp4_inputStreamSeekCallback; mp4cb->seek = mp4_inputStreamSeekCallback;
mp4cb->user_data = &inStream; mp4cb->user_data = &inStream;
...@@ -195,7 +195,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path) ...@@ -195,7 +195,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
time = 0.0; time = 0.0;
seekTable = malloc(sizeof(float) * numSamples); seekTable = xmalloc(sizeof(float) * numSamples);
for (sampleId = 0; sampleId < numSamples && !eof; sampleId++) { for (sampleId = 0; sampleId < numSamples && !eof; sampleId++) {
if (dc->seek) if (dc->seek)
...@@ -341,7 +341,7 @@ static MpdTag *mp4DataDup(char *file, int *mp4MetadataFound) ...@@ -341,7 +341,7 @@ static MpdTag *mp4DataDup(char *file, int *mp4MetadataFound)
return NULL; return NULL;
} }
cb = malloc(sizeof(mp4ff_callback_t)); cb = xmalloc(sizeof(mp4ff_callback_t));
cb->read = mp4_inputStreamReadCallback; cb->read = mp4_inputStreamReadCallback;
cb->seek = mp4_inputStreamSeekCallback; cb->seek = mp4_inputStreamSeekCallback;
cb->user_data = &inStream; cb->user_data = &inStream;
......
...@@ -172,7 +172,7 @@ static char *base64Dup(char *s) ...@@ -172,7 +172,7 @@ static char *base64Dup(char *s)
{ {
int i; int i;
int len = strlen(s); 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; unsigned char *p = (unsigned char *)ret;
char tbl[64] = { char tbl[64] = {
...@@ -216,14 +216,14 @@ static char *authString(char *header, char *user, char *password) ...@@ -216,14 +216,14 @@ static char *authString(char *header, char *user, char *password)
return NULL; return NULL;
templen = strlen(user) + strlen(password) + 2; templen = strlen(user) + strlen(password) + 2;
temp = malloc(templen); temp = xmalloc(templen);
strcpy(temp, user); strcpy(temp, user);
strcat(temp, ":"); strcat(temp, ":");
strcat(temp, password); strcat(temp, password);
temp64 = base64Dup(temp); temp64 = base64Dup(temp);
free(temp); free(temp);
ret = malloc(strlen(temp64) + strlen(header) + 3); ret = xmalloc(strlen(temp64) + strlen(header) + 3);
strcpy(ret, header); strcpy(ret, header);
strcat(ret, temp64); strcat(ret, temp64);
strcat(ret, "\r\n"); strcat(ret, "\r\n");
...@@ -240,7 +240,7 @@ static char *authString(char *header, char *user, char *password) ...@@ -240,7 +240,7 @@ static char *authString(char *header, char *user, char *password)
static InputStreamHTTPData *newInputStreamHTTPData(void) static InputStreamHTTPData *newInputStreamHTTPData(void)
{ {
InputStreamHTTPData *ret = malloc(sizeof(InputStreamHTTPData)); InputStreamHTTPData *ret = xmalloc(sizeof(InputStreamHTTPData));
if (proxyHost) { if (proxyHost) {
ret->proxyAuth = proxyAuthString(proxyUser, proxyPassword); ret->proxyAuth = proxyAuthString(proxyUser, proxyPassword);
...@@ -256,7 +256,7 @@ static InputStreamHTTPData *newInputStreamHTTPData(void) ...@@ -256,7 +256,7 @@ static InputStreamHTTPData *newInputStreamHTTPData(void)
ret->icyMetaint = 0; ret->icyMetaint = 0;
ret->prebuffer = 0; ret->prebuffer = 0;
ret->icyOffset = 0; ret->icyOffset = 0;
ret->buffer = malloc(bufferSize); ret->buffer = xmalloc(bufferSize);
return ret; return ret;
} }
...@@ -305,19 +305,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url) ...@@ -305,19 +305,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url)
char *passwd; char *passwd;
if (colon && colon < at) { if (colon && colon < at) {
user = malloc(colon - temp + 1); user = xmalloc(colon - temp + 1);
memcpy(user, temp, colon - temp); memcpy(user, temp, colon - temp);
user[colon - temp] = '\0'; user[colon - temp] = '\0';
passwd = malloc(at - colon); passwd = xmalloc(at - colon);
memcpy(passwd, colon + 1, at - colon - 1); memcpy(passwd, colon + 1, at - colon - 1);
passwd[at - colon - 1] = '\0'; passwd[at - colon - 1] = '\0';
} else { } else {
user = malloc(at - temp + 1); user = xmalloc(at - temp + 1);
memcpy(user, temp, at - temp); memcpy(user, temp, at - temp);
user[at - temp] = '\0'; user[at - temp] = '\0';
passwd = strdup(""); passwd = xstrdup("");
} }
data->httpAuth = httpAuthString(user, passwd); data->httpAuth = httpAuthString(user, passwd);
...@@ -345,7 +345,7 @@ static int parseUrl(InputStreamHTTPData * data, char *url) ...@@ -345,7 +345,7 @@ static int parseUrl(InputStreamHTTPData * data, char *url)
if (len <= 1) if (len <= 1)
return -1; return -1;
data->host = malloc(len); data->host = xmalloc(len);
memcpy(data->host, temp, len - 1); memcpy(data->host, temp, len - 1);
data->host[len - 1] = '\0'; data->host[len - 1] = '\0';
/* fetch the port */ /* fetch the port */
...@@ -353,19 +353,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url) ...@@ -353,19 +353,19 @@ static int parseUrl(InputStreamHTTPData * data, char *url)
len = strlen(colon) - 1; len = strlen(colon) - 1;
if (slash) if (slash)
len -= strlen(slash); len -= strlen(slash);
data->port = malloc(len + 1); data->port = xmalloc(len + 1);
memcpy(data->port, colon + 1, len); memcpy(data->port, colon + 1, len);
data->port[len] = '\0'; data->port[len] = '\0';
DEBUG(__FILE__ ": Port: %s\n", data->port); DEBUG(__FILE__ ": Port: %s\n", data->port);
} else { } else {
data->port = strdup("80"); data->port = xstrdup("80");
} }
/* fetch the path */ /* fetch the path */
if (proxyHost) if (proxyHost)
data->path = strdup(url); data->path = xstrdup(url);
else else
data->path = strdup(slash ? slash : "/"); data->path = xstrdup(slash ? slash : "/");
return 0; return 0;
} }
...@@ -591,7 +591,7 @@ static int getHTTPHello(InputStream * inStream) ...@@ -591,7 +591,7 @@ static int getHTTPHello(InputStream * inStream)
&& *(cur + curlen) != '\r') { && *(cur + curlen) != '\r') {
curlen++; curlen++;
} }
url = malloc(curlen + 1); url = xmalloc(curlen + 1);
memcpy(url, cur, curlen); memcpy(url, cur, curlen);
url[curlen] = '\0'; url[curlen] = '\0';
ret = parseUrl(data, url); ret = parseUrl(data, url);
...@@ -634,7 +634,7 @@ static int getHTTPHello(InputStream * inStream) ...@@ -634,7 +634,7 @@ static int getHTTPHello(InputStream * inStream)
free(inStream->metaName); free(inStream->metaName);
while (*(incr + cur) == ' ') while (*(incr + cur) == ' ')
incr++; incr++;
inStream->metaName = strdup(cur + incr); inStream->metaName = xstrdup(cur + incr);
*temp = '\r'; *temp = '\r';
DEBUG("inputStream_http: metaName: %s\n", DEBUG("inputStream_http: metaName: %s\n",
inStream->metaName); inStream->metaName);
...@@ -648,7 +648,7 @@ static int getHTTPHello(InputStream * inStream) ...@@ -648,7 +648,7 @@ static int getHTTPHello(InputStream * inStream)
free(inStream->metaName); free(inStream->metaName);
while (*(incr + cur) == ' ') while (*(incr + cur) == ' ')
incr++; incr++;
inStream->metaName = strdup(cur + incr); inStream->metaName = xstrdup(cur + incr);
*temp = '\r'; *temp = '\r';
DEBUG("inputStream_http: metaName: %s\n", DEBUG("inputStream_http: metaName: %s\n",
inStream->metaName); inStream->metaName);
...@@ -662,7 +662,7 @@ static int getHTTPHello(InputStream * inStream) ...@@ -662,7 +662,7 @@ static int getHTTPHello(InputStream * inStream)
free(inStream->mime); free(inStream->mime);
while (*(incr + cur) == ' ') while (*(incr + cur) == ' ')
incr++; incr++;
inStream->mime = strdup(cur + incr); inStream->mime = xstrdup(cur + incr);
*temp = '\r'; *temp = '\r';
} }
...@@ -735,7 +735,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size) ...@@ -735,7 +735,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size)
{ {
char *r; char *r;
char *s; char *s;
char *temp = malloc(size + 1); char *temp = xmalloc(size + 1);
memcpy(temp, metadata, size); memcpy(temp, metadata, size);
temp[size] = '\0'; temp[size] = '\0';
s = strtok_r(temp, ";", &r); s = strtok_r(temp, ";", &r);
...@@ -749,7 +749,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size) ...@@ -749,7 +749,7 @@ static void parseIcyMetadata(InputStream * inStream, char *metadata, int size)
if (s[strlen(s) - 1] == '\'') { if (s[strlen(s) - 1] == '\'') {
s[strlen(s) - 1] = '\0'; s[strlen(s) - 1] = '\0';
} }
inStream->metaTitle = strdup(s + cur); inStream->metaTitle = xstrdup(s + cur);
DEBUG("inputStream_http: metaTitle: %s\n", DEBUG("inputStream_http: metaTitle: %s\n",
inStream->metaTitle); inStream->metaTitle);
} }
......
...@@ -132,7 +132,7 @@ static void set_send_buf_size(Interface * interface) ...@@ -132,7 +132,7 @@ static void set_send_buf_size(Interface * interface)
if (interface->send_buf_alloc < new_size) { if (interface->send_buf_alloc < new_size) {
if (interface->send_buf) if (interface->send_buf)
free(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; interface->send_buf_alloc = new_size;
} }
} }
...@@ -593,9 +593,9 @@ void initInterfaces(void) ...@@ -593,9 +593,9 @@ void initInterfaces(void)
interface_max_output_buffer_size *= 1024; 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_head = &(list_cache[0]);
list_cache_tail = &(list_cache[interface_list_cache_size - 1]); list_cache_tail = &(list_cache[interface_list_cache_size - 1]);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
#include "list.h" #include "list.h"
#include "utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -32,7 +33,7 @@ static void makeListNodesArray(List * list) ...@@ -32,7 +33,7 @@ static void makeListNodesArray(List * list)
if (!list->numberOfNodes) if (!list->numberOfNodes)
return; return;
list->nodesArray = realloc(list->nodesArray, list->nodesArray = xrealloc(list->nodesArray,
sizeof(ListNode *) * list->numberOfNodes); sizeof(ListNode *) * list->numberOfNodes);
for (i = 0; i < list->numberOfNodes; i++) { for (i = 0; i < list->numberOfNodes; i++) {
...@@ -51,7 +52,7 @@ static void freeListNodesArray(List * list) ...@@ -51,7 +52,7 @@ static void freeListNodesArray(List * list)
List *makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys) List *makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys)
{ {
List *list = malloc(sizeof(List)); List *list = xmalloc(sizeof(List));
assert(list != NULL); assert(list != NULL);
...@@ -75,7 +76,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, ...@@ -75,7 +76,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos,
assert(key != NULL); assert(key != NULL);
/*assert(data!=NULL); */ /*assert(data!=NULL); */
node = malloc(sizeof(ListNode)); node = xmalloc(sizeof(ListNode));
assert(node != NULL); assert(node != NULL);
node->nextNode = beforeNode; node->nextNode = beforeNode;
...@@ -102,7 +103,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, ...@@ -102,7 +103,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos,
} }
if (list->strdupKeys) if (list->strdupKeys)
node->key = strdup(key); node->key = xstrdup(key);
else else
node->key = key; node->key = key;
...@@ -111,7 +112,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, ...@@ -111,7 +112,7 @@ ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos,
list->numberOfNodes++; list->numberOfNodes++;
if (list->sorted) { if (list->sorted) {
list->nodesArray = realloc(list->nodesArray, list->nodesArray = xrealloc(list->nodesArray,
list->numberOfNodes * list->numberOfNodes *
sizeof(ListNode *)); sizeof(ListNode *));
if (node == list->lastNode) { if (node == list->lastNode) {
...@@ -138,7 +139,7 @@ ListNode *insertInList(List * list, char *key, void *data) ...@@ -138,7 +139,7 @@ ListNode *insertInList(List * list, char *key, void *data)
assert(key != NULL); assert(key != NULL);
/*assert(data!=NULL); */ /*assert(data!=NULL); */
node = malloc(sizeof(ListNode)); node = xmalloc(sizeof(ListNode));
assert(node != NULL); assert(node != NULL);
if (list->nodesArray) if (list->nodesArray)
...@@ -154,7 +155,7 @@ ListNode *insertInList(List * list, char *key, void *data) ...@@ -154,7 +155,7 @@ ListNode *insertInList(List * list, char *key, void *data)
} }
if (list->strdupKeys) if (list->strdupKeys)
node->key = strdup(key); node->key = xstrdup(key);
else else
node->key = key; node->key = key;
...@@ -176,7 +177,7 @@ int insertInListWithoutKey(List * list, void *data) ...@@ -176,7 +177,7 @@ int insertInListWithoutKey(List * list, void *data)
assert(list != NULL); assert(list != NULL);
assert(data != NULL); assert(data != NULL);
node = malloc(sizeof(ListNode)); node = xmalloc(sizeof(ListNode));
assert(node != NULL); assert(node != NULL);
if (list->nodesArray) if (list->nodesArray)
...@@ -416,8 +417,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) ...@@ -416,8 +417,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end)
List *startList = makeList(free, 0); List *startList = makeList(free, 0);
List *endList = makeList(free, 0); List *endList = makeList(free, 0);
long *startPtr = malloc(sizeof(long)); long *startPtr = xmalloc(sizeof(long));
long *endPtr = malloc(sizeof(long)); long *endPtr = xmalloc(sizeof(long));
*startPtr = start; *startPtr = start;
*endPtr = end; *endPtr = end;
insertInListWithoutKey(startList, (void *)startPtr); insertInListWithoutKey(startList, (void *)startPtr);
...@@ -471,8 +472,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) ...@@ -471,8 +472,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end)
deleteNodeFromList(endList, endList->lastNode); deleteNodeFromList(endList, endList->lastNode);
if (pivot - 1 - start > 0) { if (pivot - 1 - start > 0) {
startPtr = malloc(sizeof(long)); startPtr = xmalloc(sizeof(long));
endPtr = malloc(sizeof(long)); endPtr = xmalloc(sizeof(long));
*startPtr = start; *startPtr = start;
*endPtr = pivot - 1; *endPtr = pivot - 1;
insertInListWithoutKey(startList, insertInListWithoutKey(startList,
...@@ -483,8 +484,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end) ...@@ -483,8 +484,8 @@ static void quickSort(ListNode ** nodesArray, long start, long end)
} }
if (end - pivot - 1 > 0) { if (end - pivot - 1 > 0) {
startPtr = malloc(sizeof(long)); startPtr = xmalloc(sizeof(long));
endPtr = malloc(sizeof(long)); endPtr = xmalloc(sizeof(long));
*startPtr = pivot + 1; *startPtr = pivot + 1;
*endPtr = end; *endPtr = end;
insertInListWithoutKey(startList, insertInListWithoutKey(startList,
......
...@@ -101,7 +101,7 @@ static int establishListen(unsigned int port, ...@@ -101,7 +101,7 @@ static int establishListen(unsigned int port,
numberOfListenSockets++; numberOfListenSockets++;
listenSockets = listenSockets =
realloc(listenSockets, sizeof(int) * numberOfListenSockets); xrealloc(listenSockets, sizeof(int) * numberOfListenSockets);
listenSockets[numberOfListenSockets - 1] = sock; listenSockets[numberOfListenSockets - 1] = sock;
......
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
#define LOG_H #define LOG_H
#include "../config.h" #include "../config.h"
#include "gcc.h"
#include "myfprintf.h"
#include <unistd.h> #include <unistd.h>
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "myfprintf.h" #include "myfprintf.h"
#include "log.h" #include "log.h"
#include "utf8.h" #include "utf8.h"
#include "utils.h"
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
...@@ -154,7 +155,7 @@ int lsPlaylists(int fd, char *utf8path) ...@@ -154,7 +155,7 @@ int lsPlaylists(int fd, char *utf8path)
int i; int i;
sortList(list); sortList(list);
dup = malloc(strlen(utf8path) + 2); dup = xmalloc(strlen(utf8path) + 2);
strcpy(dup, utf8path); strcpy(dup, utf8path);
for (i = strlen(dup) - 1; i >= 0 && dup[i] == '/'; i--) { for (i = strlen(dup) - 1; i >= 0 && dup[i] == '/'; i--) {
dup[i] = '\0'; dup[i] = '\0';
......
...@@ -90,7 +90,7 @@ static int setenv(const char *name, const char *value, int replace) ...@@ -90,7 +90,7 @@ static int setenv(const char *name, const char *value, int replace)
namelen = strlen(name); namelen = strlen(name);
valuelen = strlen(value); valuelen = strlen(value);
envstr = malloc((namelen + valuelen + 2)); envstr = xmalloc((namelen + valuelen + 2));
if (!envstr) if (!envstr)
return -1; return -1;
......
...@@ -91,7 +91,7 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream, ...@@ -91,7 +91,7 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
&(cb-> &(cb->
audioFormat)); audioFormat));
if (datalen > convBufferLen) { if (datalen > convBufferLen) {
convBuffer = realloc(convBuffer, datalen); convBuffer = xrealloc(convBuffer, datalen);
convBufferLen = datalen; convBufferLen = datalen;
} }
data = convBuffer; data = convBuffer;
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "charConv.h" #include "charConv.h"
#include "conf.h" #include "conf.h"
#include "utf8.h" #include "utf8.h"
#include "utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -69,7 +70,7 @@ char *utf8ToFsCharset(char *str) ...@@ -69,7 +70,7 @@ char *utf8ToFsCharset(char *str)
ret = pathConvCharset(fsCharset, "UTF-8", str, ret); ret = pathConvCharset(fsCharset, "UTF-8", str, ret);
if (!ret) if (!ret)
ret = strdup(str); ret = xstrdup(str);
return ret; return ret;
} }
...@@ -81,7 +82,7 @@ void setFsCharset(char *charset) ...@@ -81,7 +82,7 @@ void setFsCharset(char *charset)
if (fsCharset) if (fsCharset)
free(fsCharset); free(fsCharset);
fsCharset = strdup(charset); fsCharset = xstrdup(charset);
DEBUG("setFsCharset: fs charset is: %s\n", fsCharset); DEBUG("setFsCharset: fs charset is: %s\n", fsCharset);
...@@ -101,7 +102,7 @@ void setFsCharset(char *charset) ...@@ -101,7 +102,7 @@ void setFsCharset(char *charset)
if (error) { if (error) {
free(fsCharset); free(fsCharset);
WARNING("setting fs charset to ISO-8859-1!\n"); 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) ...@@ -116,7 +117,7 @@ static char *appendSlash(char **path)
int len = strlen(temp); int len = strlen(temp);
if (temp[len - 1] != '/') { if (temp[len - 1] != '/') {
temp = malloc(len + 2); temp = xmalloc(len + 2);
memset(temp, 0, len + 2); memset(temp, 0, len + 2);
memcpy(temp, *path, len); memcpy(temp, *path, len);
temp[len] = '/'; temp[len] = '/';
...@@ -157,14 +158,14 @@ void initPaths(void) ...@@ -157,14 +158,14 @@ void initPaths(void)
closedir(dir); closedir(dir);
if (fsCharsetParam) { if (fsCharsetParam) {
charset = strdup(fsCharsetParam->value); charset = xstrdup(fsCharsetParam->value);
} }
#ifdef HAVE_LOCALE #ifdef HAVE_LOCALE
#ifdef HAVE_LANGINFO_CODESET #ifdef HAVE_LANGINFO_CODESET
else if ((originalLocale = setlocale(LC_CTYPE, NULL))) { else if ((originalLocale = setlocale(LC_CTYPE, NULL))) {
char *temp; char *temp;
char *currentLocale; char *currentLocale;
originalLocale = strdup(originalLocale); originalLocale = xstrdup(originalLocale);
if (!(currentLocale = setlocale(LC_CTYPE, ""))) { if (!(currentLocale = setlocale(LC_CTYPE, ""))) {
WARNING("problems setting current locale with " WARNING("problems setting current locale with "
...@@ -175,7 +176,7 @@ void initPaths(void) ...@@ -175,7 +176,7 @@ void initPaths(void)
WARNING("current locale is \"%s\"\n", WARNING("current locale is \"%s\"\n",
currentLocale); currentLocale);
} else if ((temp = nl_langinfo(CODESET))) { } else if ((temp = nl_langinfo(CODESET))) {
charset = strdup(temp); charset = xstrdup(temp);
} else } else
WARNING WARNING
("problems getting charset for locale\n"); ("problems getting charset for locale\n");
...@@ -273,7 +274,7 @@ char *parentPath(char *path) ...@@ -273,7 +274,7 @@ char *parentPath(char *path)
char *sanitizePathDup(char *path) char *sanitizePathDup(char *path)
{ {
int len = strlen(path) + 1; int len = strlen(path) + 1;
char *ret = malloc(len); char *ret = xmalloc(len);
char *cp = ret; char *cp = ret;
memset(ret, 0, len); memset(ret, 0, len);
...@@ -307,5 +308,5 @@ char *sanitizePathDup(char *path) ...@@ -307,5 +308,5 @@ char *sanitizePathDup(char *path)
DEBUG("sanitized: %s\n", ret); DEBUG("sanitized: %s\n", ret);
return realloc(ret, len + 1); return xrealloc(ret, len + 1);
} }
...@@ -34,7 +34,7 @@ void finishPaths(void); ...@@ -34,7 +34,7 @@ void finishPaths(void);
* which means: * which means:
* - Do not manually free the return value of these functions, it'll be * - Do not manually free the return value of these functions, it'll be
* automatically freed the next time it is called. * 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 * you expect to call one of these functions again, but still need the
* previous result. * previous result.
* - The static pointer is unique to each function. * - The static pointer is unique to each function.
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "mpd_types.h" #include "mpd_types.h"
#include "log.h" #include "log.h"
#include "utils.h"
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
...@@ -153,7 +154,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t ...@@ -153,7 +154,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
case 8: case 8:
dataBitLen = inSize << 1; dataBitLen = inSize << 1;
if (dataBitLen > bitConvBufferLength) { if (dataBitLen > bitConvBufferLength) {
bitConvBuffer = realloc(bitConvBuffer, dataBitLen); bitConvBuffer = xrealloc(bitConvBuffer, dataBitLen);
bitConvBufferLength = dataBitLen; bitConvBufferLength = dataBitLen;
} }
dataBitConv = bitConvBuffer; dataBitConv = bitConvBuffer;
...@@ -187,7 +188,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t ...@@ -187,7 +188,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
case 1: case 1:
dataChannelLen = (dataBitLen >> 1) << 2; dataChannelLen = (dataBitLen >> 1) << 2;
if (dataChannelLen > channelConvBufferLength) { if (dataChannelLen > channelConvBufferLength) {
channelConvBuffer = realloc(channelConvBuffer, channelConvBuffer = xrealloc(channelConvBuffer,
dataChannelLen); dataChannelLen);
channelConvBufferLength = dataChannelLen; channelConvBufferLength = dataChannelLen;
} }
...@@ -207,7 +208,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t ...@@ -207,7 +208,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
case 2: case 2:
dataChannelLen = dataBitLen >> 1; dataChannelLen = dataBitLen >> 1;
if (dataChannelLen > channelConvBufferLength) { if (dataChannelLen > channelConvBufferLength) {
channelConvBuffer = realloc(channelConvBuffer, channelConvBuffer = xrealloc(channelConvBuffer,
dataChannelLen); dataChannelLen);
channelConvBufferLength = dataChannelLen; channelConvBufferLength = dataChannelLen;
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "conf.h" #include "conf.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "utils.h"
#include <string.h> #include <string.h>
...@@ -103,7 +104,7 @@ void initPermissions(void) ...@@ -103,7 +104,7 @@ void initPermissions(void)
password = temp; password = temp;
permission = malloc(sizeof(int)); permission = xmalloc(sizeof(int));
*permission = *permission =
parsePermissions(strtok_r(NULL, "", &cp2)); parsePermissions(strtok_r(NULL, "", &cp2));
......
...@@ -301,7 +301,7 @@ char *getPlayerErrorStr(void) ...@@ -301,7 +301,7 @@ char *getPlayerErrorStr(void)
int errorlen = MAXPATHLEN + 1024; int errorlen = MAXPATHLEN + 1024;
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
error = realloc(error, errorlen + 1); error = xrealloc(error, errorlen + 1);
memset(error, 0, errorlen + 1); memset(error, 0, errorlen + 1);
switch (pc->error) { switch (pc->error) {
...@@ -328,7 +328,7 @@ char *getPlayerErrorStr(void) ...@@ -328,7 +328,7 @@ char *getPlayerErrorStr(void)
} }
errorlen = strlen(error); errorlen = strlen(error);
error = realloc(error, errorlen + 1); error = xrealloc(error, errorlen + 1);
if (errorlen) if (errorlen)
return error; return error;
...@@ -513,12 +513,12 @@ Song *playerCurrentDecodeSong(void) ...@@ -513,12 +513,12 @@ Song *playerCurrentDecodeSong(void)
DEBUG("playerCurrentDecodeSong: caught new metadata!\n"); DEBUG("playerCurrentDecodeSong: caught new metadata!\n");
if (prev) if (prev)
free(prev); free(prev);
prev = malloc(sizeof(MetadataChunk)); prev = xmalloc(sizeof(MetadataChunk));
memcpy(prev, &(pc->metadataChunk), sizeof(MetadataChunk)); memcpy(prev, &(pc->metadataChunk), sizeof(MetadataChunk));
if (song) if (song)
freeJustSong(song); freeJustSong(song);
song = newNullSong(); song = newNullSong();
song->url = strdup(pc->currentUrl); song->url = xstrdup(pc->currentUrl);
song->tag = metadataChunkToMpdTagDup(prev); song->tag = metadataChunkToMpdTagDup(prev);
ret = song; ret = song;
resetPlayerMetadata(); resetPlayerMetadata();
......
...@@ -161,12 +161,12 @@ void initPlaylist(void) ...@@ -161,12 +161,12 @@ void initPlaylist(void)
if (playlist_saveAbsolutePaths == -1) playlist_saveAbsolutePaths = 0; if (playlist_saveAbsolutePaths == -1) playlist_saveAbsolutePaths = 0;
else if (playlist_saveAbsolutePaths < 0) exit(EXIT_FAILURE); else if (playlist_saveAbsolutePaths < 0) exit(EXIT_FAILURE);
playlist.songs = malloc(sizeof(Song *) * playlist_max_length); playlist.songs = xmalloc(sizeof(Song *) * playlist_max_length);
playlist.songMod = malloc(sizeof(mpd_uint32) * playlist_max_length); playlist.songMod = xmalloc(sizeof(mpd_uint32) * playlist_max_length);
playlist.order = malloc(sizeof(int) * playlist_max_length); playlist.order = xmalloc(sizeof(int) * playlist_max_length);
playlist.idToPosition = malloc(sizeof(int) * playlist_max_length * playlist.idToPosition = xmalloc(sizeof(int) * playlist_max_length *
PLAYLIST_HASH_MULT); 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); memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
...@@ -1264,7 +1264,7 @@ int shufflePlaylist(int fd) ...@@ -1264,7 +1264,7 @@ int shufflePlaylist(int fd)
int deletePlaylist(int fd, char *utf8file) int deletePlaylist(int fd, char *utf8file)
{ {
char *file = utf8ToFsCharset(utf8file); char *file = utf8ToFsCharset(utf8file);
char *rfile = malloc(strlen(file) + strlen(".") + char *rfile = xmalloc(strlen(file) + strlen(".") +
strlen(PLAYLIST_FILE_SUFFIX) + 1); strlen(PLAYLIST_FILE_SUFFIX) + 1);
char *actualFile; char *actualFile;
...@@ -1308,7 +1308,7 @@ int savePlaylist(int fd, char *utf8file) ...@@ -1308,7 +1308,7 @@ int savePlaylist(int fd, char *utf8file)
file = utf8ToFsCharset(utf8file); file = utf8ToFsCharset(utf8file);
rfile = malloc(strlen(file) + strlen(".") + rfile = xmalloc(strlen(file) + strlen(".") +
strlen(PLAYLIST_FILE_SUFFIX) + 1); strlen(PLAYLIST_FILE_SUFFIX) + 1);
strcpy(rfile, file); strcpy(rfile, file);
...@@ -1421,7 +1421,7 @@ static int PlaylistIterFunc(int fd, char *utf8file, ...@@ -1421,7 +1421,7 @@ static int PlaylistIterFunc(int fd, char *utf8file,
char s[MAXPATHLEN + 1]; char s[MAXPATHLEN + 1];
int slength = 0; int slength = 0;
char *temp = utf8ToFsCharset(utf8file); char *temp = utf8ToFsCharset(utf8file);
char *rfile = malloc(strlen(temp) + strlen(".") + char *rfile = xmalloc(strlen(temp) + strlen(".") +
strlen(PLAYLIST_FILE_SUFFIX) + 1); strlen(PLAYLIST_FILE_SUFFIX) + 1);
char *actualFile; char *actualFile;
char *parent = parentPath(temp); char *parent = parentPath(temp);
...@@ -1461,7 +1461,7 @@ static int PlaylistIterFunc(int fd, char *utf8file, ...@@ -1461,7 +1461,7 @@ static int PlaylistIterFunc(int fd, char *utf8file,
if (strncmp(s, musicDir, strlen(musicDir)) == 0) { if (strncmp(s, musicDir, strlen(musicDir)) == 0) {
strcpy(s, &(s[strlen(musicDir)])); strcpy(s, &(s[strlen(musicDir)]));
} else if (parentlen) { } else if (parentlen) {
temp = strdup(s); temp = xstrdup(s);
memset(s, 0, MAXPATHLEN + 1); memset(s, 0, MAXPATHLEN + 1);
strcpy(s, parent); strcpy(s, parent);
strncat(s, "/", MAXPATHLEN - parentlen); strncat(s, "/", MAXPATHLEN - parentlen);
...@@ -1536,7 +1536,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile) ...@@ -1536,7 +1536,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile)
} else if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) { } else if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
/* for windows compatibilit, convert slashes */ /* for windows compatibilit, convert slashes */
char *temp2 = strdup(temp); char *temp2 = xstrdup(temp);
char *p = temp2; char *p = temp2;
while (*p) { while (*p) {
if (*p == '\\') if (*p == '\\')
...@@ -1545,7 +1545,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile) ...@@ -1545,7 +1545,7 @@ static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile)
} }
if ((addToPlaylist(STDERR_FILENO, temp2, 0)) < 0) { if ((addToPlaylist(STDERR_FILENO, temp2, 0)) < 0) {
if (!*erroredFile) { if (!*erroredFile) {
*erroredFile = strdup(temp); *erroredFile = xstrdup(temp);
} }
} }
free(temp2); free(temp2);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
#include "replayGain.h" #include "replayGain.h"
#include "utils.h"
#include "log.h" #include "log.h"
#include "conf.h" #include "conf.h"
...@@ -89,7 +90,7 @@ static float computeReplayGainScale(float gain, float peak) ...@@ -89,7 +90,7 @@ static float computeReplayGainScale(float gain, float peak)
ReplayGainInfo *newReplayGainInfo(void) ReplayGainInfo *newReplayGainInfo(void)
{ {
ReplayGainInfo *ret = malloc(sizeof(ReplayGainInfo)); ReplayGainInfo *ret = xmalloc(sizeof(ReplayGainInfo));
ret->albumGain = 0.0; ret->albumGain = 0.0;
ret->albumPeak = 0.0; ret->albumPeak = 0.0;
......
...@@ -30,14 +30,14 @@ static void init_strnode(struct strnode *x, char *s) ...@@ -30,14 +30,14 @@ static void init_strnode(struct strnode *x, char *s)
struct strnode *new_strnode(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); init_strnode(x, s);
return x; return x;
} }
struct strnode *new_strnode_dup(char *s, const size_t size) 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->next = NULL;
x->data = ((char *)x + sizeof(struct strnode)); x->data = ((char *)x + sizeof(struct strnode));
memcpy((void *)x->data, (void*)s, size); memcpy((void *)x->data, (void*)s, size);
...@@ -46,7 +46,7 @@ struct strnode *new_strnode_dup(char *s, const size_t 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 *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->next = NULL;
x->size = size; x->size = size;
x->data = ((char *)x + sizeof(struct sllnode)); x->data = ((char *)x + sizeof(struct sllnode));
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
* should _NEVER_ be explicitly freed * should _NEVER_ be explicitly freed
* *
* there's no free command, iterate through them yourself and just * 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 {
struct strnode *next; struct strnode *next;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "path.h" #include "path.h"
#include "playlist.h" #include "playlist.h"
#include "inputPlugin.h" #include "inputPlugin.h"
#include "myfprintf.h"
#define SONG_KEY "key: " #define SONG_KEY "key: "
#define SONG_FILE "file: " #define SONG_FILE "file: "
...@@ -37,7 +38,7 @@ ...@@ -37,7 +38,7 @@
Song *newNullSong(void) Song *newNullSong(void)
{ {
Song *song = malloc(sizeof(Song)); Song *song = xmalloc(sizeof(Song));
song->tag = NULL; song->tag = NULL;
song->url = NULL; song->url = NULL;
...@@ -58,7 +59,7 @@ Song *newSong(char *url, int type, Directory * parentDir) ...@@ -58,7 +59,7 @@ Song *newSong(char *url, int type, Directory * parentDir)
song = newNullSong(); song = newNullSong();
song->url = strdup(url); song->url = xstrdup(url);
song->type = type; song->type = type;
song->parentDir = parentDir; song->parentDir = parentDir;
...@@ -248,7 +249,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) ...@@ -248,7 +249,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir)
} }
song = newNullSong(); song = newNullSong();
song->url = strdup(buffer + strlen(SONG_KEY)); song->url = xstrdup(buffer + strlen(SONG_KEY));
song->type = SONG_TYPE_FILE; song->type = SONG_TYPE_FILE;
song->parentDir = parentDir; song->parentDir = parentDir;
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
...@@ -257,7 +258,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) ...@@ -257,7 +258,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* we don't need this info anymore /* 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)) { } else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
if (!song->tag) if (!song->tag)
...@@ -346,7 +347,7 @@ char *getSongUrl(Song * song) ...@@ -346,7 +347,7 @@ char *getSongUrl(Song * song)
size = slen + dlen + 2; size = slen + dlen + 2;
if (size > bufferSize) { if (size > bufferSize) {
buffer = realloc(buffer, size); buffer = xrealloc(buffer, size);
bufferSize = size; bufferSize = size;
} }
......
...@@ -93,7 +93,7 @@ void initTagConfig(void) ...@@ -93,7 +93,7 @@ void initTagConfig(void)
if (0 == strcasecmp(param->value, "none")) if (0 == strcasecmp(param->value, "none"))
return; return;
temp = c = s = strdup(param->value); temp = c = s = xstrdup(param->value);
while (!quit) { while (!quit) {
if (*s == ',' || *s == '\0') { if (*s == ',' || *s == '\0') {
if (*s == '\0') if (*s == '\0')
...@@ -250,7 +250,7 @@ static struct id3_tag *getId3Tag(FILE * stream, long offset, int whence) ...@@ -250,7 +250,7 @@ static struct id3_tag *getId3Tag(FILE * stream, long offset, int whence)
if (tagSize <= 0) return NULL; if (tagSize <= 0) return NULL;
/* Found a tag. Allocate a buffer and read it in. */ /* Found a tag. Allocate a buffer and read it in. */
tagBuf = malloc(tagSize); tagBuf = xmalloc(tagSize);
if (!tagBuf) return NULL; if (!tagBuf) return NULL;
tagBufSize = fillBuffer(tagBuf, tagSize, stream, offset, whence); tagBufSize = fillBuffer(tagBuf, tagSize, stream, offset, whence);
...@@ -428,7 +428,7 @@ MpdTag *apeDup(char *file) ...@@ -428,7 +428,7 @@ MpdTag *apeDup(char *file)
/* read tag into buffer */ /* read tag into buffer */
tagLen -= sizeof(footer); tagLen -= sizeof(footer);
buffer = malloc(tagLen); buffer = xmalloc(tagLen);
if (fread(buffer, 1, tagLen, fp) != tagLen) if (fread(buffer, 1, tagLen, fp) != tagLen)
goto fail; goto fail;
...@@ -481,7 +481,7 @@ fail: ...@@ -481,7 +481,7 @@ fail:
MpdTag *newMpdTag(void) MpdTag *newMpdTag(void)
{ {
MpdTag *ret = malloc(sizeof(MpdTag)); MpdTag *ret = xmalloc(sizeof(MpdTag));
ret->items = NULL; ret->items = NULL;
ret->time = -1; ret->time = -1;
ret->numOfItems = 0; ret->numOfItems = 0;
...@@ -503,7 +503,7 @@ static void deleteItem(MpdTag * tag, int index) ...@@ -503,7 +503,7 @@ static void deleteItem(MpdTag * tag, int index)
} }
if (tag->numOfItems > 0) { if (tag->numOfItems > 0) {
tag->items = realloc(tag->items, tag->items = xrealloc(tag->items,
tag->numOfItems * sizeof(MpdTagItem)); tag->numOfItems * sizeof(MpdTagItem));
} else { } else {
free(tag->items); free(tag->items);
...@@ -605,7 +605,7 @@ int mpdTagsAreEqual(MpdTag * tag1, MpdTag * tag2) ...@@ -605,7 +605,7 @@ int mpdTagsAreEqual(MpdTag * tag1, MpdTag * tag2)
static void appendToTagItems(MpdTag * tag, int type, char *value, int len) static void appendToTagItems(MpdTag * tag, int type, char *value, int len)
{ {
int i = tag->numOfItems; int i = tag->numOfItems;
char *dup = malloc(len + 1); char *dup = xmalloc(len + 1);
memcpy(dup, value, len); memcpy(dup, value, len);
dup[len] = '\0'; dup[len] = '\0';
...@@ -614,7 +614,7 @@ static void appendToTagItems(MpdTag * tag, int type, char *value, int len) ...@@ -614,7 +614,7 @@ static void appendToTagItems(MpdTag * tag, int type, char *value, int len)
stripReturnChar(dup); stripReturnChar(dup);
tag->numOfItems++; 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].type = type;
tag->items[i].value = getTagItemString(type, dup); tag->items[i].value = getTagItemString(type, dup);
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "tree.h" #include "tree.h"
#include "log.h" #include "log.h"
#include "utils.h"
#include "myfprintf.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -57,8 +59,8 @@ char *getTagItemString(int type, char *string) ...@@ -57,8 +59,8 @@ char *getTagItemString(int type, char *string)
} }
else else
{ {
TagTrackerItem *item = malloc(sizeof(TagTrackerItem)); TagTrackerItem *item = xmalloc(sizeof(TagTrackerItem));
char *key = strdup(string); char *key = xstrdup(string);
item->count = 1; item->count = 1;
item->visited = 0; item->visited = 0;
InsertInTree(tagTrees[type], key, item); InsertInTree(tagTrees[type], key, item);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
#include "tree.h" #include "tree.h"
#include "utils.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -59,7 +60,7 @@ static ...@@ -59,7 +60,7 @@ static
TreeNode * TreeNode *
_MakeNode(void) _MakeNode(void)
{ {
TreeNode * ret = malloc(sizeof(TreeNode)); TreeNode * ret = xmalloc(sizeof(TreeNode));
memset(ret, 0, sizeof(TreeNode)); memset(ret, 0, sizeof(TreeNode));
return ret; return ret;
} }
...@@ -571,7 +572,7 @@ MakeTree(TreeCompareKeyFunction compareKey, ...@@ -571,7 +572,7 @@ MakeTree(TreeCompareKeyFunction compareKey,
TreeFreeFunction freeKey, TreeFreeFunction freeKey,
TreeFreeFunction freeData) TreeFreeFunction freeData)
{ {
Tree * ret = malloc(sizeof(Tree)); Tree * ret = xmalloc(sizeof(Tree));
ret->compareKey = compareKey; ret->compareKey = compareKey;
ret->freeKey = freeKey; ret->freeKey = freeKey;
ret->freeData = freeData; ret->freeData = freeData;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
#include "utf8.h" #include "utf8.h"
#include "utils.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
...@@ -46,7 +47,7 @@ char *latin1StrToUtf8Dup(char *latin1) ...@@ -46,7 +47,7 @@ char *latin1StrToUtf8Dup(char *latin1)
{ {
/* utf8 should have at most two char's per latin1 char */ /* utf8 should have at most two char's per latin1 char */
int len = strlen(latin1) * 2 + 1; int len = strlen(latin1) * 2 + 1;
char *ret = malloc(len); char *ret = xmalloc(len);
char *cp = ret; char *cp = ret;
char *utf8; char *utf8;
...@@ -63,7 +64,7 @@ char *latin1StrToUtf8Dup(char *latin1) ...@@ -63,7 +64,7 @@ char *latin1StrToUtf8Dup(char *latin1)
latin1++; latin1++;
} }
return realloc(ret, len + 1); return xrealloc(ret, len + 1);
} }
static char utf8ToLatin1(char *inUtf8) static char utf8ToLatin1(char *inUtf8)
...@@ -124,7 +125,7 @@ char *utf8StrToLatin1Dup(char *utf8) ...@@ -124,7 +125,7 @@ char *utf8StrToLatin1Dup(char *utf8)
{ {
/* utf8 should have at most two char's per latin1 char */ /* utf8 should have at most two char's per latin1 char */
int len = strlen(utf8) + 1; int len = strlen(utf8) + 1;
char *ret = malloc(len); char *ret = xmalloc(len);
char *cp = ret; char *cp = ret;
int count; int count;
...@@ -143,5 +144,5 @@ char *utf8StrToLatin1Dup(char *utf8) ...@@ -143,5 +144,5 @@ char *utf8StrToLatin1Dup(char *utf8)
len++; len++;
} }
return realloc(ret, len + 1); return xrealloc(ret, len + 1);
} }
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
#include "utils.h" #include "utils.h"
#include "log.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
#include <sys/select.h> #include <sys/select.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
char *myFgets(char *buffer, int bufferSize, FILE * fp) char *myFgets(char *buffer, int bufferSize, FILE * fp)
{ {
...@@ -41,7 +43,7 @@ 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 *strDupToUpper(char *str)
{ {
char *ret = strdup(str); char *ret = xstrdup(str);
int i; int i;
for (i = 0; i < strlen(str); i++) for (i = 0; i < strlen(str); i++)
...@@ -86,12 +88,12 @@ char *appendToString(char *dest, const char *src) ...@@ -86,12 +88,12 @@ char *appendToString(char *dest, const char *src)
int srclen = strlen(src); int srclen = strlen(src);
if (dest == NULL) { if (dest == NULL) {
dest = malloc(srclen + 1); dest = xmalloc(srclen + 1);
memset(dest, 0, srclen + 1); memset(dest, 0, srclen + 1);
destlen = 0; destlen = 0;
} else { } else {
destlen = strlen(dest); destlen = strlen(dest);
dest = realloc(dest, destlen + srclen + 1); dest = xrealloc(dest, destlen + srclen + 1);
} }
memcpy(dest + destlen, src, srclen); memcpy(dest + destlen, src, srclen);
...@@ -106,3 +108,53 @@ unsigned long readLEuint32(const unsigned char *p) ...@@ -106,3 +108,53 @@ unsigned long readLEuint32(const unsigned char *p)
((unsigned long)p[1] << 8) | ((unsigned long)p[1] << 8) |
((unsigned long)p[2] << 16) | ((unsigned long)p[3] << 24); ((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;
}
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#define UTILS_H #define UTILS_H
#include "../config.h" #include "../config.h"
#include "gcc.h"
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -73,4 +74,12 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len) ...@@ -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 #endif
...@@ -112,7 +112,7 @@ static int prepOssMixer(char *device) ...@@ -112,7 +112,7 @@ static int prepOssMixer(char *device)
} }
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
dup = strdup(labels[i]); dup = xstrdup(labels[i]);
/* eliminate spaces at the end */ /* eliminate spaces at the end */
j = strlen(dup) - 1; j = strlen(dup) - 1;
while (j >= 0 && dup[j] == ' ') while (j >= 0 && dup[j] == ' ')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment