Prevent invalid memory access under failure conditions

This commit is contained in:
kwolekr 2014-10-28 00:18:53 -04:00
parent 6ddf458504
commit bc28ca0636
3 changed files with 64 additions and 61 deletions

View File

@ -30,7 +30,8 @@ NoiseParams nparams_biome_def_heat(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.
NoiseParams nparams_biome_def_humidity(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55); NoiseParams nparams_biome_def_humidity(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55);
BiomeDefManager::BiomeDefManager(NodeResolver *resolver) { BiomeDefManager::BiomeDefManager(NodeResolver *resolver)
{
biome_registration_finished = false; biome_registration_finished = false;
np_heat = &nparams_biome_def_heat; np_heat = &nparams_biome_def_heat;
np_humidity = &nparams_biome_def_humidity; np_humidity = &nparams_biome_def_humidity;
@ -58,7 +59,8 @@ BiomeDefManager::BiomeDefManager(NodeResolver *resolver) {
} }
BiomeDefManager::~BiomeDefManager() { BiomeDefManager::~BiomeDefManager()
{
//if (biomecache) //if (biomecache)
// delete[] biomecache; // delete[] biomecache;
@ -67,7 +69,8 @@ BiomeDefManager::~BiomeDefManager() {
} }
Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) { Biome *BiomeDefManager::createBiome(BiomeTerrainType btt)
{
/*switch (btt) { /*switch (btt) {
case BIOME_TERRAIN_NORMAL: case BIOME_TERRAIN_NORMAL:
return new Biome; return new Biome;
@ -86,7 +89,8 @@ Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) {
// just a PoC, obviously needs optimization later on (precalculate this) // just a PoC, obviously needs optimization later on (precalculate this)
void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) { void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map)
{
int i = 0; int i = 0;
for (int y = 0; y != input->mapsize.Y; y++) { for (int y = 0; y != input->mapsize.Y; y++) {
for (int x = 0; x != input->mapsize.X; x++, i++) { for (int x = 0; x != input->mapsize.X; x++, i++) {
@ -98,29 +102,31 @@ void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) {
} }
void BiomeDefManager::addBiome(Biome *b) { bool BiomeDefManager::addBiome(Biome *b)
{
if (biome_registration_finished) { if (biome_registration_finished) {
errorstream << "BIomeDefManager: biome registration already " errorstream << "BiomeDefManager: biome registration already "
"finished, dropping " << b->name << std::endl; "finished, dropping " << b->name << std::endl;
delete b; return false;
return;
} }
size_t nbiomes = biomes.size(); size_t nbiomes = biomes.size();
if (nbiomes >= 0xFF) { if (nbiomes >= 0xFF) {
errorstream << "BiomeDefManager: too many biomes, dropping " errorstream << "BiomeDefManager: too many biomes, dropping "
<< b->name << std::endl; << b->name << std::endl;
delete b; return false;
return;
} }
b->id = (u8)nbiomes; b->id = (u8)nbiomes;
biomes.push_back(b); biomes.push_back(b);
verbosestream << "BiomeDefManager: added biome " << b->name << std::endl; verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
return true;
} }
Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) { Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y)
{
Biome *b, *biome_closest = NULL; Biome *b, *biome_closest = NULL;
float dist_min = FLT_MAX; float dist_min = FLT_MAX;
@ -143,7 +149,8 @@ Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) {
} }
u8 BiomeDefManager::getBiomeIdByName(const char *name) { u8 BiomeDefManager::getBiomeIdByName(const char *name)
{
for (size_t i = 0; i != biomes.size(); i++) { for (size_t i = 0; i != biomes.size(); i++) {
if (!strcasecmp(name, biomes[i]->name.c_str())) if (!strcasecmp(name, biomes[i]->name.c_str()))
return i; return i;

View File

@ -44,24 +44,16 @@ public:
u8 id; u8 id;
std::string name; std::string name;
u32 flags; u32 flags;
/*
std::string nname_top;
std::string nname_filler;
std::string nname_water;
std::string nname_dust;
std::string nname_dust_water;
*/
content_t c_top; content_t c_top;
content_t c_filler; content_t c_filler;
content_t c_water; content_t c_water;
content_t c_dust; content_t c_dust;
content_t c_dust_water; content_t c_dust_water;
s16 depth_top; s16 depth_top;
s16 depth_filler; s16 depth_filler;
s16 height_min; s16 height_min;
s16 height_max; s16 height_max;
float heat_point; float heat_point;
@ -85,15 +77,14 @@ public:
BiomeDefManager(NodeResolver *resolver); BiomeDefManager(NodeResolver *resolver);
~BiomeDefManager(); ~BiomeDefManager();
Biome *createBiome(BiomeTerrainType btt); Biome *createBiome(BiomeTerrainType btt);
void calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map); void calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map);
Biome *getBiome(float heat, float humidity, s16 y); Biome *getBiome(float heat, float humidity, s16 y);
void addBiome(Biome *b); bool addBiome(Biome *b);
void resolveNodeNames(INodeDefManager *ndef);
u8 getBiomeIdByName(const char *name); u8 getBiomeIdByName(const char *name);
s16 calcBlockHeat(v3s16 p, u64 seed, float timeofday, float totaltime); s16 calcBlockHeat(v3s16 p, u64 seed, float timeofday, float totaltime);
s16 calcBlockHumidity(v3s16 p, u64 seed, float timeofday, float totaltime); s16 calcBlockHumidity(v3s16 p, u64 seed, float timeofday, float totaltime);
}; };

View File

@ -142,7 +142,8 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
// emerged max pos // emerged max pos
push_v3s16(L, vm->m_area.MaxEdge); push_v3s16(L, vm->m_area.MaxEdge);
return 3; } return 3;
}
case MGOBJ_HEIGHTMAP: { case MGOBJ_HEIGHTMAP: {
if (!mg->heightmap) if (!mg->heightmap)
return 0; return 0;
@ -153,7 +154,8 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
return 1; } return 1;
}
case MGOBJ_BIOMEMAP: { case MGOBJ_BIOMEMAP: {
if (!mg->biomemap) if (!mg->biomemap)
return 0; return 0;
@ -164,7 +166,8 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
return 1; } return 1;
}
case MGOBJ_HEATMAP: { // Mapgen V7 specific objects case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
case MGOBJ_HUMIDMAP: case MGOBJ_HUMIDMAP:
if (strcmp(emerge->params.mg_name.c_str(), "v7")) if (strcmp(emerge->params.mg_name.c_str(), "v7"))
@ -183,7 +186,8 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
return 1; } return 1;
}
case MGOBJ_GENNOTIFY: { case MGOBJ_GENNOTIFY: {
lua_newtable(L); lua_newtable(L);
for (int i = 0; flagdesc_gennotify[i].name; i++) { for (int i = 0; flagdesc_gennotify[i].name; i++) {
@ -204,7 +208,8 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
posvec->clear(); posvec->clear();
} }
return 1; } return 1;
}
} }
return 0; return 0;
@ -302,15 +307,26 @@ int ModApiMapgen::l_register_biome(lua_State *L)
NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef; BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
if (!bmgr) {
verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
return 0;
}
enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index, enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
"terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL); "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
Biome *b = bmgr->createBiome(terrain); Biome *b = bmgr->createBiome(terrain);
b->name = getstringfield_default(L, index, "name", "<no name>");
b->depth_top = getintfield_default(L, index, "depth_top", 1);
b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
b->height_min = getintfield_default(L, index, "height_min", 0);
b->height_max = getintfield_default(L, index, "height_max", 0);
b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
b->flags = 0; //reserved
if (!bmgr->addBiome(b)) {
delete b;
return 0;
}
// Pend node resolutions only if insertion succeeded
resolver->addNode(getstringfield_default(L, index, "node_top", ""), resolver->addNode(getstringfield_default(L, index, "node_top", ""),
"mapgen_dirt_with_grass", CONTENT_AIR, &b->c_top); "mapgen_dirt_with_grass", CONTENT_AIR, &b->c_top);
resolver->addNode(getstringfield_default(L, index, "node_filler", ""), resolver->addNode(getstringfield_default(L, index, "node_filler", ""),
@ -322,17 +338,7 @@ int ModApiMapgen::l_register_biome(lua_State *L)
resolver->addNode(getstringfield_default(L, index, "node_dust_water", ""), resolver->addNode(getstringfield_default(L, index, "node_dust_water", ""),
"mapgen_water_source", CONTENT_IGNORE, &b->c_dust_water); "mapgen_water_source", CONTENT_IGNORE, &b->c_dust_water);
b->name = getstringfield_default(L, index, "name", "<no name>");
b->depth_top = getintfield_default(L, index, "depth_top", 1);
b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
b->height_min = getintfield_default(L, index, "height_min", 0);
b->height_max = getintfield_default(L, index, "height_max", 0);
b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
b->flags = 0; //reserved
verbosestream << "register_biome: " << b->name << std::endl; verbosestream << "register_biome: " << b->name << std::endl;
bmgr->addBiome(b);
return 0; return 0;
} }
@ -496,9 +502,6 @@ int ModApiMapgen::l_register_ore(lua_State *L)
return 0; return 0;
} }
resolver->addNode(getstringfield_default(L, index, "ore", ""),
"", CONTENT_AIR, &ore->c_ore);
ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0); ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1); ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1); ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
@ -506,19 +509,8 @@ int ModApiMapgen::l_register_ore(lua_State *L)
ore->height_min = getintfield_default(L, index, "height_min", 0); ore->height_min = getintfield_default(L, index, "height_min", 0);
ore->height_max = getintfield_default(L, index, "height_max", 0); ore->height_max = getintfield_default(L, index, "height_max", 0);
ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.); ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
ore->noise = NULL;
ore->flags = 0; ore->flags = 0;
getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
std::vector<const char *> wherein_names;
getstringlistfield(L, index, "wherein", wherein_names);
for (size_t i = 0; i != wherein_names.size(); i++)
resolver->addNodeList(wherein_names[i], &ore->c_wherein);
lua_getfield(L, index, "noise_params");
ore->np = read_noiseparams(L, -1);
lua_pop(L, 1);
ore->noise = NULL;
if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) { if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
errorstream << "register_ore: clust_scarcity and clust_num_ores" errorstream << "register_ore: clust_scarcity and clust_num_ores"
@ -527,6 +519,20 @@ int ModApiMapgen::l_register_ore(lua_State *L)
return 0; return 0;
} }
getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
lua_getfield(L, index, "noise_params");
ore->np = read_noiseparams(L, -1);
lua_pop(L, 1);
std::vector<const char *> wherein_names;
getstringlistfield(L, index, "wherein", wherein_names);
for (size_t i = 0; i != wherein_names.size(); i++)
resolver->addNodeList(wherein_names[i], &ore->c_wherein);
resolver->addNode(getstringfield_default(L, index, "ore", ""),
"", CONTENT_AIR, &ore->c_ore);
emerge->ores.push_back(ore); emerge->ores.push_back(ore);
//verbosestream << "register_ore: ore '" << ore->ore_name //verbosestream << "register_ore: ore '" << ore->ore_name
@ -595,7 +601,6 @@ int ModApiMapgen::l_create_schematic(lua_State *L)
return 1; return 1;
} }
// place_schematic(p, schematic, rotation, replacement) // place_schematic(p, schematic, rotation, replacement)
int ModApiMapgen::l_place_schematic(lua_State *L) int ModApiMapgen::l_place_schematic(lua_State *L)
{ {