Skip to content

Commit 3cc45fd

Browse files
committed
Fix leak and possible segfault in minetest.set_mapgen_params
1 parent 83cc882 commit 3cc45fd

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/script/lua_api/l_mapgen.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,45 +81,45 @@ struct EnumString ModApiMapgen::es_Rotation[] =
8181
int ModApiMapgen::l_get_mapgen_object(lua_State *L)
8282
{
8383
const char *mgobjstr = lua_tostring(L, 1);
84-
84+
8585
int mgobjint;
8686
if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
8787
return 0;
88-
88+
8989
enum MapgenObject mgobj = (MapgenObject)mgobjint;
9090

9191
EmergeManager *emerge = getServer(L)->getEmergeManager();
9292
Mapgen *mg = emerge->getCurrentMapgen();
9393
if (!mg)
9494
return 0;
95-
95+
9696
size_t maplen = mg->csize.X * mg->csize.Z;
97-
97+
9898
int nargs = 1;
99-
99+
100100
switch (mgobj) {
101101
case MGOBJ_VMANIP: {
102102
ManualMapVoxelManipulator *vm = mg->vm;
103-
103+
104104
// VoxelManip object
105105
LuaVoxelManip *o = new LuaVoxelManip(vm, true);
106106
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
107107
luaL_getmetatable(L, "VoxelManip");
108108
lua_setmetatable(L, -2);
109-
109+
110110
// emerged min pos
111111
push_v3s16(L, vm->m_area.MinEdge);
112112

113113
// emerged max pos
114114
push_v3s16(L, vm->m_area.MaxEdge);
115-
115+
116116
nargs = 3;
117-
117+
118118
break; }
119119
case MGOBJ_HEIGHTMAP: {
120120
if (!mg->heightmap)
121121
return 0;
122-
122+
123123
lua_newtable(L);
124124
for (size_t i = 0; i != maplen; i++) {
125125
lua_pushinteger(L, mg->heightmap[i]);
@@ -129,7 +129,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
129129
case MGOBJ_BIOMEMAP: {
130130
if (!mg->biomemap)
131131
return 0;
132-
132+
133133
lua_newtable(L);
134134
for (size_t i = 0; i != maplen; i++) {
135135
lua_pushinteger(L, mg->biomemap[i]);
@@ -140,22 +140,22 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
140140
case MGOBJ_HUMIDMAP:
141141
if (strcmp(emerge->params->mg_name.c_str(), "v7"))
142142
return 0;
143-
143+
144144
MapgenV7 *mgv7 = (MapgenV7 *)mg;
145145

146-
float *arr = (mgobj == MGOBJ_HEATMAP) ?
146+
float *arr = (mgobj == MGOBJ_HEATMAP) ?
147147
mgv7->noise_heat->result : mgv7->noise_humidity->result;
148148
if (!arr)
149149
return 0;
150-
150+
151151
lua_newtable(L);
152152
for (size_t i = 0; i != maplen; i++) {
153153
lua_pushnumber(L, arr[i]);
154154
lua_rawseti(L, -2, i + 1);
155155
}
156156
break; }
157157
}
158-
158+
159159
return nargs;
160160
}
161161

@@ -167,25 +167,25 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L)
167167
return 0;
168168

169169
EmergeManager *emerge = getServer(L)->getEmergeManager();
170-
if (emerge->mapgen.size())
170+
if (!emerge || emerge->mapgen.size())
171171
return 0;
172-
172+
173173
MapgenParams *oparams = new MapgenParams;
174174
u32 paramsmodified = 0;
175175
u32 flagmask = 0;
176-
176+
177177
lua_getfield(L, 1, "mgname");
178178
if (lua_isstring(L, -1)) {
179179
oparams->mg_name = std::string(lua_tostring(L, -1));
180180
paramsmodified |= MGPARAMS_SET_MGNAME;
181181
}
182-
182+
183183
lua_getfield(L, 1, "seed");
184184
if (lua_isnumber(L, -1)) {
185185
oparams->seed = lua_tointeger(L, -1);
186186
paramsmodified |= MGPARAMS_SET_SEED;
187187
}
188-
188+
189189
lua_getfield(L, 1, "water_level");
190190
if (lua_isnumber(L, -1)) {
191191
oparams->water_level = lua_tointeger(L, -1);
@@ -197,18 +197,20 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L)
197197
std::string flagstr = std::string(lua_tostring(L, -1));
198198
oparams->flags = readFlagString(flagstr, flagdesc_mapgen);
199199
paramsmodified |= MGPARAMS_SET_FLAGS;
200-
200+
201201
lua_getfield(L, 1, "flagmask");
202202
if (lua_isstring(L, -1)) {
203203
flagstr = std::string(lua_tostring(L, -1));
204204
flagmask = readFlagString(flagstr, flagdesc_mapgen);
205205
}
206206
}
207-
207+
208+
delete emerge->luaoverride_params;
209+
208210
emerge->luaoverride_params = oparams;
209211
emerge->luaoverride_params_modified = paramsmodified;
210212
emerge->luaoverride_flagmask = flagmask;
211-
213+
212214
return 0;
213215
}
214216

@@ -240,7 +242,7 @@ int ModApiMapgen::l_register_biome(lua_State *L)
240242
"air");
241243
b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
242244
"mapgen_water_source");
243-
245+
244246
b->depth_top = getintfield_default(L, index, "depth_top", 1);
245247
b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
246248
b->height_min = getintfield_default(L, index, "height_min", 0);
@@ -254,7 +256,7 @@ int ModApiMapgen::l_register_biome(lua_State *L)
254256
b->c_water = CONTENT_IGNORE;
255257
b->c_dust = CONTENT_IGNORE;
256258
b->c_dust_water = CONTENT_IGNORE;
257-
259+
258260
verbosestream << "register_biome: " << b->name << std::endl;
259261
bmgr->addBiome(b);
260262

@@ -277,7 +279,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
277279
"decoration placement type";
278280
return 0;
279281
}
280-
282+
281283
Decoration *deco = createDecoration(decotype);
282284
if (!deco) {
283285
errorstream << "register_decoration: decoration placement type "
@@ -295,11 +297,11 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
295297
delete deco;
296298
return 0;
297299
}
298-
300+
299301
lua_getfield(L, index, "noise_params");
300302
deco->np = read_noiseparams(L, -1);
301303
lua_pop(L, 1);
302-
304+
303305
lua_getfield(L, index, "biomes");
304306
if (lua_istable(L, -1)) {
305307
lua_pushnil(L);
@@ -313,7 +315,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
313315
}
314316
lua_pop(L, 1);
315317
}
316-
318+
317319
switch (decotype) {
318320
case DECO_SIMPLE: {
319321
DecoSimple *dsimple = (DecoSimple *)deco;
@@ -323,7 +325,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
323325
dsimple->deco_height = getintfield_default(L, index, "height", 1);
324326
dsimple->deco_height_max = getintfield_default(L, index, "height_max", 0);
325327
dsimple->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
326-
328+
327329
lua_getfield(L, index, "decoration");
328330
if (lua_istable(L, -1)) {
329331
lua_pushnil(L);
@@ -340,7 +342,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
340342
dsimple->deco_name = std::string("air");
341343
}
342344
lua_pop(L, 1);
343-
345+
344346
if (dsimple->deco_height <= 0) {
345347
errorstream << "register_decoration: simple decoration height"
346348
" must be greater than 0" << std::endl;
@@ -380,7 +382,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
380382
return 0;
381383
}
382384
lua_pop(L, -1);
383-
385+
384386
if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
385387
errorstream << "register_decoration: failed to load schematic file '"
386388
<< dschem->filename << "'" << std::endl;
@@ -390,7 +392,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
390392
break; }
391393
case DECO_LSYSTEM: {
392394
//DecoLSystem *decolsystem = (DecoLSystem *)deco;
393-
395+
394396
break; }
395397
}
396398

@@ -474,7 +476,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L)
474476
v3s16 p1 = read_v3s16(L, 1);
475477
v3s16 p2 = read_v3s16(L, 2);
476478
sortBoxVerticies(p1, p2);
477-
479+
478480
std::vector<std::pair<v3s16, u8> > prob_list;
479481
if (lua_istable(L, 3)) {
480482
lua_pushnil(L);
@@ -483,15 +485,15 @@ int ModApiMapgen::l_create_schematic(lua_State *L)
483485
lua_getfield(L, -1, "pos");
484486
v3s16 pos = read_v3s16(L, -1);
485487
lua_pop(L, 1);
486-
488+
487489
u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
488490
prob_list.push_back(std::make_pair(pos, prob));
489491
}
490492

491493
lua_pop(L, 1);
492494
}
493495
}
494-
496+
495497
std::vector<std::pair<s16, u8> > slice_prob_list;
496498
if (lua_istable(L, 5)) {
497499
lua_pushnil(L);
@@ -516,7 +518,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L)
516518
}
517519

518520
dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
519-
521+
520522
dschem.saveSchematicFile(ndef);
521523
actionstream << "create_schematic: saved schematic file '"
522524
<< dschem.filename << "'." << std::endl;
@@ -536,11 +538,11 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
536538
v3s16 p = read_v3s16(L, 1);
537539
if (!read_schematic(L, 2, &dschem, getServer(L)))
538540
return 0;
539-
541+
540542
int rot = ROTATE_0;
541543
if (lua_isstring(L, 3))
542544
string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
543-
545+
544546
dschem.rotation = (Rotation)rot;
545547

546548
if (lua_istable(L, 4)) {
@@ -568,7 +570,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
568570
}
569571
dschem.resolveNodeNames(ndef);
570572
}
571-
573+
572574
dschem.placeStructure(map, p);
573575

574576
return 1;

0 commit comments

Comments
 (0)