Changeset f173b63669bf1fa0b58b00a88763d999fffcb115

Show
Ignore:
Timestamp:
09/18/07 01:15:37 (1 year ago)
Author:
Rafaël Carré <funman@videolan.org>
git-committer:
Rafaël Carré <funman@videolan.org> 1190070937 +0000
git-parent:

[034015dae895274013ad2e472188b952a1343323]

git-author:
Rafaël Carré <funman@videolan.org> 1190070937 +0000
Message:

str_format_meta(): fix HUGE memory leak & segfault.
realloc() can change the pointer initialised with malloc() if memory is low !

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/text/strings.c

    rd001ebd rf173b63  
    635635} 
    636636 
    637 #define INSERT_STRING( check, string )                              \ 
    638                     if( check )                                     \ 
     637#define INSERT_STRING( string )                                     \ 
     638                    if( string != NULL )                            \ 
    639639                    {                                               \ 
    640                         psz_meta = string;                          \ 
    641                         if( psz_meta != NULL )                      \ 
    642                         {                                           \ 
    643                             int len = strlen( string );             \ 
    644                             dst = realloc( dst,                     \ 
    645                                    i_size = i_size + len + 1 );     \ 
    646                             strncpy( d, psz_meta, len+1 );          \ 
    647                             d += len;                               \ 
    648                         }                                           \ 
    649                         else                                        \ 
    650                         {                                           \ 
    651                             *d = '-';                               \ 
    652                             d++;                                    \ 
    653                         }                                           \ 
    654                     } 
     640                        int len = strlen( string );                 \ 
     641                        dst = realloc( dst, i_size = i_size + len );\ 
     642                        memcpy( (dst+d), string, len );             \ 
     643                        d += len;                                   \ 
     644                        free( string );                             \ 
     645                    }                                               \ 
     646                    else                                            \ 
     647                    {                                               \ 
     648                        *(dst+d) = '-';                             \ 
     649                        d++;                                        \ 
     650                    }                                               \ 
    655651 
    656652/* same than INSERT_STRING, except that string won't be freed */ 
    657653#define INSERT_STRING_NO_FREE( string )                             \ 
    658654                    {                                               \ 
    659                             int len = strlen( string );             \ 
    660                             dst = realloc( dst,                     \ 
    661                                    i_size = i_size + len + 1 );     \ 
    662                             strncpy( d, string, len+1 );            \ 
    663                             d += len;                               \ 
    664                             free( string );                         \ 
    665                     }                                                
     655                        int len = strlen( string );                 \ 
     656                        dst = realloc( dst, i_size = i_size + len );\ 
     657                        memcpy( dst+d, string, len );               \ 
     658                        d += len;                                   \ 
     659                    } 
    666660char *__str_format_meta( vlc_object_t *p_object, const char *string ) 
    667661{ 
    668662    const char *s = string; 
    669     char *dst = malloc( 1000 ); 
    670     char *d = dst; 
    671663    int b_is_format = 0; 
    672664    int b_empty_if_na = 0; 
    673665    char buf[10]; 
    674     int i_size = strlen( string ); 
     666    int i_size = strlen( string ) + 1; /* +1 to store '\0' */ 
     667    char *dst = malloc( i_size ); 
     668    int d = 0; 
    675669 
    676670    playlist_t *p_playlist = pl_Yield( p_object ); 
     
    692686            switch( *s ) 
    693687            { 
    694                 char *psz_meta; /* used by INSERT_STRING */ 
    695688                case 'a': 
    696                     INSERT_STRING( p_item, input_item_GetArtist(p_item) ); 
     689                    if( p_item ) 
     690                    { 
     691                        INSERT_STRING( input_item_GetArtist( p_item ) ); 
     692                    } 
    697693                    break; 
    698694                case 'b': 
    699                     INSERT_STRING( p_item, input_item_GetAlbum(p_item) ); 
     695                    if( p_item ) 
     696                    { 
     697                        INSERT_STRING( input_item_GetAlbum( p_item ) ); 
     698                    } 
    700699                    break; 
    701700                case 'c': 
    702                     INSERT_STRING( p_item, input_item_GetCopyright(p_item) ); 
     701                    if( p_item ) 
     702                    { 
     703                        INSERT_STRING( input_item_GetCopyright( p_item ) ); 
     704                    } 
    703705                    break; 
    704706                case 'd': 
    705                     INSERT_STRING( p_item, input_item_GetDescription(p_item) ); 
     707                    if( p_item ) 
     708                    { 
     709                        INSERT_STRING( input_item_GetDescription( p_item ) ); 
     710                    } 
    706711                    break; 
    707712                case 'e': 
    708                     INSERT_STRING( p_item, input_item_GetEncodedBy(p_item) ); 
     713                    if( p_item ) 
     714                    { 
     715                        INSERT_STRING( input_item_GetEncodedBy( p_item ) ); 
     716                    } 
    709717                    break; 
    710718                case 'g': 
    711                     INSERT_STRING( p_item, input_item_GetGenre(p_item) ); 
     719                    if( p_item ) 
     720                    { 
     721                        INSERT_STRING( input_item_GetGenre( p_item ) ); 
     722                    } 
    712723                    break; 
    713724                case 'l': 
    714                     INSERT_STRING( p_item, input_item_GetLanguage(p_item) ); 
     725                    if( p_item ) 
     726                    { 
     727                        INSERT_STRING( input_item_GetLanguage( p_item ) ); 
     728                    } 
    715729                    break; 
    716730                case 'n': 
    717                     INSERT_STRING( p_item, input_item_GetTrackNum(p_item) ); 
     731                    if( p_item ) 
     732                    { 
     733                        INSERT_STRING( input_item_GetTrackNum( p_item ) ); 
     734                    } 
    718735                    break; 
    719736                case 'p': 
    720                     INSERT_STRING( p_item, input_item_GetNowPlaying(p_item) ); 
     737                    if( p_item ) 
     738                    { 
     739                        INSERT_STRING( input_item_GetNowPlaying( p_item ) ); 
     740                    } 
    721741                    break; 
    722742                case 'r': 
    723                     INSERT_STRING( p_item, input_item_GetRating(p_item) ); 
     743                    if( p_item ) 
     744                    { 
     745                        INSERT_STRING( input_item_GetRating( p_item ) ); 
     746                    } 
    724747                    break; 
    725748                case 's': 
     
    730753                    if( lang == NULL ) 
    731754                        lang = strdup( b_empty_if_na ? "" : "-" ); 
    732                     INSERT_STRING( 1, lang ); 
     755                    INSERT_STRING( lang ); 
    733756                    break; 
    734757                } 
    735758                case 't': 
    736                     INSERT_STRING( p_item, input_item_GetTitle(p_item) ); 
     759                    if( p_item ) 
     760                    { 
     761                        INSERT_STRING( input_item_GetTitle( p_item ) ); 
     762                    } 
    737763                    break; 
    738764                case 'u': 
    739                     INSERT_STRING( p_item, input_item_GetURL(p_item) ); 
     765                    if( p_item ) 
     766                    { 
     767                        INSERT_STRING( input_item_GetURL( p_item ) ); 
     768                    } 
    740769                    break; 
    741770                case 'A': 
    742                     INSERT_STRING( p_item, input_item_GetDate(p_item) ); 
     771                    if( p_item ) 
     772                    { 
     773                        INSERT_STRING( input_item_GetDate( p_item ) ); 
     774                    } 
    743775                    break; 
    744776                case 'B': 
     
    782814                    break; 
    783815                case 'F': 
    784                     INSERT_STRING( p_item, input_item_GetURI( p_item ) ); 
     816                    if( p_item ) 
     817                    { 
     818                        INSERT_STRING( input_item_GetURI( p_item ) ); 
     819                    } 
    785820                    break; 
    786821                case 'I': 
     
    813848                    break; 
    814849                case 'N': 
    815                     INSERT_STRING( p_item, input_item_GetName( p_item ) ); 
     850                    if( p_item ) 
     851                    { 
     852                        INSERT_STRING( input_item_GetName( p_item ) ); 
     853                    } 
    816854                    break; 
    817855                case 'O': 
     
    823861                    if( lang == NULL ) 
    824862                        lang = strdup( b_empty_if_na ? "" : "-" ); 
    825                     INSERT_STRING( 1, lang ); 
     863                    INSERT_STRING( lang ); 
    826864                    break; 
    827865                } 
     
    877915                    break; 
    878916                case 'U': 
    879                     INSERT_STRING( p_item, input_item_GetPublisher(p_item) ); 
     917                    if( p_item ) 
     918                    { 
     919                        INSERT_STRING( input_item_GetPublisher( p_item ) ); 
     920                    } 
    880921                    break; 
    881922                case 'V': 
     
    888929                } 
    889930                case '_': 
    890                     *d = '\n'; 
     931                    *(dst+d) = '\n'; 
    891932                    d++; 
    892933                    break; 
     
    897938 
    898939                default: 
    899                     *d = *s; 
     940                    *(dst+d) = *s; 
    900941                    d++; 
    901942                    break; 
     
    911952        else 
    912953        { 
    913             *d = *s; 
     954            *(dst+d) = *s; 
    914955            d++; 
    915956        } 
    916957        s++; 
    917958    } 
    918     *d = '\0'; 
     959    *(dst+d) = '\0'; 
    919960 
    920961    if( p_input )