Skip to content

Commit bccd73c

Browse files
authored
Fix stack overflow when calling quirc_decode (#165)
Some stack variables have been moved to the heap to leave just enough extra stack space for the function.
1 parent a534920 commit bccd73c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

source/qr/qrcode.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,17 @@ void QRCode::handler(std::string &result) {
385385
quirc_end(this->qrData);
386386

387387
if (quirc_count(this->qrData) > 0) {
388-
struct quirc_code code;
389-
struct quirc_data scan_data;
390-
quirc_extract(this->qrData, 0, &code);
391-
392-
if (!quirc_decode(&code, &scan_data)) {
388+
// `quirc_decode` uses some large stack buffers, which is why
389+
// `code` and `scan_data` here are allocated on the heap to leave
390+
// just barely enough stack space for it to not overflow the stack.
391+
std::unique_ptr<struct quirc_code> code = std::make_unique<struct quirc_code>();
392+
std::unique_ptr<struct quirc_data> scan_data = std::make_unique<struct quirc_data>();
393+
quirc_extract(this->qrData, 0, code.get());
394+
395+
if (!quirc_decode(code.get(), scan_data.get())) {
393396
this->finish();
394-
this->out.resize(scan_data.payload_len);
395-
std::copy(scan_data.payload, scan_data.payload + scan_data.payload_len, this->out.begin());
397+
this->out.resize(scan_data->payload_len);
398+
std::copy(scan_data->payload, scan_data->payload + scan_data->payload_len, this->out.begin());
396399

397400
/* From scanned stuff. */
398401
if (this->out.empty()) result = "";

0 commit comments

Comments
 (0)