]> granicus.if.org Git - transmission/commitdiff
allocate torrent/resume filenames on the heap instead of the stack
authorCharles Kerr <charles@transmissionbt.com>
Wed, 20 Aug 2008 19:00:52 +0000 (19:00 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Wed, 20 Aug 2008 19:00:52 +0000 (19:00 +0000)
libtransmission/metainfo.c
libtransmission/resume.c

index 77377129ae304d3b9fa5fddf449d09fa275fdc34..71c0f3281385d34634ac6d524a1e5cc805a00b64 100644 (file)
@@ -123,36 +123,32 @@ strlcat_utf8( void * dest, const void * src, size_t len, char skip )
     }
 }
 
-static void
+static char*
 getTorrentFilename( const tr_handle  * handle,
-                    const tr_info    * inf,
-                    char             * buf,
-                    size_t             buflen )
+                    const tr_info    * inf )
 {
-    const char * dir = tr_getTorrentDir( handle );
-    char base[MAX_PATH_LENGTH];
-    tr_snprintf( base, sizeof( base ), "%s.%16.16s.torrent", inf->name, inf->hashString );
-    tr_buildPath( buf, buflen, dir, base, NULL );
+    return tr_strdup_printf( "%s%c%s.%16.16s.torrent",
+                             tr_getTorrentDir( handle ),
+                             TR_PATH_DELIMITER,
+                             inf->name,
+                             inf->hashString );
 }
 
-static void
+static char*
 getTorrentOldFilename( const tr_handle * handle,
-                       const tr_info   * info,
-                       char            * name,
-                       size_t            len )
+                       const tr_info   * inf )
 {
-    const char * torDir = tr_getTorrentDir( handle );
-
-    if( !handle->tag )
-    {
-        tr_buildPath( name, len, torDir, info->hashString, NULL );
-    }
-    else
-    {
-        char base[1024];
-        tr_snprintf( base, sizeof(base), "%s-%s", info->hashString, handle->tag );
-        tr_buildPath( name, len, torDir, base, NULL );
-    }
+    char * ret;
+    struct evbuffer * buf = evbuffer_new( );
+    evbuffer_add_printf( buf, "%s%c%s", 
+                         tr_getTorrentDir( handle ),
+                         TR_PATH_DELIMITER,
+                         inf->hashString );
+    if( handle->tag )
+        evbuffer_add_printf( buf, "-%s", handle->tag );
+    ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
+    evbuffer_free( buf );
+    return ret;
 }
 
 void
@@ -160,18 +156,15 @@ tr_metainfoMigrate( tr_handle * handle,
                     tr_info   * inf )
 {
     struct stat new_sb;
-    char new_name[MAX_PATH_LENGTH];
-
-    getTorrentFilename( handle, inf, new_name, sizeof( new_name ) );
+    char * new_name = getTorrentFilename( handle, inf );
 
     if( stat( new_name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
     {
-        char old_name[MAX_PATH_LENGTH];
+        char * old_name = getTorrentOldFilename( handle, inf );
         size_t contentLen;
         uint8_t * content;
 
         tr_mkdirp( tr_getTorrentDir( handle ), 0777 );
-        getTorrentOldFilename( handle, inf, old_name, sizeof( old_name ) );
         if(( content = tr_loadFile( old_name, &contentLen )))
         {
             FILE * out;
@@ -195,7 +188,10 @@ tr_metainfoMigrate( tr_handle * handle,
         }
 
         tr_free( content );
+        tr_free( old_name );
     }
+
+    tr_free( new_name );
 }
 
 static char *
@@ -434,9 +430,8 @@ tr_metainfoParse( const tr_handle  * handle,
     geturllist( inf, meta );
 
     /* filename of Transmission's copy */
-    getTorrentFilename( handle, inf, buf, sizeof( buf ) );
     tr_free( inf->torrent );
-    inf->torrent = tr_strdup( buf );
+    inf->torrent = getTorrentFilename( handle, inf );
 
     return TR_OK;
 
@@ -533,13 +528,15 @@ void
 tr_metainfoRemoveSaved( const tr_handle * handle,
                         const tr_info   * inf )
 {
-    char filename[MAX_PATH_LENGTH];
+    char * filename;
 
-    getTorrentFilename( handle, inf, filename, sizeof( filename ) );
+    filename = getTorrentFilename( handle, inf );
     unlink( filename );
+    tr_free( filename );
 
-    getTorrentOldFilename( handle, inf, filename, sizeof( filename ) );
+    filename = getTorrentOldFilename( handle, inf );
     unlink( filename );
+    tr_free( filename );
 }
 
 static int
index 510f85b545d8db2164c5596bf971010aed236789..71ee8b1d1786765a68f2d58cec468c9d9fae718a 100644 (file)
 #define KEY_PROGRESS_MTIMES   "mtimes"
 #define KEY_PROGRESS_BITFIELD "bitfield"
 
-static void
-getResumeFilename( char * buf, size_t buflen, const tr_torrent * tor )
+static char*
+getResumeFilename( const tr_torrent * tor )
 {
-    const char * dir = tr_getResumeDir( tor->handle );
-    char base[MAX_PATH_LENGTH];
-    tr_snprintf( base, sizeof( base ), "%s.%16.16s.resume",
-                 tor->info.name,
-                 tor->info.hashString );
-    tr_buildPath( buf, buflen, dir, base, NULL );
+    return tr_strdup_printf( "%s%c%s.%16.16s.resume",
+                             tr_getResumeDir( tor->handle ),
+                             TR_PATH_DELIMITER,
+                             tor->info.name,
+                             tor->info.hashString );
 }
 
 /***
@@ -348,7 +347,7 @@ void
 tr_torrentSaveResume( const tr_torrent * tor )
 {
     tr_benc top;
-    char filename[MAX_PATH_LENGTH];
+    char * filename;
 
     if( !tor )
         return;
@@ -378,8 +377,9 @@ tr_torrentSaveResume( const tr_torrent * tor )
     saveProgress( &top, tor );
     saveSpeedLimits( &top, tor );
 
-    getResumeFilename( filename, sizeof( filename ), tor );
+    filename = getResumeFilename( tor );
     tr_bencSaveFile( filename, &top );
+    tr_free( filename );
 
     tr_bencFree( &top );
 }
@@ -391,10 +391,10 @@ loadFromFile( tr_torrent    * tor,
     int64_t i;
     const char * str;
     uint64_t fieldsLoaded = 0;
-    char filename[MAX_PATH_LENGTH];
+    char * filename;
     tr_benc top;
 
-    getResumeFilename( filename, sizeof( filename ), tor );
+    filename = getResumeFilename( tor );
 
     if( tr_bencLoadFile( filename, &top ) )
     {
@@ -408,6 +408,7 @@ loadFromFile( tr_torrent    * tor,
             tr_tordbg( tor, "Migrated resume file to \"%s\"", filename );
         }
 
+        tr_free( filename );
         return fieldsLoaded;
     }
 
@@ -484,6 +485,7 @@ loadFromFile( tr_torrent    * tor,
         fieldsLoaded |= loadSpeedLimits( &top, tor );
 
     tr_bencFree( &top );
+    tr_free( filename );
     return fieldsLoaded;
 }
 
@@ -547,8 +549,8 @@ tr_torrentLoadResume( tr_torrent    * tor,
 void
 tr_torrentRemoveResume( const tr_torrent * tor )
 {
-    char filename[MAX_PATH_LENGTH];
-    getResumeFilename( filename, sizeof( filename ), tor );
+    char * filename = getResumeFilename( tor );
     unlink( filename );
     tr_fastResumeRemove( tor );
+    tr_free( filename );
 }