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/tag.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/tag.c') 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); -- cgit v1.2.3