From cc038afd9d9cca3c7eb50b71d133da73fa39f8af Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Thu, 19 Jun 2008 17:00:25 +0200 Subject: piano: Using own free () wrapper now PS: "lib:" prefix is now "piano:"; I may add other libraries (last.fm support e.g.) --- libpiano/src/crypt.c | 9 +++-- libpiano/src/main.c | 111 ++++++++++++++++++++++++++++----------------------- libpiano/src/main.h | 1 + libpiano/src/xml.c | 5 ++- 4 files changed, 71 insertions(+), 55 deletions(-) (limited to 'libpiano') diff --git a/libpiano/src/crypt.c b/libpiano/src/crypt.c index 10744e0..c4871dc 100644 --- a/libpiano/src/crypt.c +++ b/libpiano/src/crypt.c @@ -26,6 +26,7 @@ THE SOFTWARE. #include "crypt_key_output.h" #include "crypt_key_input.h" +#include "main.h" /* hex string to array of unsigned int values * @param hex string @@ -133,8 +134,8 @@ char *PianoDecryptString (char *strInput) { PianoDecipherInts (cipherInts, cipherIntsN, &plainInts); strDecrypted = PianoIntsToString (plainInts, cipherIntsN); - free (cipherInts); - free (plainInts); + PianoFree (cipherInts, cipherIntsN * sizeof (*cipherInts)); + PianoFree (plainInts, cipherIntsN * sizeof (*plainInts)); return strDecrypted; } @@ -258,8 +259,8 @@ char *PianoEncryptString (char *strInput) { PianoEncipherInts (plainInts, plainIntsN, &cipherInts); strHex = PianoIntsToHexString (cipherInts, plainIntsN); - free (plainInts); - free (cipherInts); + PianoFree (plainInts, plainIntsN * sizeof (*plainInts)); + PianoFree (cipherInts, plainIntsN * sizeof (*cipherInts)); return strHex; } diff --git a/libpiano/src/main.c b/libpiano/src/main.c index 1d5a6d0..0659283 100644 --- a/libpiano/src/main.c +++ b/libpiano/src/main.c @@ -32,6 +32,23 @@ THE SOFTWARE. #include "xml.h" #include "crypt.h" +/* more "secure" free version; only use this function, not original free () + * in this library + * @public no!!! + * @param free this pointer + * @param zero n bytes; 0 disables zeroing (for strings with unknown size, + * e.g.) + */ +void PianoFree (void *ptr, size_t size) { + if (ptr != NULL) { + if (size > 0) { + /* avoid reuse of freed memory */ + memset ((char *) ptr, 0, size); + } + free (ptr); + } +} + /* initialize piano handle, set up curl handle and settings; note: you * _must_ init curl and libxml2 using curl_global_init (CURL_GLOBAL_SSL) * and xmlInitParser (), you also _must_ cleanup their garbage on your own! @@ -57,23 +74,21 @@ void PianoDestroySearchResult (PianoSearchResult_t *searchResult) { curArtist = searchResult->artists; while (curArtist != NULL) { - free (curArtist->name); - free (curArtist->musicId); + PianoFree (curArtist->name, 0); + PianoFree (curArtist->musicId, 0); lastArtist = curArtist; curArtist = curArtist->next; - memset (lastArtist, 0, sizeof (*lastArtist)); - free (lastArtist); + PianoFree (lastArtist, sizeof (*lastArtist)); } curSong = searchResult->songs; while (curSong != NULL) { - free (curSong->title); - free (curSong->artist); - free (curSong->musicId); + PianoFree (curSong->title, 0); + PianoFree (curSong->artist, 0); + PianoFree (curSong->musicId, 0); lastSong = curSong; curSong = curSong->next; - memset (lastSong, 0, sizeof (*lastSong)); - free (lastSong); + PianoFree (lastSong, sizeof (*lastSong)); } } @@ -82,8 +97,8 @@ void PianoDestroySearchResult (PianoSearchResult_t *searchResult) { * @param station */ void PianoDestroyStation (PianoStation_t *station) { - free (station->name); - free (station->id); + PianoFree (station->name, 0); + PianoFree (station->id, 0); memset (station, 0, sizeof (station)); } @@ -98,7 +113,7 @@ void PianoDestroyStations (PianoHandle_t *ph) { lastStation = curStation; curStation = curStation->next; PianoDestroyStation (lastStation); - free (lastStation); + PianoFree (lastStation, sizeof (*lastStation)); } ph->stations = NULL; } @@ -113,17 +128,16 @@ void PianoDestroyPlaylist (PianoHandle_t *ph) { curSong = ph->playlist; while (curSong != NULL) { - free (curSong->audioUrl); - free (curSong->artist); - free (curSong->focusTraitId); - free (curSong->matchingSeed); - free (curSong->musicId); - free (curSong->title); - free (curSong->userSeed); + PianoFree (curSong->audioUrl, 0); + PianoFree (curSong->artist, 0); + PianoFree (curSong->focusTraitId, 0); + PianoFree (curSong->matchingSeed, 0); + PianoFree (curSong->musicId, 0); + PianoFree (curSong->title, 0); + PianoFree (curSong->userSeed, 0); lastSong = curSong; curSong = curSong->next; - memset (lastSong, 0, sizeof (*lastSong)); - free (lastSong); + PianoFree (lastSong, sizeof (*lastSong)); } ph->playlist = NULL; } @@ -136,10 +150,9 @@ void PianoDestroyPlaylist (PianoHandle_t *ph) { */ void PianoDestroy (PianoHandle_t *ph) { curl_easy_cleanup (ph->curlHandle); - /* FIXME: only free if pointer != NULL */ - free (ph->user.webAuthToken); - free (ph->user.authToken); - free (ph->user.listenerId); + PianoFree (ph->user.webAuthToken, 0); + PianoFree (ph->user.authToken, 0); + PianoFree (ph->user.listenerId, 0); PianoDestroyStations (ph); PianoDestroyPlaylist (ph); @@ -163,8 +176,8 @@ PianoReturn_t PianoConnect (PianoHandle_t *ph, char *user, char *password) { snprintf (url, sizeof (url), PIANO_RPC_URL "rid=%s&method=sync", ph->routeId); PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); /* authenticate */ snprintf (requestStrPlain, sizeof (requestStrPlain), @@ -180,8 +193,8 @@ PianoReturn_t PianoConnect (PianoHandle_t *ph, char *user, char *password) { PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); ret = PianoXmlParseUserinfo (ph, retStr); - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } @@ -206,8 +219,8 @@ PianoReturn_t PianoGetStations (PianoHandle_t *ph) { ph->user.listenerId); PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); ret = PianoXmlParseStations (ph, retStr); - free (retStr); - free (requestStr); + PianoFree (retStr, 0); + PianoFree (requestStr, 0); return ret; } @@ -241,8 +254,8 @@ PianoReturn_t PianoGetPlaylist (PianoHandle_t *ph, char *stationId) { ph->user.listenerId, stationId); PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); ret = PianoXmlParsePlaylist (ph, retStr); - free (retStr); - free (requestStr); + PianoFree (retStr, 0); + PianoFree (requestStr, 0); return ret; } @@ -291,8 +304,8 @@ PianoReturn_t PianoRateTrack (PianoHandle_t *ph, PianoStation_t *station, song->rating = rating; } - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } @@ -329,14 +342,14 @@ PianoReturn_t PianoRenameStation (PianoHandle_t *ph, PianoStation_t *station, ret = PianoXmlParseSimple (retStr); if (ret == PIANO_RET_OK) { - free (station->name); + PianoFree (station->name, 0); station->name = strdup (newName); } curl_free (urlencodedNewName); - free (xmlencodedNewName); - free (requestStr); - free (retStr); + PianoFree (xmlencodedNewName, 0); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } @@ -379,15 +392,15 @@ PianoReturn_t PianoDeleteStation (PianoHandle_t *ph, PianoStation_t *station) { ph->stations = curStation->next; } PianoDestroyStation (curStation); - free (curStation); + PianoFree (curStation, sizeof (*curStation)); } lastStation = curStation; curStation = curStation->next; } } - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } @@ -425,9 +438,9 @@ PianoReturn_t PianoSearchMusic (PianoHandle_t *ph, char *searchStr, ret = PianoXmlParseSearch (retStr, searchResult); curl_free (urlencodedSearchStr); - free (xmlencodedSearchStr); - free (retStr); - free (requestStr); + PianoFree (xmlencodedSearchStr, 0); + PianoFree (retStr, 0); + PianoFree (requestStr, 0); return ret; } @@ -459,8 +472,8 @@ PianoReturn_t PianoCreateStation (PianoHandle_t *ph, char *musicId) { PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); ret = PianoXmlParseCreateStation (ph, retStr); - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } @@ -497,8 +510,8 @@ PianoReturn_t PianoStationAddMusic (PianoHandle_t *ph, PianoHttpPost (ph->curlHandle, url, requestStr, &retStr); ret = PianoXmlParseAddSeed (ph, retStr, station); - free (requestStr); - free (retStr); + PianoFree (requestStr, 0); + PianoFree (retStr, 0); return ret; } diff --git a/libpiano/src/main.h b/libpiano/src/main.h index 1746e53..50edec4 100644 --- a/libpiano/src/main.h +++ b/libpiano/src/main.h @@ -23,5 +23,6 @@ THE SOFTWARE. #ifndef _MAIN_H #define _MAIN_H +void PianoFree (void *ptr, size_t size); #endif /* _MAIN_H */ diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c index 2d9e82d..2613f40 100644 --- a/libpiano/src/xml.c +++ b/libpiano/src/xml.c @@ -29,6 +29,7 @@ THE SOFTWARE. #include "piano.h" #include "crypt.h" #include "config.h" +#include "main.h" void PianoXmlStructParser (xmlNode *structRoot, void (*callback) (char *, xmlNode *, void *), void *data); @@ -65,7 +66,7 @@ void PianoXmlIsFaultCb (char *key, xmlNode *value, void *data) { printf (PACKAGE ": Unknown error %s in %s\n", matchStr, valueStr); } - free (matchStr); + PianoFree (matchStr, 0); } } } @@ -228,7 +229,7 @@ void PianoXmlParsePlaylistCb (char *key, xmlNode *value, void *data) { /* FIXME: the key seems to be broken... so ignore 8 x 0x08 postfix; * urlTailN/2 because the encrypted hex string is now decoded */ strncat (song->audioUrl, urlTail, (urlTailN/2)-8); - free (urlTail); + PianoFree (urlTail, 0); } else if (strcmp ("artistSummary", key) == 0) { song->artist = strdup (valueStr); -- cgit v1.2.3