Skip to content

Commit

Permalink
fix potential race condition during library initialization (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 11, 2023
1 parent 0b3d6dd commit b04bfc0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libheif/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ struct heif_error heif_init(struct heif_init_params*)
std::lock_guard<std::recursive_mutex> lock(heif_init_mutex());
#endif

heif_library_initialization_count++;

if (heif_library_initialization_count == 1) {
if (heif_library_initialization_count == 0) {

ColorConversionPipeline::init_ops();

Expand All @@ -133,6 +131,10 @@ struct heif_error heif_init(struct heif_init_params*)
#endif
}

// Note: it is important that we increase the counter AFTER initialization such that 'load_plugins_if_not_initialized_yet()' can check this
// without having to lock the mutex.
heif_library_initialization_count++;

return {heif_error_Ok, heif_suberror_Unspecified, Error::kSuccess};
}

Expand All @@ -148,9 +150,7 @@ void heif_deinit()
return;
}

heif_library_initialization_count--;

if (heif_library_initialization_count == 0) {
if (heif_library_initialization_count == 1) {
heif_unregister_decoder_plugins();
heif_unregister_encoder_plugins();
default_plugins_registered = false;
Expand All @@ -159,6 +159,10 @@ void heif_deinit()

ColorConversionPipeline::release_ops();
}

// Note: contrary to heif_init() I think it does not matter whether we decrease the counter before or after deinitialization.
// If the client application calls heif_deinit() in parallel to some other libheif function, it is really broken.
heif_library_initialization_count--;
}


Expand Down

0 comments on commit b04bfc0

Please sign in to comment.