diff options
author | MetaDucky <metaducky AT gmail DOT com> | 2013-11-20 22:11:57 +0100 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-11-29 23:35:16 -0500 |
commit | 5be786c804d36e9950598a01cf39f05574af2acc (patch) | |
tree | 488f9fd610b5785389c15d0faa02f840ba9e7110 /src/script/lua_api/l_nodemeta.cpp | |
parent | 747bc40840ff13bcf9c7a60b790a6de24f94f946 (diff) | |
download | minetest-5be786c804d36e9950598a01cf39f05574af2acc.tar.gz minetest-5be786c804d36e9950598a01cf39f05574af2acc.tar.bz2 minetest-5be786c804d36e9950598a01cf39f05574af2acc.zip |
Fixed potential NULL pointer and leak when setting node metadata
Diffstat (limited to 'src/script/lua_api/l_nodemeta.cpp')
-rw-r--r-- | src/script/lua_api/l_nodemeta.cpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index f9c8794d5..4f20e56f9 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -42,10 +42,12 @@ NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg) NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create) { NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p); - if(meta == NULL && auto_create) - { + if(meta == NULL && auto_create) { meta = new NodeMetadata(ref->m_env->getGameDef()); - ref->m_env->getMap().setNodeMetadata(ref->m_p, meta); + if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) { + delete meta; + return NULL; + } } return meta; } @@ -227,17 +229,21 @@ int NodeMetaRef::l_from_table(lua_State *L) NodeMetaRef *ref = checkobject(L, 1); int base = 2; + // clear old metadata first + ref->m_env->getMap().removeNodeMetadata(ref->m_p); + if(lua_isnil(L, base)){ // No metadata - ref->m_env->getMap().removeNodeMetadata(ref->m_p); lua_pushboolean(L, true); return 1; } - // Has metadata; clear old one first - ref->m_env->getMap().removeNodeMetadata(ref->m_p); // Create new metadata NodeMetadata *meta = getmeta(ref, true); + if(meta == NULL){ + lua_pushboolean(L, false); + return 1; + } // Set fields lua_getfield(L, base, "fields"); int fieldstable = lua_gettop(L); |