Skip to content

Commit 7b234f0

Browse files
Do it on the heap
1 parent 455187a commit 7b234f0

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

c_mpos/src/quirc_decode.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <stdio.h>
2+
#include <stdlib.h> // Added for malloc and free
23
#include "py/obj.h"
34
#include "py/runtime.h"
45
#include "py/mperrno.h"
56
#include <string.h>
67
#include "../quirc/lib/quirc.h"
7-
#include "py/mpstate.h" // Added for micropython_stack_use()
8+
#include "py/mpstate.h" // For micropython_stack_use()
89

910
#define QRDECODE_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__);
1011

@@ -117,28 +118,45 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
117118
QRDECODE_DEBUG_PRINT("qrdecode: Extracting first QR code\n");
118119
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
119120
fflush(stdout);
120-
struct quirc_code code;
121-
quirc_extract(qr, 0, &code);
121+
struct quirc_code *code = (struct quirc_code *)malloc(sizeof(struct quirc_code));
122+
if (!code) {
123+
quirc_destroy(qr);
124+
mp_raise_OSError(MP_ENOMEM);
125+
}
126+
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code on heap\n");
127+
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
128+
fflush(stdout);
129+
quirc_extract(qr, 0, code);
122130
QRDECODE_DEBUG_PRINT("qrdecode: QR code extracted\n");
123131
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
124132
fflush(stdout);
125133
// it works until here!
126-
/*
127-
// Decode the QR code - this is the part that fails:
134+
135+
// Decode the QR code - this is the part that fails (uncomment to test):
128136
QRDECODE_DEBUG_PRINT("qrdecode: Decoding QR code\n");
129137
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
130138
fflush(stdout);
131-
struct quirc_data data;
132-
int err = quirc_decode(&code, &data);
139+
struct quirc_data *data = (struct quirc_data *)malloc(sizeof(struct quirc_data));
140+
if (!data) {
141+
free(code);
142+
quirc_destroy(qr);
143+
mp_raise_OSError(MP_ENOMEM);
144+
}
145+
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_data on heap\n");
146+
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
147+
fflush(stdout);
148+
int err = quirc_decode(code, data);
133149
if (err != QUIRC_SUCCESS) {
150+
free(data);
151+
free(code);
134152
quirc_destroy(qr);
135153
mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
136154
}
137-
QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
155+
QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data->payload_len);
138156
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
139157
fflush(stdout);
140158

141-
QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data.payload);
159+
QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data->payload);
142160
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
143161
fflush(stdout);
144162
QRDECODE_DEBUG_PRINT("ok so now what?!");
@@ -148,7 +166,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
148166
QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n");
149167
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
150168
fflush(stdout);
151-
mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len);
169+
mp_obj_t result = mp_obj_new_str((const char *)data->payload, data->payload_len);
152170
QRDECODE_DEBUG_PRINT("qrdecode: Python string created\n");
153171
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
154172
fflush(stdout);
@@ -157,16 +175,17 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
157175
QRDECODE_DEBUG_PRINT("qrdecode: Cleaning up\n");
158176
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
159177
fflush(stdout);
178+
free(data);
179+
free(code);
160180
quirc_destroy(qr);
161181
QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n");
162182
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
163183
fflush(stdout);
164-
*/
165184

166185
QRDECODE_DEBUG_PRINT("qrdecode: Returning result\n");
167186
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
168-
//return result;
169-
return mp_const_none; // MicroPython functions typically return None
187+
return result;
188+
//return mp_const_none; // MicroPython functions typically return None
170189
}
171190

172191
// Wrapper function to fix incompatible pointer type warning

0 commit comments

Comments
 (0)