From: Nano Date: Wed, 26 Apr 2023 07:09:52 +0000 (+0800) Subject: fix(wechat_qrcode): Init nBytes after the count value is determined (#3480) X-Git-Tag: archive/raspbian/4.6.0+dfsg-13+rpi1^2~3 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=02a59df2afabf14cf898f6e0b8641973a7b05625;p=opencv.git fix(wechat_qrcode): Init nBytes after the count value is determined (#3480) * fix(wechat_qrcode): Initialize nBytes after the count value is determined * fix(wechat_qrcode): Incorrect count data repair * chore: format expr * fix(wechat_qrcode): Avoid null pointer exception * fix(wechat_qrcode): return when bytes_ is empty * test(wechat_qrcode): add test case --------- Co-authored-by: GZTime Gbp-Pq: Name 0009-fix-wechat_qrcode-Init-nBytes-after-the-count-value-.patch --- diff --git a/contrib/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp b/contrib/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp index 05de793..b3a0a69 100644 --- a/contrib/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp +++ b/contrib/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp @@ -65,7 +65,8 @@ void DecodedBitStreamParser::append(std::string& result, string const& in, void DecodedBitStreamParser::append(std::string& result, const char* bufIn, size_t nIn, ErrorHandler& err_handler) { - if (err_handler.ErrCode()) return; + // avoid null pointer exception + if (err_handler.ErrCode() || bufIn == nullptr) return; #ifndef NO_ICONV_INSIDE if (nIn == 0) { return; @@ -190,16 +191,20 @@ void DecodedBitStreamParser::decodeByteSegment(Ref bits_, string& res CharacterSetECI* currentCharacterSetECI, ArrayRef >& byteSegments, ErrorHandler& err_handler) { - int nBytes = count; BitSource& bits(*bits_); // Don't crash trying to read more bits than we have available. int available = bits.available(); // try to repair count data if count data is invalid if (count * 8 > available) { - count = (available + 7 / 8); + count = (available + 7) / 8; } + size_t nBytes = count; + + ArrayRef bytes_(nBytes); + // issue https://github.com/opencv/opencv_contrib/issues/3478 + if (bytes_->empty()) + return; - ArrayRef bytes_(count); char* readBytes = &(*bytes_)[0]; for (int i = 0; i < count; i++) { // readBytes[i] = (char) bits.readBits(8); diff --git a/contrib/modules/wechat_qrcode/test/test_qrcode.cpp b/contrib/modules/wechat_qrcode/test/test_qrcode.cpp index 5de6533..6989563 100644 --- a/contrib/modules/wechat_qrcode/test/test_qrcode.cpp +++ b/contrib/modules/wechat_qrcode/test/test_qrcode.cpp @@ -289,5 +289,16 @@ INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Curved, testing::ValuesIn(qrcode_images_curved)); // INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple)); +TEST(Objdetect_QRCode_bug, issue_3478) { + auto detector = wechat_qrcode::WeChatQRCode(); + std::string image_path = findDataFile("qrcode/issue_3478.png"); + Mat src = imread(image_path, IMREAD_GRAYSCALE); + ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; + std::vector outs = detector.detectAndDecode(src); + ASSERT_EQ(1, (int) outs.size()); + ASSERT_EQ(16, (int) outs[0].size()); + ASSERT_EQ("KFCVW50 ", outs[0]); +} + } // namespace } // namespace opencv_test