Changeset 10091


Ignore:
Timestamp:
Feb 3, 2010, 2:31:12 PM (12 years ago)
Author:
charles
Message:

(trunk libT) #2716 "magnet torrents not being saved after quit" -- fix bencode bug.

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode-test.c

    r8889 r10091  
    1111#include "utils.h" /* tr_free */
    1212
    13 #undef VERBOSE
     13/* #define VERBOSE */
    1414
    1515static int test = 0;
     
    171171#if 0
    172172        fprintf( stderr, "in: [%s]\n", str );
    173         fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON( &val, NULL ) );
     173        fprintf( stderr, "out:\n%s", tr_bencToStr( &val, TR_FMT_JSON, NULL ) );
    174174#endif
    175175        check( end == (const uint8_t*)str + len );
  • trunk/libtransmission/bencode.c

    r10084 r10091  
    356356***/
    357357
    358 /* returns true if the given string length would fit in our string buffer */
    359 static inline int
    360 stringFitsInBuffer( const tr_benc * val, int len )
    361 {
    362     return len < (int)sizeof( val->val.s.str.buf );
    363 }
    364 
    365358/* returns true if the benc's string was malloced.
    366359 * this occurs when the string is too long for our string buffer */
     
    368361stringIsAlloced( const tr_benc * val )
    369362{
    370     return !stringFitsInBuffer( val, val->val.s.len );
     363    return val->val.s.len >= sizeof( val->val.s.str.buf );
    371364}
    372365
     
    578571tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
    579572{
     573    char * setme;
    580574    tr_bencInit( val, TR_TYPE_STR );
    581575
    582     if( stringFitsInBuffer( val, val->val.s.len = byteCount ))
    583         memcpy( val->val.s.str.buf, src, byteCount );
     576    /* There's no way in benc notation to distinguish between
     577     * zero-terminated strings and raw byte arrays.
     578     * Because of this, tr_bencMergeDicts() and tr_bencListCopy()
     579     * don't know whether or not a TR_TYPE_STR node needs a '\0'.
     580     * Append one, een to the raw arrays, just to be safe. */
     581
     582    if( byteCount < sizeof( val->val.s.str.buf ) )
     583        setme = val->val.s.str.buf;
    584584    else
    585         val->val.s.str.ptr = tr_memdup( src, byteCount );
     585        setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 );
     586
     587    memcpy( setme, src, byteCount );
     588    setme[byteCount] = '\0';
     589    val->val.s.len = byteCount;
    586590}
    587591
     
    589593tr_bencInitStr( tr_benc * val, const void * str, int len )
    590594{
    591     tr_bencInit( val, TR_TYPE_STR );
    592 
    593595    if( str == NULL )
    594596        len = 0;
     
    596598        len = strlen( str );
    597599
    598     if( stringFitsInBuffer( val, val->val.s.len = len )) {
    599         memcpy( val->val.s.str.buf, str, len );
    600         val->val.s.str.buf[len] = '\0';
    601     } else
    602         val->val.s.str.ptr = tr_strndup( str, len );
     600    tr_bencInitRaw( val, str, len );
    603601}
    604602
Note: See TracChangeset for help on using the changeset viewer.