From bf186d81b1ac66295d848cd32e376dfa9da3687e Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Sun, 2 Jun 2024 04:20:34 -0400 Subject: [PATCH] Avoid a crash in `drawMeshNode()` after reading out of array * backported from https://github.com/MultiCraft/MultiCraft/commit/b201316aed364e51020b86603b8601f66dee6d4d --- src/client/content_mapblock.cpp | 9 +++++++-- src/mapnode.cpp | 2 +- src/util/directiontables.cpp | 8 ++++++++ src/util/directiontables.h | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/client/content_mapblock.cpp b/src/client/content_mapblock.cpp index 4e900ac7c..3afe50d4f 100644 --- a/src/client/content_mapblock.cpp +++ b/src/client/content_mapblock.cpp @@ -1420,10 +1420,15 @@ void MapblockMeshGenerator::drawMeshNode() // Convert wallmounted to 6dfacedir. // When cache enabled, it is already converted. facedir = n.getWallMounted(nodedef); - if (!enable_mesh_cache) - facedir = wallmounted_to_facedir[facedir]; + if (!enable_mesh_cache) { + facedir = wallmountedToFacedir(facedir); + } } + // f->mesh_ptr has 24 elements + if (facedir > 23) + facedir = 0; + if (!data->m_smooth_lighting && f->mesh_ptr[facedir]) { // use cached meshes private_mesh = false; diff --git a/src/mapnode.cpp b/src/mapnode.cpp index 57c0cb0bc..edb97db8e 100644 --- a/src/mapnode.cpp +++ b/src/mapnode.cpp @@ -151,7 +151,7 @@ u8 MapNode::getFaceDir(const NodeDefManager *nodemgr, return (getParam2() & 0x1F) % 24; if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED || f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) - return wallmounted_to_facedir[getParam2() & 0x07]; + return wallmountedToFacedir(getParam2() & 0x07); return 0; } diff --git a/src/util/directiontables.cpp b/src/util/directiontables.cpp index 9eb13b3e6..0579d6fb0 100644 --- a/src/util/directiontables.cpp +++ b/src/util/directiontables.cpp @@ -118,3 +118,11 @@ const u8 wallmounted_to_facedir[6] = { 8, 4 + 2 }; + +const u8 wallmountedToFacedir(u8 index) +{ + if (index > 5) + return 0; + else + return wallmounted_to_facedir[index]; +} diff --git a/src/util/directiontables.h b/src/util/directiontables.h index f3773b4ae..240fa5633 100644 --- a/src/util/directiontables.h +++ b/src/util/directiontables.h @@ -31,7 +31,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 u8 wallmountedToFacedir(u8 index); /// Direction in the 6D format. g_27dirs contains corresponding vectors. /// Here P means Positive, N stands for Negative.