Changeset 6993


Ignore:
Timestamp:
Oct 30, 2008, 7:47:00 PM (12 years ago)
Author:
charles
Message:

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

Location:
trunk/libtransmission
Files:
2 edited

Legend:

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

    r6795 r6993  
    22#include <string.h> /* strcmp */
    33#include "transmission.h"
     4#include "platform.h"
    45#include "utils.h"
    56#include "crypto.h"
     
    119120}
    120121
     122static int
     123test_buildpath( void )
     124{
     125    char * out;
     126
     127    out = tr_buildPath( "foo", "bar", NULL );
     128    check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) );
     129    tr_free( out );
     130
     131    out = tr_buildPath( "", "foo", "bar", NULL );
     132    check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) );
     133    tr_free( out );
     134
     135    return 0;
     136}
     137
    121138int
    122139main( void )
     
    142159    if( ( i = test_strstrip( ) ) )
    143160        return i;
     161    if( ( i = test_buildpath( ) ) )
     162        return i;
    144163
    145164    /* test that tr_cryptoRandInt() stays in-bounds */
  • trunk/libtransmission/utils.c

    r6982 r6993  
    616616        element = (const char*) va_arg( vl, const char* );
    617617    }
    618     pch = buf = tr_new0( char, bufLen );
     618    pch = buf = tr_new( char, bufLen );
    619619    va_end( vl );
    620620
     
    624624    while( element ) {
    625625        const size_t elementLen = strlen( element );
    626         if( pch != buf )
    627             *pch++ = TR_PATH_DELIMITER;
    628626        memcpy( pch, element, elementLen );
    629627        pch += elementLen;
     628        *pch++ = TR_PATH_DELIMITER;
    630629        element = (const char*) va_arg( vl, const char* );
    631630    }
    632631    va_end( vl );
    633632
     633    /* terminate the string.  if nonempty, eat the unwanted trailing slash */
     634    if( pch != buf )
     635        --pch;
     636    *pch++ = '\0';
     637
    634638    /* sanity checks & return */
    635     *pch++ = '\0';
    636639    assert( pch - buf == (off_t)bufLen );
    637640    return buf;
Note: See TracChangeset for help on using the changeset viewer.