From 5ba70cf5efbd4ba13149f8611f18f587ba356529 Mon Sep 17 00:00:00 2001 From: savilli <78875209+savilli@users.noreply.github.com> Date: Thu, 18 May 2023 20:31:04 +0200 Subject: [PATCH] Fix crash on handling wallmounted nodes with invalid param2 (#13487) --- src/mapnode.cpp | 14 ++++++++------ src/util/directiontables.h | 35 ++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/mapnode.cpp b/src/mapnode.cpp index b130a7363..76c050e7f 100644 --- a/src/mapnode.cpp +++ b/src/mapnode.cpp @@ -64,8 +64,10 @@ u8 MapNode::getFaceDir(const NodeDefManager *nodemgr, f.param_type_2 == CPT2_COLORED_4DIR) return getParam2() & 0x03; if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED || - f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) - return wallmounted_to_facedir[getParam2() & 0x07]; + f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) { + u8 wmountface = MYMIN(getParam2() & 0x07, DWM_COUNT - 1); + return wallmounted_to_facedir[wmountface]; + } return 0; } @@ -73,9 +75,9 @@ u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const { const ContentFeatures &f = nodemgr->get(*this); if (f.param_type_2 == CPT2_WALLMOUNTED || - f.param_type_2 == CPT2_COLORED_WALLMOUNTED) { - return getParam2() & 0x07; - } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE || + f.param_type_2 == CPT2_COLORED_WALLMOUNTED) + return MYMIN(getParam2() & 0x07, DWM_COUNT - 1); + else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE || f.drawtype == NDT_PLANTLIKE || f.drawtype == NDT_PLANTLIKE_ROOTED) { return 1; @@ -160,7 +162,7 @@ void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot) } } else if (cpt2 == CPT2_WALLMOUNTED || cpt2 == CPT2_COLORED_WALLMOUNTED) { - u8 wmountface = (param2 & 7); + u8 wmountface = MYMIN(param2 & 0x07, DWM_COUNT - 1); if (wmountface <= 1) return; diff --git a/src/util/directiontables.h b/src/util/directiontables.h index 3883a6e37..105a0ef27 100644 --- a/src/util/directiontables.h +++ b/src/util/directiontables.h @@ -22,23 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes.h" #include "irr_v3d.h" -extern const v3s16 g_6dirs[6]; - -extern const v3s16 g_7dirs[7]; - -extern const v3s16 g_26dirs[26]; - -// 26th is (0,0,0) -extern const v3s16 g_27dirs[27]; - -extern const u8 wallmounted_to_facedir[6]; - -extern const v3s16 wallmounted_dirs[8]; - -extern const v3s16 facedir_dirs[32]; - -extern const v3s16 fourdir_dirs[4]; - /// Direction in the 6D format. g_27dirs contains corresponding vectors. /// Here P means Positive, N stands for Negative. enum Direction6D { @@ -92,4 +75,22 @@ enum DirectionWallmounted { DWM_XN, DWM_ZP, DWM_ZN, + DWM_COUNT, }; + +extern const v3s16 g_6dirs[DWM_COUNT]; + +extern const v3s16 g_7dirs[7]; + +extern const v3s16 g_26dirs[26]; + +// 26th is (0,0,0) +extern const v3s16 g_27dirs[27]; + +extern const u8 wallmounted_to_facedir[6]; + +extern const v3s16 wallmounted_dirs[8]; + +extern const v3s16 facedir_dirs[32]; + +extern const v3s16 fourdir_dirs[4];