Skip to content

Commit ccc2772

Browse files
KonanoGZTimeWalker
andauthored
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 <[email protected]>
1 parent 960b3f6 commit ccc2772

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void DecodedBitStreamParser::append(std::string& result, string const& in,
6565

6666
void DecodedBitStreamParser::append(std::string& result, const char* bufIn, size_t nIn,
6767
ErrorHandler& err_handler) {
68-
if (err_handler.ErrCode()) return;
68+
// avoid null pointer exception
69+
if (err_handler.ErrCode() || bufIn == nullptr) return;
6970
#ifndef NO_ICONV_INSIDE
7071
if (nIn == 0) {
7172
return;
@@ -190,16 +191,20 @@ void DecodedBitStreamParser::decodeByteSegment(Ref<BitSource> bits_, string& res
190191
CharacterSetECI* currentCharacterSetECI,
191192
ArrayRef<ArrayRef<char> >& byteSegments,
192193
ErrorHandler& err_handler) {
193-
int nBytes = count;
194194
BitSource& bits(*bits_);
195195
// Don't crash trying to read more bits than we have available.
196196
int available = bits.available();
197197
// try to repair count data if count data is invalid
198198
if (count * 8 > available) {
199-
count = (available + 7 / 8);
199+
count = (available + 7) / 8;
200200
}
201+
size_t nBytes = count;
202+
203+
ArrayRef<char> bytes_(nBytes);
204+
// issue https://github.com/opencv/opencv_contrib/issues/3478
205+
if (bytes_->empty())
206+
return;
201207

202-
ArrayRef<char> bytes_(count);
203208
char* readBytes = &(*bytes_)[0];
204209
for (int i = 0; i < count; i++) {
205210
// readBytes[i] = (char) bits.readBits(8);

modules/wechat_qrcode/test/test_qrcode.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,16 @@ TEST_P(Objdetect_QRCode_Easy_Multi, regression) {
455455
std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"};
456456
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Easy_Multi, testing::ValuesIn(qrcode_model_path));
457457

458+
TEST(Objdetect_QRCode_bug, issue_3478) {
459+
auto detector = wechat_qrcode::WeChatQRCode();
460+
std::string image_path = findDataFile("qrcode/issue_3478.png");
461+
Mat src = imread(image_path, IMREAD_GRAYSCALE);
462+
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
463+
std::vector<std::string> outs = detector.detectAndDecode(src);
464+
ASSERT_EQ(1, (int) outs.size());
465+
ASSERT_EQ(16, (int) outs[0].size());
466+
ASSERT_EQ("KFCVW50 ", outs[0]);
467+
}
468+
458469
} // namespace
459470
} // namespace opencv_test

0 commit comments

Comments
 (0)