From 2b62ff6181163eea029ed1cab11363b4996e9cd6 Mon Sep 17 00:00:00 2001
From: Nano <nanoapezlk@gmail.com>
Date: Thu, 27 Apr 2023 17:38:35 +0800
Subject: [PATCH] fix(wechat_qrcode): fixed memory leaks

CVE: CVE-2023-2618

Upstream-Status: Backport [https://github.com/opencv/opencv_contrib/pull/3484/commits/2b62ff6181163eea029ed1cab11363b4996e9cd6]

Signed-off-by: Narpat Mali <narpat.mali@windriver.com>
---
 .../src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp   | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp b/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp
index b3a0a69c..f02435d5 100644
--- a/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp
+++ b/modules/wechat_qrcode/src/zxing/qrcode/decoder/decoded_bit_stream_parser.cpp
@@ -127,7 +127,10 @@ void DecodedBitStreamParser::decodeHanziSegment(Ref<BitSource> bits_, string& re
     while (count > 0) {
         // Each 13 bits encodes a 2-byte character
         int twoBytes = bits.readBits(13, err_handler);
-        if (err_handler.ErrCode()) return;
+        if (err_handler.ErrCode()) {
+            delete[] buffer;
+            return;
+        }
         int assembledTwoBytes = ((twoBytes / 0x060) << 8) | (twoBytes % 0x060);
         if (assembledTwoBytes < 0x003BF) {
             // In the 0xA1A1 to 0xAAFE range
--
2.40.0
