Skip to content

Commit 5f83c30

Browse files
authored
Merge pull request #229 from sparkprime/json_to_heap_segfault
Postpone setting filled until content points to valid data.
2 parents 7415b0b + 6ea31d5 commit 5f83c30

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

core/vm.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,47 +1270,51 @@ class Interpreter {
12701270
return nullptr;
12711271
}
12721272

1273-
void jsonToHeap(const std::unique_ptr<JsonnetJsonValue> &v, Value &attach)
1273+
void jsonToHeap(const std::unique_ptr<JsonnetJsonValue> &v, bool &filled, Value &attach)
12741274
{
12751275
// In order to not anger the garbage collector, assign to attach immediately after
12761276
// making the heap object.
12771277
switch (v->kind) {
12781278
case JsonnetJsonValue::STRING:
12791279
attach = makeString(decode_utf8(v->string));
1280+
filled = true;
12801281
break;
12811282

12821283
case JsonnetJsonValue::BOOL:
12831284
attach = makeBoolean(v->number != 0.0);
1285+
filled = true;
12841286
break;
12851287

12861288
case JsonnetJsonValue::NUMBER:
12871289
attach = makeDouble(v->number);
1290+
filled = true;
12881291
break;
12891292

12901293
case JsonnetJsonValue::NULL_KIND:
12911294
attach = makeNull();
1295+
filled = true;
12921296
break;
12931297

12941298
case JsonnetJsonValue::ARRAY: {
12951299
attach = makeArray(std::vector<HeapThunk*>{});
1300+
filled = true;
12961301
auto *arr = static_cast<HeapArray*>(attach.v.h);
12971302
for (size_t i = 0; i < v->elements.size() ; ++i) {
12981303
arr->elements.push_back(
12991304
makeHeap<HeapThunk>(idArrayElement, nullptr, 0, nullptr));
1300-
arr->elements[i]->filled = true;
1301-
jsonToHeap(v->elements[i], arr->elements[i]->content);
1305+
jsonToHeap(v->elements[i], arr->elements[i]->filled, arr->elements[i]->content);
13021306
}
13031307
} break;
13041308

13051309
case JsonnetJsonValue::OBJECT: {
13061310
attach = makeObject<HeapComprehensionObject>(
13071311
BindingFrame{}, jsonObjVar, idJsonObjVar, BindingFrame{});
1312+
filled = true;
13081313
auto *obj = static_cast<HeapComprehensionObject*>(attach.v.h);
13091314
for (const auto &pair : v->fields) {
13101315
auto *thunk = makeHeap<HeapThunk>(idJsonObjVar, nullptr, 0, nullptr);
13111316
obj->compValues[alloc->makeIdentifier(decode_utf8(pair.first))] = thunk;
1312-
thunk->filled = true;
1313-
jsonToHeap(pair.second, thunk->content);
1317+
jsonToHeap(pair.second, thunk->filled, thunk->content);
13141318
}
13151319
} break;
13161320
}
@@ -2092,7 +2096,8 @@ class Interpreter {
20922096
std::unique_ptr<JsonnetJsonValue> r(cb.cb(cb.ctx, &args3[0], &succ));
20932097

20942098
if (succ) {
2095-
jsonToHeap(r, scratch);
2099+
bool unused;
2100+
jsonToHeap(r, unused, scratch);
20962101
} else {
20972102
if (r->kind != JsonnetJsonValue::STRING) {
20982103
throw makeError(

0 commit comments

Comments
 (0)