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

fix(wechat_qrcode): Init nBytes after the count value is determined #3480

Merged
merged 6 commits into from Apr 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -190,16 +191,20 @@ void DecodedBitStreamParser::decodeByteSegment(Ref<BitSource> bits_, string& res
CharacterSetECI* currentCharacterSetECI,
ArrayRef<ArrayRef<char> >& 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<char> bytes_(nBytes);
// issue https://github.com/opencv/opencv_contrib/issues/3478
if (bytes_->empty())
return;
zihaomu marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +201 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, bytes_->empty() should be equivalent to nBytes == 0. So why not using nBytes == 0 for simplicity and efficiency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change would be a more visual indication of the intention to avoid null pointers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


ArrayRef<char> bytes_(count);
char* readBytes = &(*bytes_)[0];
for (int i = 0; i < count; i++) {
// readBytes[i] = (char) bits.readBits(8);
Expand Down
11 changes: 11 additions & 0 deletions modules/wechat_qrcode/test/test_qrcode.cpp
Expand Up @@ -455,5 +455,16 @@ TEST_P(Objdetect_QRCode_Easy_Multi, regression) {
std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"};
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Easy_Multi, testing::ValuesIn(qrcode_model_path));

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<std::string> 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