Skip to content

Commit c0a2559

Browse files
committed
fix: segfaults with NULL instances
1 parent bd0eb8f commit c0a2559

23 files changed

+1307
-479
lines changed

src/DaedalusScript.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,32 @@ ZkDaedalusSymbol* ZkDaedalusScript_getSymbolByName(ZkDaedalusScript* slf, ZkStri
8888

8989
ZkString ZkDaedalusSymbol_getString(ZkDaedalusSymbol const* slf, uint16_t index, ZkDaedalusInstance const* context) {
9090
ZKC_CHECK_NULL(slf);
91-
ZKC_RETURN_CATCH(slf->get_string(index, context->get()).c_str());
91+
ZKC_RETURN_CATCH(slf->get_string(index, context ? context->get() : nullptr).c_str());
9292
}
9393

9494
float ZkDaedalusSymbol_getFloat(ZkDaedalusSymbol const* slf, uint16_t index, ZkDaedalusInstance const* context) {
9595
ZKC_CHECK_NULL(slf);
96-
ZKC_RETURN_CATCH(slf->get_float(index, context->get()));
96+
ZKC_RETURN_CATCH(slf->get_float(index, context ? context->get() : nullptr));
9797
}
9898

9999
int32_t ZkDaedalusSymbol_getInt(ZkDaedalusSymbol const* slf, uint16_t index, ZkDaedalusInstance const* context) {
100100
ZKC_CHECK_NULL(slf);
101-
ZKC_RETURN_CATCH(slf->get_int(index, context->get()));
101+
ZKC_RETURN_CATCH(slf->get_int(index, context ? context->get() : nullptr));
102102
}
103103

104104
void ZkDaedalusSymbol_setString(ZkDaedalusSymbol* slf, ZkString value, uint16_t index, ZkDaedalusInstance* context) {
105105
ZKC_CHECK_NULLV(slf, value);
106-
ZKC_CATCH(slf->set_string(value, index, context->get()));
106+
ZKC_CATCH(slf->set_string(value, index, context ? context->get() : nullptr));
107107
}
108108

109109
void ZkDaedalusSymbol_setFloat(ZkDaedalusSymbol* slf, float value, uint16_t index, ZkDaedalusInstance* context) {
110110
ZKC_CHECK_NULLV(slf);
111-
ZKC_CATCH(slf->set_float(value, index, context->get()));
111+
ZKC_CATCH(slf->set_float(value, index, context ? context->get() : nullptr));
112112
}
113113

114114
void ZkDaedalusSymbol_setInt(ZkDaedalusSymbol* slf, int32_t value, uint16_t index, ZkDaedalusInstance* context) {
115115
ZKC_CHECK_NULLV(slf);
116-
ZKC_CATCH(slf->set_int(value, index, context->get()));
116+
ZKC_CATCH(slf->set_int(value, index, context ? context->get() : nullptr));
117117
}
118118

119119
ZkBool ZkDaedalusSymbol_getIsConst(ZkDaedalusSymbol const* slf) {

src/DaedalusVm.cc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,71 +156,91 @@ ZkDaedalusInstance* ZkDaedalusVm_popInstance(ZkDaedalusVm* slf) {
156156
ZKC_TRACE_FN();
157157
ZKC_CHECK_NULL(slf);
158158

159-
auto* instance = new ZkDaedalusInstance(slf->handle.pop_instance());
160-
ZKC_RETURN_CATCH(instance);
159+
auto instance = slf->handle.pop_instance();
160+
if (instance == nullptr) {
161+
return nullptr;
162+
}
163+
164+
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
161165
}
162166

163167
ZkDaedalusInstance* ZkDaedalusVm_getGlobalSelf(ZkDaedalusVm* slf) {
164168
ZKC_TRACE_FN();
165169
ZKC_CHECK_NULL(slf);
166170

167171
auto& instance = slf->handle.global_self()->get_instance();
172+
if (instance == nullptr) {
173+
return nullptr;
174+
}
168175
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
169176
}
170177

171178
ZkDaedalusInstance* ZkDaedalusVm_getGlobalOther(ZkDaedalusVm* slf) {
172179
ZKC_TRACE_FN();
173180
ZKC_CHECK_NULL(slf);
174181
auto& instance = slf->handle.global_other()->get_instance();
182+
if (instance == nullptr) {
183+
return nullptr;
184+
}
175185
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
176186
}
177187

178188
ZkDaedalusInstance* ZkDaedalusVm_getGlobalVictim(ZkDaedalusVm* slf) {
179189
ZKC_TRACE_FN();
180190
ZKC_CHECK_NULL(slf);
181191
auto& instance = slf->handle.global_victim()->get_instance();
192+
if (instance == nullptr) {
193+
return nullptr;
194+
}
182195
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
183196
}
184197

185198
ZkDaedalusInstance* ZkDaedalusVm_getGlobalHero(ZkDaedalusVm* slf) {
186199
ZKC_TRACE_FN();
187200
ZKC_CHECK_NULL(slf);
188201
auto& instance = slf->handle.global_hero()->get_instance();
202+
if (instance == nullptr) {
203+
return nullptr;
204+
}
189205
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
190206
}
191207

192208
ZkDaedalusInstance* ZkDaedalusVm_getGlobalItem(ZkDaedalusVm* slf) {
193209
ZKC_TRACE_FN();
194210
ZKC_CHECK_NULL(slf);
195211
auto& instance = slf->handle.global_item()->get_instance();
212+
if (instance == nullptr) {
213+
return nullptr;
214+
}
215+
196216
ZKC_RETURN_CATCH(new ZkDaedalusInstance(instance));
197217
}
198218

199219
void ZkDaedalusVm_setGlobalSelf(ZkDaedalusVm* slf, ZkDaedalusInstance* value) {
200220
ZKC_TRACE_FN();
201221
ZKC_CHECK_NULLV(slf);
202222

203-
ZKC_CATCH(slf->handle.global_self()->set_instance(*value));
223+
ZKC_CATCH(slf->handle.global_self()->set_instance(value ? *value : nullptr));
204224
}
205225
void ZkDaedalusVm_setGlobalOther(ZkDaedalusVm* slf, ZkDaedalusInstance* value) {
206226
ZKC_TRACE_FN();
207227
ZKC_CHECK_NULLV(slf);
208-
ZKC_CATCH(slf->handle.global_other()->set_instance(*value));
228+
ZKC_CATCH(slf->handle.global_other()->set_instance(value ? *value : nullptr));
209229
}
210230
void ZkDaedalusVm_setGlobalVictim(ZkDaedalusVm* slf, ZkDaedalusInstance* value) {
211231
ZKC_TRACE_FN();
212232
ZKC_CHECK_NULLV(slf);
213-
ZKC_CATCH(slf->handle.global_victim()->set_instance(*value));
233+
ZKC_CATCH(slf->handle.global_victim()->set_instance(value ? *value : nullptr));
214234
}
215235
void ZkDaedalusVm_setGlobalHero(ZkDaedalusVm* slf, ZkDaedalusInstance* value) {
216236
ZKC_TRACE_FN();
217237
ZKC_CHECK_NULLV(slf);
218-
ZKC_CATCH(slf->handle.global_hero()->set_instance(*value));
238+
ZKC_CATCH(slf->handle.global_hero()->set_instance(value ? *value : nullptr));
219239
}
220240
void ZkDaedalusVm_setGlobalItem(ZkDaedalusVm* slf, ZkDaedalusInstance* value) {
221241
ZKC_TRACE_FN();
222242
ZKC_CHECK_NULLV(slf);
223-
ZKC_CATCH(slf->handle.global_item()->set_instance(*value));
243+
ZKC_CATCH(slf->handle.global_item()->set_instance(value ? *value : nullptr));
224244
}
225245

226246
void ZkDaedalusVm_callFunction(ZkDaedalusVm* slf, ZkDaedalusSymbol* sym) {

0 commit comments

Comments
 (0)