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
This commit is contained in:
Eric Wong
2006-08-26 06:25:57 +00:00
parent be554c2596
commit 90847fc881
48 changed files with 257 additions and 183 deletions
+3 -3
View File
@@ -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;
+3 -3
View File
@@ -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;
+1 -1
View File
@@ -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;
+5 -5
View File
@@ -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;
+2 -2
View File
@@ -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;
+3 -3
View File
@@ -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;
+1 -1
View File
@@ -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;