Skip to content

Commit 431d8a9

Browse files
HybridDognerzhul
authored andcommitted
Abort when trying to set a not registered node (#7011)
I removed the MapNode constructor which takes a nodename and gives the node's id or CONTENT_IGNORE The code which used this constructor (two places) now handles the situation of not registered nodes correctly: * minetest.set_node and similar functions make minetest crash when a not registered node is passed * reverting a node with rollback aborts if the node is not registered
1 parent 3066d76 commit 431d8a9

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

src/mapnode.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ static const u8 rot_to_wallmounted[] = {
4444
MapNode
4545
*/
4646

47-
// Create directly from a nodename
48-
// If name is unknown, sets CONTENT_IGNORE
49-
MapNode::MapNode(const NodeDefManager *ndef, const std::string &name,
50-
u8 a_param1, u8 a_param2)
51-
{
52-
content_t id = CONTENT_IGNORE;
53-
ndef->getId(name, id);
54-
param0 = id;
55-
param1 = a_param1;
56-
param2 = a_param2;
57-
}
58-
5947
void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
6048
{
6149
if (f.palette) {

src/mapnode.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@ struct MapNode
145145
param2(a_param2)
146146
{ }
147147

148-
// Create directly from a nodename
149-
// If name is unknown, sets CONTENT_IGNORE
150-
MapNode(const NodeDefManager *ndef, const std::string &name,
151-
u8 a_param1=0, u8 a_param2=0);
152-
153148
bool operator==(const MapNode &other) const noexcept
154149
{
155150
return (param0 == other.param0

src/rollback_interface.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gam
139139
return false;
140140
}
141141
// Create rollback node
142-
MapNode n(ndef, n_old.name, n_old.param1, n_old.param2);
142+
content_t id = CONTENT_IGNORE;
143+
if (!ndef->getId(n_old.name, id)) {
144+
// The old node is not registered
145+
return false;
146+
}
147+
MapNode n(id, n_old.param1, n_old.param2);
143148
// Set rollback node
144149
try {
145150
if (!map->addNodeWithEvent(p, n)) {
@@ -203,7 +208,7 @@ bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gam
203208
<< inventory_location << std::endl;
204209
return false;
205210
}
206-
211+
207212
// If item was added, take away item, otherwise add removed item
208213
if (inventory_add) {
209214
// Silently ignore different current item

src/script/common/c_content.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
10931093
lua_getfield(L, index, "name");
10941094
if (!lua_isstring(L, -1))
10951095
throw LuaError("Node name is not set or is not a string!");
1096-
const char *name = lua_tostring(L, -1);
1096+
std::string name = lua_tostring(L, -1);
10971097
lua_pop(L, 1);
10981098

10991099
u8 param1 = 0;
@@ -1108,7 +1108,11 @@ MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
11081108
param2 = lua_tonumber(L, -1);
11091109
lua_pop(L, 1);
11101110

1111-
return {ndef, name, param1, param2};
1111+
content_t id = CONTENT_IGNORE;
1112+
if (!ndef->getId(name, id))
1113+
throw LuaError("\"" + name + "\" is not a registered node!");
1114+
1115+
return {id, param1, param2};
11121116
}
11131117

11141118
/******************************************************************************/

0 commit comments

Comments
 (0)