From 2af6775c14a553ebe6e38f6a8f54b4d023c6ebb0 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Tue, 11 Jul 2023 09:25:53 +0200 Subject: [PATCH] Merge pull request #23943 from vrabaud:avif_tsan Fix checkSignature not thread safe for AVIF. #23943 A common decoder cannot be shared with checkSignature which is used like a static function (on a static ist of decoders). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/imgcodecs/src/grfmt_avif.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_avif.cpp b/modules/imgcodecs/src/grfmt_avif.cpp index e8d1446cbe..b2830b755a 100644 --- a/modules/imgcodecs/src/grfmt_avif.cpp +++ b/modules/imgcodecs/src/grfmt_avif.cpp @@ -148,11 +148,14 @@ AvifDecoder::~AvifDecoder() { size_t AvifDecoder::signatureLength() const { return kAvifSignatureSize; } bool AvifDecoder::checkSignature(const String &signature) const { - avifDecoderSetIOMemory(decoder_, + avifDecoder *decoder = avifDecoderCreate(); + if (!decoder) return false; + avifDecoderSetIOMemory(decoder, reinterpret_cast(signature.c_str()), signature.size()); - decoder_->io->sizeHint = 1e9; - const avifResult status = avifDecoderParse(decoder_); + decoder->io->sizeHint = 1e9; + const avifResult status = avifDecoderParse(decoder); + avifDecoderDestroy(decoder); return (status == AVIF_RESULT_OK || status == AVIF_RESULT_TRUNCATED_DATA); }