Skip to content

Commit c1fde47

Browse files
authored
Fix memory leak in libjsonnet++.cpp (#1013)
1 parent ad5d1e0 commit c1fde47

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cpp/libjsonnet++.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ bool Jsonnet::evaluateFile(const std::string& filename, std::string* output)
9494
return false;
9595
}
9696
int error = 0;
97-
const char* jsonnet_output = ::jsonnet_evaluate_file(vm_, filename.c_str(), &error);
97+
char* jsonnet_output = ::jsonnet_evaluate_file(vm_, filename.c_str(), &error);
9898
if (error != 0) {
9999
last_error_.assign(jsonnet_output);
100+
jsonnet_realloc(vm_, jsonnet_output, 0);
100101
return false;
101102
}
102103
output->assign(jsonnet_output);
104+
jsonnet_realloc(vm_, jsonnet_output, 0);
103105
return true;
104106
}
105107

@@ -110,13 +112,15 @@ bool Jsonnet::evaluateSnippet(const std::string& filename, const std::string& sn
110112
return false;
111113
}
112114
int error = 0;
113-
const char* jsonnet_output =
115+
char* jsonnet_output =
114116
::jsonnet_evaluate_snippet(vm_, filename.c_str(), snippet.c_str(), &error);
115117
if (error != 0) {
116118
last_error_.assign(jsonnet_output);
119+
jsonnet_realloc(vm_, jsonnet_output, 0);
117120
return false;
118121
}
119122
output->assign(jsonnet_output);
123+
jsonnet_realloc(vm_, jsonnet_output, 0);
120124
return true;
121125
}
122126

@@ -146,12 +150,14 @@ bool Jsonnet::evaluateFileMulti(const std::string& filename,
146150
return false;
147151
}
148152
int error = 0;
149-
const char* jsonnet_output = ::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
153+
char* jsonnet_output = ::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
150154
if (error != 0) {
151155
last_error_.assign(jsonnet_output);
156+
jsonnet_realloc(vm_, jsonnet_output, 0);
152157
return false;
153158
}
154159
parseMultiOutput(jsonnet_output, outputs);
160+
jsonnet_realloc(vm_, jsonnet_output, 0);
155161
return true;
156162
}
157163

@@ -162,13 +168,15 @@ bool Jsonnet::evaluateSnippetMulti(const std::string& filename, const std::strin
162168
return false;
163169
}
164170
int error = 0;
165-
const char* jsonnet_output =
171+
char* jsonnet_output =
166172
::jsonnet_evaluate_snippet_multi(vm_, filename.c_str(), snippet.c_str(), &error);
167173
if (error != 0) {
168174
last_error_.assign(jsonnet_output);
175+
jsonnet_realloc(vm_, jsonnet_output, 0);
169176
return false;
170177
}
171178
parseMultiOutput(jsonnet_output, outputs);
179+
jsonnet_realloc(vm_, jsonnet_output, 0);
172180
return true;
173181
}
174182

0 commit comments

Comments
 (0)