Skip to content

Commit

Permalink
Fix a bunch of bugs in skip_single_link implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmf01 committed Sep 26, 2024
1 parent 93f586f commit 3117e86
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/libFLAC/include/private/ogg_decoder_aspect.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef struct FLAC__OggDecoderAspect {
FLAC__OggDecoderAspect_TargetLink target_link; /* to pass data to the seek routine */
uint32_t number_of_links_detected;
uint32_t number_of_links_indexed;
uint32_t number_of_links_allocated;
uint32_t current_linknumber; /* The linknumber the FLAC parser is in */
uint32_t current_linknumber_advance_read; /* The linknumber the ogg parser is in. The name 'advance read' is because it reads ahead, to see whether there is another link */
FLAC__bool is_seeking;
Expand Down
11 changes: 7 additions & 4 deletions src/libFLAC/ogg_decoder_aspect.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ static FLAC__OggDecoderAspectReadStatus process_page_(FLAC__OggDecoderAspect *as
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR;
}
current_link->other_serial_numbers = tmpptr;
if(aspect->number_of_links_allocated < aspect->current_linknumber_advance_read)
aspect->number_of_links_allocated = aspect->current_linknumber_advance_read;
}
current_link->other_serial_numbers[current_link->number_of_other_streams] = ogg_page_serialno(&aspect->working_page);
current_link->number_of_other_streams++;
Expand Down Expand Up @@ -166,6 +168,7 @@ FLAC__bool FLAC__ogg_decoder_aspect_init(FLAC__OggDecoderAspect *aspect)
aspect->current_linknumber_advance_read = 0;
aspect->number_of_links_indexed = 0;
aspect->number_of_links_detected = 0;
aspect->number_of_links_allocated = 0;

if(NULL == (aspect->linkdetails = safe_realloc_mul_2op_(NULL,4,sizeof(FLAC__OggDecoderAspect_LinkDetails))))
return false;
Expand All @@ -180,7 +183,7 @@ void FLAC__ogg_decoder_aspect_finish(FLAC__OggDecoderAspect *aspect)
(void)ogg_sync_clear(&aspect->sync_state);
(void)ogg_stream_clear(&aspect->stream_state);
if(NULL != aspect->linkdetails) {
for(i = 0; i <= aspect->number_of_links_indexed; i++)
for(i = 0; i <= aspect->number_of_links_allocated; i++)
free(aspect->linkdetails[i].other_serial_numbers);
free(aspect->linkdetails);
}
Expand Down Expand Up @@ -341,7 +344,7 @@ FLAC__OggDecoderAspectReadStatus FLAC__ogg_decoder_aspect_read_callback_wrapper(
aspect->end_of_stream = true;
else {
aspect->end_of_link = true;
aspect->current_linknumber_advance_read++;
aspect->current_linknumber_advance_read = aspect->current_linknumber + 1;
if(aspect->current_linknumber >= aspect->number_of_links_indexed) {
FLAC__uint64 tell_offset;
FLAC__ASSERT(aspect->current_linknumber == aspect->number_of_links_indexed);
Expand Down Expand Up @@ -478,7 +481,7 @@ FLAC__OggDecoderAspectReadStatus FLAC__ogg_decoder_aspect_skip_link(FLAC__OggDec
aspect->need_serial_number = true;
aspect->bos_flag_seen = false;
aspect->current_linknumber++;
aspect->current_linknumber_advance_read++;
aspect->current_linknumber_advance_read = aspect->current_linknumber;
aspect->have_working_page = false;
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
}
Expand Down Expand Up @@ -602,7 +605,7 @@ FLAC__OggDecoderAspectReadStatus FLAC__ogg_decoder_aspect_skip_link(FLAC__OggDec
aspect->linkdetails[aspect->current_linknumber].samples = ogg_page_granulepos(&aspect->working_page);

aspect->number_of_links_indexed++;
aspect->current_linknumber_advance_read++;
aspect->current_linknumber_advance_read = aspect->current_linknumber + 1;
aspect->need_serial_number = true;

/* reallocate in chunks of 4 */
Expand Down
6 changes: 5 additions & 1 deletion src/libFLAC/stream_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,8 @@ FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_link(FLAC__StreamDecoder *decoder)
{
#if FLAC__HAS_OGG
FLAC__ASSERT_DECLARATION(FLAC__uint32 linknumber_start);
FLAC__OggDecoderAspectReadStatus status;
FLAC__ASSERT_DECLARATION(FLAC__uint32 linknumber_start);
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->protected_);

Expand Down Expand Up @@ -1253,6 +1253,10 @@ FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_link(FLAC__StreamDecoder *d
decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
return false;
}
else {
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
reset_decoder_internal_(decoder);
}

FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM || decoder->protected_->ogg_decoder_aspect.current_linknumber > linknumber_start);
return true;
Expand Down

0 comments on commit 3117e86

Please sign in to comment.