1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 01:57:52 +00:00

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

This commit is contained in:
Charles Kerr 2010-02-03 14:31:12 +00:00
parent d118db0f37
commit 168770f508
2 changed files with 18 additions and 20 deletions

View file

@ -10,7 +10,7 @@
#include "json.h" #include "json.h"
#include "utils.h" /* tr_free */ #include "utils.h" /* tr_free */
#undef VERBOSE /* #define VERBOSE */
static int test = 0; static int test = 0;
@ -170,7 +170,7 @@ testString( const char * str,
check( !err ); check( !err );
#if 0 #if 0
fprintf( stderr, "in: [%s]\n", str ); fprintf( stderr, "in: [%s]\n", str );
fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON( &val, NULL ) ); fprintf( stderr, "out:\n%s", tr_bencToStr( &val, TR_FMT_JSON, NULL ) );
#endif #endif
check( end == (const uint8_t*)str + len ); check( end == (const uint8_t*)str + len );
saved = tr_bencToStr( &val, TR_FMT_BENC, &savedLen ); saved = tr_bencToStr( &val, TR_FMT_BENC, &savedLen );

View file

@ -355,19 +355,12 @@ tr_bencLoad( const void * buf_in,
**** ****
***/ ***/
/* returns true if the given string length would fit in our string buffer */
static inline int
stringFitsInBuffer( const tr_benc * val, int len )
{
return len < (int)sizeof( val->val.s.str.buf );
}
/* returns true if the benc's string was malloced. /* returns true if the benc's string was malloced.
* this occurs when the string is too long for our string buffer */ * this occurs when the string is too long for our string buffer */
static inline int static inline int
stringIsAlloced( const tr_benc * val ) stringIsAlloced( const tr_benc * val )
{ {
return !stringFitsInBuffer( val, val->val.s.len ); return val->val.s.len >= sizeof( val->val.s.str.buf );
} }
/* returns a const pointer to the benc's string */ /* returns a const pointer to the benc's string */
@ -577,29 +570,34 @@ tr_bencDictFindRaw( tr_benc * dict,
void void
tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount ) tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
{ {
char * setme;
tr_bencInit( val, TR_TYPE_STR ); tr_bencInit( val, TR_TYPE_STR );
if( stringFitsInBuffer( val, val->val.s.len = byteCount )) /* There's no way in benc notation to distinguish between
memcpy( val->val.s.str.buf, src, byteCount ); * zero-terminated strings and raw byte arrays.
* Because of this, tr_bencMergeDicts() and tr_bencListCopy()
* don't know whether or not a TR_TYPE_STR node needs a '\0'.
* Append one, een to the raw arrays, just to be safe. */
if( byteCount < sizeof( val->val.s.str.buf ) )
setme = val->val.s.str.buf;
else else
val->val.s.str.ptr = tr_memdup( src, byteCount ); setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 );
memcpy( setme, src, byteCount );
setme[byteCount] = '\0';
val->val.s.len = byteCount;
} }
void void
tr_bencInitStr( tr_benc * val, const void * str, int len ) tr_bencInitStr( tr_benc * val, const void * str, int len )
{ {
tr_bencInit( val, TR_TYPE_STR );
if( str == NULL ) if( str == NULL )
len = 0; len = 0;
else if( len < 0 ) else if( len < 0 )
len = strlen( str ); len = strlen( str );
if( stringFitsInBuffer( val, val->val.s.len = len )) { tr_bencInitRaw( val, str, len );
memcpy( val->val.s.str.buf, str, len );
val->val.s.str.buf[len] = '\0';
} else
val->val.s.str.ptr = tr_strndup( str, len );
} }
void void