From 06c9c5327b1baaeded4b150af2a80b0fa37116f1 Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Tue, 30 Dec 2003 03:33:43 +0000 Subject: [PATCH] fix bug in read callback where *bytes was not being set to 0 on end-of-stream condition --- src/libFLAC/seekable_stream_decoder.c | 7 +++---- src/test_libFLAC/decoders.c | 14 ++++++++------ src/test_libOggFLAC/decoders.c | 16 +++++++++------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/libFLAC/seekable_stream_decoder.c b/src/libFLAC/seekable_stream_decoder.c index 7cc074d..4648d0e 100644 --- a/src/libFLAC/seekable_stream_decoder.c +++ b/src/libFLAC/seekable_stream_decoder.c @@ -706,16 +706,16 @@ FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data; (void)decoder; if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) { + *bytes = 0; seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM; return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; } else if(*bytes > 0) { - unsigned bytes_read = *bytes; - if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, &bytes_read, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) { + if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, bytes, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) { seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR; return FLAC__STREAM_DECODER_READ_STATUS_ABORT; } - if(bytes_read == 0) { + if(*bytes == 0) { if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) { seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM; return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; @@ -724,7 +724,6 @@ FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; } else { - *bytes = bytes_read; return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; } } diff --git a/src/test_libFLAC/decoders.c b/src/test_libFLAC/decoders.c index 2d50557..40c27be 100644 --- a/src/test_libFLAC/decoders.c +++ b/src/test_libFLAC/decoders.c @@ -137,6 +137,7 @@ static FLAC__bool generate_file_() static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) { stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data; + const unsigned requested_bytes = *bytes; (void)decoder; @@ -148,18 +149,19 @@ static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const FLAC__S if(dcd->error_occurred) return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - if(feof(dcd->file)) + if(feof(dcd->file)) { + *bytes = 0; return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - else if(*bytes > 0) { - unsigned bytes_read = fread(buffer, 1, *bytes, dcd->file); - if(bytes_read == 0) { + } + else if(requested_bytes > 0) { + *bytes = fread(buffer, 1, requested_bytes, dcd->file); + if(*bytes == 0) { if(feof(dcd->file)) return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; else - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; } else { - *bytes = bytes_read; return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; } } diff --git a/src/test_libOggFLAC/decoders.c b/src/test_libOggFLAC/decoders.c index 7238311..3351d91 100644 --- a/src/test_libOggFLAC/decoders.c +++ b/src/test_libOggFLAC/decoders.c @@ -149,6 +149,7 @@ static FLAC__bool generate_file_() static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) { stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data; + const unsigned requested_bytes = *bytes; (void)decoder; @@ -160,18 +161,19 @@ static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const OggFLAC if(dcd->error_occurred) return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - if(feof(dcd->file)) + if(feof(dcd->file)) { + *bytes = 0; return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - else if(*bytes > 0) { - unsigned bytes_read = fread(buffer, 1, *bytes, dcd->file); - if(bytes_read == 0) { + } + else if(requested_bytes > 0) { + *bytes = fread(buffer, 1, requested_bytes, dcd->file); + if(*bytes == 0) { if(feof(dcd->file)) return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; else - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; } else { - *bytes = bytes_read; return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; } } @@ -805,9 +807,9 @@ static FLAC__SeekableStreamDecoderReadStatus seekable_stream_decoder_read_callba (void)decoder; switch(stream_decoder_read_callback_(0, buffer, bytes, client_data)) { case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE: + case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK; case FLAC__STREAM_DECODER_READ_STATUS_ABORT: - case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; default: FLAC__ASSERT(0); -- 2.7.4