Skip to content

Commit e37194d

Browse files
committed
fix memory leaks when input json is bad
1 parent 8b12815 commit e37194d

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

src/jres_json_converter.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ char* allocate_and_copy(const std::string& s) {
5959

6060
JRES_SOLVER_API JresSolverInput* jres_input_from_json(const char* jsonData) {
6161
last_error_message.clear(); // Clear previous error
62+
JresSolverInput* input = nullptr;
6263
try {
6364
json j = json::parse(jsonData);
64-
JresSolverInput* input = new JresSolverInput();
65+
input = new JresSolverInput();
6566

6667
if (j.find("teamMembers") == j.end()) {
6768
throw std::runtime_error("Missing 'teamMembers' key in input JSON.");
@@ -126,9 +127,11 @@ JRES_SOLVER_API JresSolverInput* jres_input_from_json(const char* jsonData) {
126127

127128
} catch (const json::parse_error& e) {
128129
last_error_message = "JSON parse error: " + std::string(e.what());
130+
if (input) free_jres_solver_input(input);
129131
return nullptr;
130132
} catch (const std::exception& e) {
131133
last_error_message = "Error: " + std::string(e.what());
134+
if (input) free_jres_solver_input(input);
132135
return nullptr;
133136
}
134137
}
@@ -260,29 +263,37 @@ JRES_SOLVER_API void free_jres_solver_output(JresSolverOutput* output) {
260263
}
261264

262265
JRES_SOLVER_API void free_jres_solver_input(JresSolverInput* input) {
266+
if (!input) return;
267+
263268
if (input->firstStintDriver) {
264269
delete[] input->firstStintDriver;
265270
}
266271

267-
for (int i = 0; i < input->teamMembers_len; ++i) {
268-
delete[] input->teamMembers[i].name;
272+
if (input->teamMembers) {
273+
for (int i = 0; i < input->teamMembers_len; ++i) {
274+
delete[] input->teamMembers[i].name;
275+
}
276+
delete[] input->teamMembers;
269277
}
270-
delete[] input->teamMembers;
271278

272-
for (int i = 0; i < input->stints_len; ++i) {
273-
delete[] input->stints[i].startTime;
274-
delete[] input->stints[i].endTime;
279+
if (input->stints) {
280+
for (int i = 0; i < input->stints_len; ++i) {
281+
delete[] input->stints[i].startTime;
282+
delete[] input->stints[i].endTime;
283+
}
284+
delete[] input->stints;
275285
}
276-
delete[] input->stints;
277286

278-
for (int i = 0; i < input->availability_len; ++i) {
279-
for (int j = 0; j < input->availability[i].availability_len; ++j) {
280-
delete[] input->availability[i].availability[j].time;
287+
if (input->availability) {
288+
for (int i = 0; i < input->availability_len; ++i) {
289+
for (int j = 0; j < input->availability[i].availability_len; ++j) {
290+
delete[] input->availability[i].availability[j].time;
291+
}
292+
delete[] input->availability[i].availability;
293+
delete[] input->availability[i].name;
281294
}
282-
delete[] input->availability[i].availability;
283-
delete[] input->availability[i].name;
295+
delete[] input->availability;
284296
}
285-
delete[] input->availability;
286297

287298
delete input;
288299
}

0 commit comments

Comments
 (0)