fix memory leaks found with valgrind
authorJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 4 Dec 2002 07:01:37 +0000 (07:01 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 4 Dec 2002 07:01:37 +0000 (07:01 +0000)
src/flac/main.c
src/libFLAC/metadata_object.c
src/libFLAC/stream_decoder.c
src/libFLAC/stream_encoder.c
src/metaflac/options.c
src/test_libFLAC/bitbuffer.c
src/test_libFLAC/metadata_manip.c

index 3f9b512..3106fd6 100644 (file)
@@ -980,8 +980,14 @@ int parse_option(int short_option, const char *long_option, const char *option_a
 
 void free_options()
 {
-       if(0 != option_values.filenames)
+       unsigned i;
+       if(0 != option_values.filenames) {
+               for(i = 0; i < option_values.num_files; i++) {
+                       if(0 != option_values.filenames[i])
+                               free(option_values.filenames[i]);
+               }
                free(option_values.filenames);
+       }
        if(0 != option_values.vorbis_comment)
                FLAC__metadata_object_delete(option_values.vorbis_comment);
 }
index 3e3d33e..439902c 100644 (file)
@@ -392,8 +392,10 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMet
                                }
                                break;
                        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
-                               if(0 != to->data.vorbis_comment.vendor_string.entry)
+                               if(0 != to->data.vorbis_comment.vendor_string.entry) {
                                        free(to->data.vorbis_comment.vendor_string.entry);
+                                       to->data.vorbis_comment.vendor_string.entry = 0;
+                               }
                                if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
                                        FLAC__metadata_object_delete(to);
                                        return 0;
@@ -446,16 +448,22 @@ void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
                case FLAC__METADATA_TYPE_PADDING:
                        break;
                case FLAC__METADATA_TYPE_APPLICATION:
-                       if(0 != object->data.application.data)
+                       if(0 != object->data.application.data) {
                                free(object->data.application.data);
+                               object->data.application.data = 0;
+                       }
                        break;
                case FLAC__METADATA_TYPE_SEEKTABLE:
-                       if(0 != object->data.seek_table.points)
+                       if(0 != object->data.seek_table.points) {
                                free(object->data.seek_table.points);
+                               object->data.seek_table.points = 0;
+                       }
                        break;
                case FLAC__METADATA_TYPE_VORBIS_COMMENT:
-                       if(0 != object->data.vorbis_comment.vendor_string.entry)
+                       if(0 != object->data.vorbis_comment.vendor_string.entry) {
                                free(object->data.vorbis_comment.vendor_string.entry);
+                               object->data.vorbis_comment.vendor_string.entry = 0;
+                       }
                        if(0 != object->data.vorbis_comment.comments) {
                                FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
                                vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
@@ -1144,7 +1152,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__Stre
        track = &object->data.cue_sheet.tracks[track_num];
 
        /* move all indices > index_num backward one space */
-       memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(track->num_indices-index_num-1));
+       memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
 
        FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
        cuesheet_calculate_length_(object);
index 0071eba..aca9406 100644 (file)
@@ -296,8 +296,7 @@ FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
        FLAC__ASSERT(0 != decoder);
        if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
                return;
-       if(decoder->private_->has_seek_table) {
-               FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
+       if(0 != decoder->private_->seek_table.data.seek_table.points) {
                free(decoder->private_->seek_table.data.seek_table.points);
                decoder->private_->seek_table.data.seek_table.points = 0;
                decoder->private_->has_seek_table = false;
@@ -1030,7 +1029,8 @@ FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_
 
        decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
 
-       if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
+       /* use realloc since we may pass through here several times (e.g. after seeking) */
+       if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
index a1f153b..db123d8 100644 (file)
@@ -506,7 +506,7 @@ FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
 
        FLAC__stream_encoder_finish(encoder);
 
-       if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
+       if(0 != encoder->private_->verify.decoder)
                FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
index 5158264..c3e6549 100644 (file)
@@ -294,8 +294,13 @@ void free_options(CommandLineOptions *options)
        if(0 != options->args.arguments)
                free(options->args.arguments);
 
-       if(0 != options->filenames)
+       if(0 != options->filenames) {
+               for(i = 0; i < options->num_files; i++) {
+                       if(0 != options->filenames[i])
+                               free(options->filenames[i]);
+               }
                free(options->filenames);
+       }
 }
 
 /*
index afcda56..f28e0c0 100644 (file)
@@ -730,9 +730,17 @@ FLAC__bool test_bitbuffer()
        }
        printf("OK\n");
 
-       printf("testing free... OK\n");
+       printf("testing free... ");
        FLAC__bitbuffer_free(bb);
        FLAC__bitbuffer_free(bbcopy);
+       printf("OK\n");
+
+       printf("testing delete... ");
+       FLAC__bitbuffer_delete(bb);
+       FLAC__bitbuffer_delete(bb_zero);
+       FLAC__bitbuffer_delete(bb_one);
+       FLAC__bitbuffer_delete(bbcopy);
+       printf("OK\n");
 
        printf("\nPASSED!\n");
        return true;
index fd2bfe5..1445707 100644 (file)
@@ -482,6 +482,7 @@ static FLAC__bool test_level_1_()
                return die_("mismatch in min_blocksize");
        if(block->data.stream_info.max_blocksize != 576)
                return die_("mismatch in max_blocksize");
+       FLAC__metadata_object_delete(block);
 
        if(!FLAC__metadata_simple_iterator_next(iterator))
                return die_("forward iterator ended early");
@@ -502,6 +503,7 @@ static FLAC__bool test_level_1_()
        /* check to see if some basic data matches (c.f. generate_file_()) */
        if(block->length != 1234)
                return die_("bad PADDING length");
+       FLAC__metadata_object_delete(block);
 
        if(FLAC__metadata_simple_iterator_next(iterator))
                return die_("forward iterator returned true but should have returned false");