]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_nodemeta.cpp
Fix use-after-free in node meta cleanup
[minetest.git] / src / script / lua_api / l_nodemeta.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "lua_api/l_nodemeta.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_inventory.h"
23 #include "common/c_content.h"
24 #include "serverenvironment.h"
25 #include "map.h"
26 #include "mapblock.h"
27 #include "server.h"
28
29 /*
30         NodeMetaRef
31 */
32 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
33 {
34         luaL_checktype(L, narg, LUA_TUSERDATA);
35         void *ud = luaL_checkudata(L, narg, className);
36         if(!ud) luaL_typerror(L, narg, className);
37         return *(NodeMetaRef**)ud;  // unbox pointer
38 }
39
40 Metadata* NodeMetaRef::getmeta(bool auto_create)
41 {
42         if (m_is_local)
43                 return m_local_meta;
44
45         NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
46         if (meta == NULL && auto_create) {
47                 meta = new NodeMetadata(m_env->getGameDef()->idef());
48                 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
49                         delete meta;
50                         return NULL;
51                 }
52         }
53         return meta;
54 }
55
56 void NodeMetaRef::clearMeta()
57 {
58         SANITY_CHECK(!m_is_local);
59         m_env->getMap().removeNodeMetadata(m_p);
60 }
61
62 void NodeMetaRef::reportMetadataChange(const std::string *name)
63 {
64         SANITY_CHECK(!m_is_local);
65         // Inform other things that the metadata has changed
66         NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
67
68         // If the metadata is now empty, get rid of it
69         if (meta && meta->empty()) {
70                 clearMeta();
71                 meta = nullptr;
72         }
73
74         MapEditEvent event;
75         event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
76         event.p = m_p;
77         event.is_private_change = name && meta && meta->isPrivate(*name);
78         m_env->getMap().dispatchEvent(event);
79 }
80
81 // Exported functions
82
83 // garbage collector
84 int NodeMetaRef::gc_object(lua_State *L) {
85         NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
86         delete o;
87         return 0;
88 }
89
90 // get_inventory(self)
91 int NodeMetaRef::l_get_inventory(lua_State *L)
92 {
93         MAP_LOCK_REQUIRED;
94
95         NodeMetaRef *ref = checkobject(L, 1);
96         ref->getmeta(true);  // try to ensure the metadata exists
97
98         InventoryLocation loc;
99         loc.setNodeMeta(ref->m_p);
100         InvRef::create(L, loc);
101         return 1;
102 }
103
104 // mark_as_private(self, <string> or {<string>, <string>, ...})
105 int NodeMetaRef::l_mark_as_private(lua_State *L)
106 {
107         MAP_LOCK_REQUIRED;
108
109         NodeMetaRef *ref = checkobject(L, 1);
110         NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
111         assert(meta);
112
113         if (lua_istable(L, 2)) {
114                 lua_pushnil(L);
115                 while (lua_next(L, 2) != 0) {
116                         // key at index -2 and value at index -1
117                         luaL_checktype(L, -1, LUA_TSTRING);
118                         meta->markPrivate(readParam<std::string>(L, -1), true);
119                         // removes value, keeps key for next iteration
120                         lua_pop(L, 1);
121                 }
122         } else if (lua_isstring(L, 2)) {
123                 meta->markPrivate(readParam<std::string>(L, 2), true);
124         }
125         ref->reportMetadataChange();
126
127         return 0;
128 }
129
130 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
131 {
132         // fields
133         MetaDataRef::handleToTable(L, _meta);
134
135         NodeMetadata *meta = (NodeMetadata *) _meta;
136
137         // inventory
138         Inventory *inv = meta->getInventory();
139         if (inv) {
140                 push_inventory_lists(L, *inv);
141         } else {
142                 lua_newtable(L);
143         }
144         lua_setfield(L, -2, "inventory");
145 }
146
147 // from_table(self, table)
148 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
149 {
150         // fields
151         if (!MetaDataRef::handleFromTable(L, table, _meta))
152                 return false;
153
154         NodeMetadata *meta = (NodeMetadata*) _meta;
155
156         // inventory
157         Inventory *inv = meta->getInventory();
158         lua_getfield(L, table, "inventory");
159         if (lua_istable(L, -1)) {
160                 int inventorytable = lua_gettop(L);
161                 lua_pushnil(L);
162                 while (lua_next(L, inventorytable) != 0) {
163                         // key at index -2 and value at index -1
164                         std::string name = luaL_checkstring(L, -2);
165                         read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
166                         lua_pop(L, 1); // Remove value, keep key for next iteration
167                 }
168                 lua_pop(L, 1);
169         }
170
171         return true;
172 }
173
174
175 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
176         m_p(p),
177         m_env(env)
178 {
179 }
180
181 NodeMetaRef::NodeMetaRef(Metadata *meta):
182         m_is_local(true),
183         m_local_meta(meta)
184 {
185 }
186
187 // Creates an NodeMetaRef and leaves it on top of stack
188 // Not callable from Lua; all references are created on the C side.
189 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
190 {
191         NodeMetaRef *o = new NodeMetaRef(p, env);
192         //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
193         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
194         luaL_getmetatable(L, className);
195         lua_setmetatable(L, -2);
196 }
197
198 // Client-sided version of the above
199 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
200 {
201         NodeMetaRef *o = new NodeMetaRef(meta);
202         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
203         luaL_getmetatable(L, className);
204         lua_setmetatable(L, -2);
205 }
206
207 const char NodeMetaRef::className[] = "NodeMetaRef";
208 void NodeMetaRef::RegisterCommon(lua_State *L)
209 {
210         lua_newtable(L);
211         int methodtable = lua_gettop(L);
212         luaL_newmetatable(L, className);
213         int metatable = lua_gettop(L);
214
215         lua_pushliteral(L, "__metatable");
216         lua_pushvalue(L, methodtable);
217         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
218
219         lua_pushliteral(L, "metadata_class");
220         lua_pushlstring(L, className, strlen(className));
221         lua_settable(L, metatable);
222
223         lua_pushliteral(L, "__index");
224         lua_pushvalue(L, methodtable);
225         lua_settable(L, metatable);
226
227         lua_pushliteral(L, "__gc");
228         lua_pushcfunction(L, gc_object);
229         lua_settable(L, metatable);
230
231         lua_pushliteral(L, "__eq");
232         lua_pushcfunction(L, l_equals);
233         lua_settable(L, metatable);
234
235         lua_pop(L, 1);  // drop metatable
236 }
237
238 void NodeMetaRef::Register(lua_State *L)
239 {
240         RegisterCommon(L);
241         luaL_register(L, nullptr, methodsServer);  // fill methodtable
242         lua_pop(L, 1);  // drop methodtable
243 }
244
245
246 const luaL_Reg NodeMetaRef::methodsServer[] = {
247         luamethod(MetaDataRef, contains),
248         luamethod(MetaDataRef, get),
249         luamethod(MetaDataRef, get_string),
250         luamethod(MetaDataRef, set_string),
251         luamethod(MetaDataRef, get_int),
252         luamethod(MetaDataRef, set_int),
253         luamethod(MetaDataRef, get_float),
254         luamethod(MetaDataRef, set_float),
255         luamethod(MetaDataRef, to_table),
256         luamethod(MetaDataRef, from_table),
257         luamethod(NodeMetaRef, get_inventory),
258         luamethod(NodeMetaRef, mark_as_private),
259         luamethod(MetaDataRef, equals),
260         {0,0}
261 };
262
263
264 void NodeMetaRef::RegisterClient(lua_State *L)
265 {
266         RegisterCommon(L);
267         luaL_register(L, nullptr, methodsClient);  // fill methodtable
268         lua_pop(L, 1);  // drop methodtable
269 }
270
271
272 const luaL_Reg NodeMetaRef::methodsClient[] = {
273         luamethod(MetaDataRef, contains),
274         luamethod(MetaDataRef, get),
275         luamethod(MetaDataRef, get_string),
276         luamethod(MetaDataRef, get_int),
277         luamethod(MetaDataRef, get_float),
278         luamethod(MetaDataRef, to_table),
279         {0,0}
280 };