1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-04 10:38:13 +00:00

(libT) fix a tr_buildPath() bug reported by pea_

This commit is contained in:
Charles Kerr 2008-10-30 19:47:00 +00:00
parent e04ea4062d
commit e7461eb684
2 changed files with 26 additions and 4 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h> /* fprintf */ #include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include "transmission.h" #include "transmission.h"
#include "platform.h"
#include "utils.h" #include "utils.h"
#include "crypto.h" #include "crypto.h"
@ -118,6 +119,22 @@ test_strstrip( void )
return 0; return 0;
} }
static int
test_buildpath( void )
{
char * out;
out = tr_buildPath( "foo", "bar", NULL );
check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) );
tr_free( out );
out = tr_buildPath( "", "foo", "bar", NULL );
check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) );
tr_free( out );
return 0;
}
int int
main( void ) main( void )
{ {
@ -141,6 +158,8 @@ main( void )
if( ( i = test_strstrip( ) ) ) if( ( i = test_strstrip( ) ) )
return i; return i;
if( ( i = test_buildpath( ) ) )
return i;
/* test that tr_cryptoRandInt() stays in-bounds */ /* test that tr_cryptoRandInt() stays in-bounds */
for( i = 0; i < 100000; ++i ) for( i = 0; i < 100000; ++i )

View file

@ -615,7 +615,7 @@ tr_buildPath( const char *first_element, ... )
bufLen += strlen( element ) + 1; bufLen += strlen( element ) + 1;
element = (const char*) va_arg( vl, const char* ); element = (const char*) va_arg( vl, const char* );
} }
pch = buf = tr_new0( char, bufLen ); pch = buf = tr_new( char, bufLen );
va_end( vl ); va_end( vl );
/* pass 2: build the string piece by piece */ /* pass 2: build the string piece by piece */
@ -623,16 +623,19 @@ tr_buildPath( const char *first_element, ... )
element = first_element; element = first_element;
while( element ) { while( element ) {
const size_t elementLen = strlen( element ); const size_t elementLen = strlen( element );
if( pch != buf )
*pch++ = TR_PATH_DELIMITER;
memcpy( pch, element, elementLen ); memcpy( pch, element, elementLen );
pch += elementLen; pch += elementLen;
*pch++ = TR_PATH_DELIMITER;
element = (const char*) va_arg( vl, const char* ); element = (const char*) va_arg( vl, const char* );
} }
va_end( vl ); va_end( vl );
/* sanity checks & return */ /* terminate the string. if nonempty, eat the unwanted trailing slash */
if( pch != buf )
--pch;
*pch++ = '\0'; *pch++ = '\0';
/* sanity checks & return */
assert( pch - buf == (off_t)bufLen ); assert( pch - buf == (off_t)bufLen );
return buf; return buf;
} }