Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of verify decoder allocation failure #678

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions oss-fuzz/encoder_v2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
FLAC__bool encoder_valid = true;
FLAC__StreamEncoder *encoder = 0;
FLAC__StreamEncoderState state;
const char* state_string = "";
FLAC__StreamMetadata *metadata[16] = {NULL};
unsigned num_metadata = 0;
FLAC__StreamMetadata_VorbisComment_Entry VorbisCommentField;
Expand Down Expand Up @@ -320,6 +321,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
}

state = FLAC__stream_encoder_get_state(encoder);
state_string = FLAC__stream_encoder_get_resolved_state_string(encoder);
if(!(state == FLAC__STREAM_ENCODER_OK ||
state == FLAC__STREAM_ENCODER_UNINITIALIZED ||
state == FLAC__STREAM_ENCODER_CLIENT_ERROR ||
Expand Down
1 change: 0 additions & 1 deletion oss-fuzz/seek.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)

/* allocate the decoder */
if((decoder = FLAC__stream_decoder_new()) == NULL) {
fprintf(stderr, "ERROR: allocating decoder\n");
return 1;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libFLAC/stream_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,18 @@ FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)

FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */

decoder = calloc(1, sizeof(FLAC__StreamDecoder));
decoder = safe_calloc_(1, sizeof(FLAC__StreamDecoder));
if(decoder == 0) {
return 0;
}

decoder->protected_ = calloc(1, sizeof(FLAC__StreamDecoderProtected));
decoder->protected_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderProtected));
if(decoder->protected_ == 0) {
free(decoder);
return 0;
}

decoder->private_ = calloc(1, sizeof(FLAC__StreamDecoderPrivate));
decoder->private_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderPrivate));
if(decoder->private_ == 0) {
free(decoder->protected_);
free(decoder);
Expand Down
7 changes: 6 additions & 1 deletion src/libFLAC/stream_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,10 @@ FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(
FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->protected_);
if(encoder->protected_->verify)
return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
if(encoder->private_->verify.decoder == NULL)
return FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
else
return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
else
return FLAC__STREAM_DECODER_UNINITIALIZED;
}
Expand All @@ -2337,6 +2340,8 @@ FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__
FLAC__ASSERT(0 != encoder->protected_);
if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
return FLAC__StreamEncoderStateString[encoder->protected_->state];
else if(!encoder->private_->verify.decoder)
return FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR];
else
return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
}
Expand Down
Loading