From 0cd2322d48c8889e47a0d5ee635503c19cb6fb2b Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 7 Aug 2009 12:26:31 +0200 Subject: piano: Check (m|c|re)alloc return value --- libpiano/src/xml.c | 143 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 41 deletions(-) (limited to 'libpiano/src/xml.c') diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c index 463caa7..2cc519f 100644 --- a/libpiano/src/xml.c +++ b/libpiano/src/xml.c @@ -45,7 +45,7 @@ static void PianoXmlIsFaultCb (const char *key, const ezxml_t value, void *data) { PianoReturn_t *ret = data; char *valueStr = PianoXmlGetNodeText (value); - char *matchStart, *matchEnd, *matchStr; + char *matchStart, *matchEnd; if (strcmp ("faultString", key) == 0) { *ret = PIANO_RET_ERR; @@ -56,36 +56,34 @@ static void PianoXmlIsFaultCb (const char *key, const ezxml_t value, if ((matchStart = strchr (valueStr, '|')) != NULL) { if ((matchStart = strchr (matchStart+1, '|')) != NULL) { if ((matchEnd = strchr (matchStart+1, '|')) != NULL) { - matchStr = calloc (matchEnd - (matchStart+1)+1, - sizeof (*matchStr)); - memcpy (matchStr, matchStart+1, matchEnd - - (matchStart+1)); + /* changes text in xml node, but we don't care... */ + *matchEnd = '\0'; + ++matchStart; /* translate to our error message system */ - if (strcmp ("AUTH_INVALID_TOKEN", matchStr) == 0) { + if (strcmp ("AUTH_INVALID_TOKEN", matchStart) == 0) { *ret = PIANO_RET_AUTH_TOKEN_INVALID; } else if (strcmp ("AUTH_INVALID_USERNAME_PASSWORD", - matchStr) == 0) { + matchStart) == 0) { *ret = PIANO_RET_AUTH_USER_PASSWORD_INVALID; } else if (strcmp ("LISTENER_NOT_AUTHORIZED", - matchStr) == 0) { + matchStart) == 0) { *ret = PIANO_RET_NOT_AUTHORIZED; } else if (strcmp ("INCOMPATIBLE_VERSION", - matchStr) == 0) { + matchStart) == 0) { *ret = PIANO_RET_PROTOCOL_INCOMPATIBLE; - } else if (strcmp ("READONLY_MODE", matchStr) == 0) { + } else if (strcmp ("READONLY_MODE", matchStart) == 0) { *ret = PIANO_RET_READONLY_MODE; } else if (strcmp ("STATION_CODE_INVALID", - matchStr) == 0) { + matchStart) == 0) { *ret = PIANO_RET_STATION_CODE_INVALID; } else if (strcmp ("STATION_DOES_NOT_EXIST", - matchStr) == 0) { + matchStart) == 0) { *ret = PIANO_RET_STATION_NONEXISTENT; } else { *ret = PIANO_RET_ERR; printf (PACKAGE ": Unknown error %s in %s\n", - matchStr, valueStr); + matchStart, valueStr); } - PianoFree (matchStr, 0); } } } @@ -232,18 +230,24 @@ static void PianoXmlParsePlaylistCb (const char *key, const ezxml_t value, if (strcmp ("audioURL", key) == 0) { /* last 48 chars of audioUrl are encrypted, but they put the key - * into the door's lock; dumb pandora... */ + * into the door's lock... */ const char urlTailN = 48; const size_t valueStrN = strlen (valueStr); char *urlTail = NULL, *urlTailCrypted = &valueStr[valueStrN - urlTailN]; - urlTail = PianoDecryptString (urlTailCrypted); - song->audioUrl = calloc (valueStrN + 1, sizeof (*song->audioUrl)); - memcpy (song->audioUrl, valueStr, valueStrN - urlTailN); - /* FIXME: the key seems to be broken... so ignore 8 x 0x08 postfix; - * urlTailN/2 because the encrypted hex string is now decoded */ - memcpy (&song->audioUrl[valueStrN - urlTailN], urlTail, urlTailN/2 - 8); - PianoFree (urlTail, urlTailN/2); + + if ((urlTail = PianoDecryptString (urlTailCrypted)) != NULL) { + if ((song->audioUrl = calloc (valueStrN + 1, + sizeof (*song->audioUrl))) != NULL) { + memcpy (song->audioUrl, valueStr, valueStrN - urlTailN); + /* FIXME: the key seems to be broken... so ignore 8 x 0x08 + * postfix; urlTailN/2 because the encrypted hex string is now + * decoded */ + memcpy (&song->audioUrl[valueStrN - urlTailN], urlTail, + urlTailN/2 - 8); + } + PianoFree (urlTail, urlTailN/2); + } } else if (strcmp ("artistSummary", key) == 0) { song->artist = strdup (valueStr); } else if (strcmp ("musicId", key) == 0) { @@ -315,18 +319,32 @@ static void PianoXmlParseQuickMixStationsCb (const char *key, const ezxml_t valu curNode; curNode = curNode->next) { idsN++; if (ids == NULL) { - ids = calloc (idsN, sizeof (*ids)); + if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } } else { - ids = realloc (ids, idsN * sizeof (*ids)); + /* FIXME: memory leak (on failure) */ + if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } } ids[idsN-1] = strdup (PianoXmlGetNodeText (curNode)); } /* append NULL: list ends here */ idsN++; + /* FIXME: copy&waste */ if (ids == NULL) { - ids = calloc (idsN, sizeof (*ids)); + if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } } else { - ids = realloc (ids, idsN * sizeof (*ids)); + if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } } ids[idsN-1] = NULL; } @@ -352,9 +370,16 @@ PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, char *xml) { for (dataNode = ezxml_child (dataNode, "value"); dataNode; dataNode = dataNode->next) { - PianoStation_t *tmpStation = calloc (1, sizeof (*tmpStation)); + PianoStation_t *tmpStation; + + if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + PianoXmlStructParser (ezxml_child (dataNode, "struct"), PianoXmlParseStationsCb, tmpStation); + /* get stations selected for quickmix */ if (tmpStation->isQuickMix) { PianoXmlStructParser (ezxml_child (dataNode, "struct"), @@ -406,7 +431,11 @@ PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, char *xml) { } dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - tmpStation = calloc (1, sizeof (*tmpStation)); + + if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, tmpStation); /* FIXME: copy & waste */ /* start new linked list or append */ @@ -465,7 +494,13 @@ PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml) { for (dataNode = ezxml_child (dataNode, "value"); dataNode; dataNode = dataNode->next) { - PianoSong_t *tmpSong = calloc (1, sizeof (*tmpSong)); + PianoSong_t *tmpSong; + + if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + PianoXmlStructParser (ezxml_child (dataNode, "struct"), PianoXmlParsePlaylistCb, tmpSong); /* begin linked list or append */ @@ -538,7 +573,13 @@ static void PianoXmlParseSearchCb (const char *key, const ezxml_t value, /* skip */ for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); curNode; curNode = curNode->next) { - PianoArtist_t *artist = calloc (1, sizeof (*artist)); + PianoArtist_t *artist; + + if ((artist = calloc (1, sizeof (*artist))) == NULL) { + /* fail silently */ + break; + } + memset (artist, 0, sizeof (*artist)); PianoXmlStructParser (ezxml_child (curNode, "struct"), @@ -559,7 +600,13 @@ static void PianoXmlParseSearchCb (const char *key, const ezxml_t value, for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); curNode; curNode = curNode->next) { /* FIXME: copy & waste */ - PianoSong_t *tmpSong = calloc (1, sizeof (*tmpSong)); + PianoSong_t *tmpSong; + + if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { + /* fail silently */ + break; + } + PianoXmlStructParser (ezxml_child (curNode, "struct"), PianoXmlParsePlaylistCb, tmpSong); /* begin linked list or append */ @@ -603,15 +650,18 @@ PianoReturn_t PianoXmlParseSearch (char *xml, /* encode reserved xml chars * TODO: remove and use ezxml_ampencode * @param encode this - * @return encoded string + * @return encoded string or NULL */ char *PianoXmlEncodeString (const char *s) { char *replacements[] = {"&&", "''", "\""", "<<", ">>", NULL}; - char **r; - char *sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut)), - *sOutCurr = sOut; - char found; + char **r, *sOut, *sOutCurr, found; + + if ((sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut))) == NULL) { + return NULL; + } + + sOutCurr = sOut; while (*s != '\0') { r = replacements; @@ -635,10 +685,8 @@ char *PianoXmlEncodeString (const char *s) { } PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, catNode, genreNode; + ezxml_t xmlDoc, catNode; PianoReturn_t ret; - PianoGenreCategory_t *tmpGenreCategory; - PianoStation_t *tmpStation; if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { return ret; @@ -647,13 +695,26 @@ PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, char *xml) { /* get all nodes */ for (catNode = ezxml_child (xmlDoc, "category"); catNode; catNode = catNode->next) { - tmpGenreCategory = calloc (1, sizeof (*tmpGenreCategory)); + PianoGenreCategory_t *tmpGenreCategory; + ezxml_t genreNode; + + if ((tmpGenreCategory = calloc (1, sizeof (*tmpGenreCategory))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + tmpGenreCategory->name = strdup (ezxml_attr (catNode, "categoryName")); /* get genre subnodes */ for (genreNode = ezxml_child (catNode, "genre"); genreNode; genreNode = genreNode->next) { - tmpStation = calloc (1, sizeof (*tmpStation)); + PianoStation_t *tmpStation; + + if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + /* get genre attributes */ tmpStation->name = strdup (ezxml_attr (genreNode, "name")); tmpStation->id = strdup (ezxml_attr (genreNode, "stationId")); -- cgit v1.2.3