From 70289d9629c760690353fc04a8d558ec136cb581 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Fri, 1 Feb 2013 19:21:38 +0000 Subject: [PATCH] (libT) #5259 'Minor memory leaks in tr_torrentDeleteLocalData()': cleaner patch suggested by cfpp2p --- libtransmission/torrent.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 5b424dc3d..ee6da4927 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -2847,25 +2847,34 @@ deleteLocalData (tr_torrent * tor, tr_fileFunc func) /* build a list of 'top's child directories that belong to this torrent */ for (f=0; finfo.fileCount; ++f) { + char * dir; + char * filename; + /* get the directory that this file goes in... */ - char * filename = tr_buildPath (top, tor->info.files[f].name, NULL); - char * dir = tr_dirname (filename); + filename = tr_buildPath (top, tor->info.files[f].name, NULL); + dir = tr_dirname (filename); + tr_free (filename); + + /* walk up the directory tree until we reach 'top' */ if (!tr_is_same_file (top, dir) && strcmp (top, dir)) { - for (;;) { - char * parent = tr_dirname (dir); - const bool done_walking = tr_is_same_file (top, parent) || !strcmp (top, parent); - if (done_walking && tr_ptrArrayFindSorted (&folders, dir, vstrcmp)) - tr_ptrArrayInsertSorted (&folders, dir, vstrcmp); /* folders assumes ownership of dir */ - else - tr_free (dir); - dir = parent; - if (done_walking) + for (;;) { + char * parent = tr_dirname (dir); + if (tr_is_same_file (top, parent) || !strcmp (top, parent)) { + if (tr_ptrArrayFindSorted (&folders, dir, vstrcmp) == NULL) + tr_ptrArrayInsertSorted (&folders, tr_strdup(dir), vstrcmp); + tr_free (parent); break; + } + + /* walk upwards to parent */ + tr_free (dir); + dir = parent; } } - tr_free (dir); - tr_free (filename); + + tr_free (dir); } + for (i=0, n=tr_ptrArraySize (&folders); i