diff --git a/src/user/user_flexcomp.cc b/src/user/user_flexcomp.cc
index 50c12d68..1d0dbece 100644
--- a/src/user/user_flexcomp.cc
+++ b/src/user/user_flexcomp.cc
@@ -1167,6 +1167,11 @@ void mjCFlexcomp::LoadGMSH41(char* buffer, int binary, int nodeend,
throw mjCError(NULL, "All nodes must be in single block");
}
+ // require maximum number of nodes be equal to maximum number of nodes in a block
+ if (maxNodeTag != numNodesInBlock){
+ throw mjCError(NULL, "Maximum number of nodes must be equal to number of nodes in a block");
+ }
+
// check dimensionality and save
if (entityDim < 1 || entityDim > 3) {
throw mjCError(NULL, "Entity must be 1D, 2D or 3D");
diff --git a/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.msh b/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.msh
new file mode 100644
index 00000000..7f88379f
--- /dev/null
+++ b/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.msh
@@ -0,0 +1,67 @@
+$MeshFormat
+4.1 0 8
+$EndMeshFormat
+$Entities
+0 0 0 1
+1 -0.5 -0.5 0 0.5 0.5 1 0 0
+$EndEntities
+$Nodes
+1 14 1 100
+3 1 0 14
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+-0.5 -0.5 0
+0.5 -0.5 0
+0.5 0.5 0
+-0.5 0.5 0
+-0.5 -0.5 1
+0.5 -0.5 1
+0.5 0.5 1
+-0.5 0.5 1
+0 0 0
+0 -0.5 0.5
+0.5 0 0.5
+0 0.5 0.5
+-0.5 0 0.5
+0 0 1
+$EndNodes
+$Elements
+1 24 1 24
+3 1 4 24
+1 10 9 12 11
+2 10 12 13 14
+3 9 10 12 13
+4 10 12 14 11
+5 9 10 1 2
+6 7 11 12 3
+7 4 1 9 13
+8 13 12 4 8
+9 1 10 13 5
+10 13 8 5 14
+11 12 8 14 7
+12 14 5 10 6
+13 14 6 11 7
+14 9 4 12 3
+15 3 11 9 2
+16 11 6 10 2
+17 6 10 14 11
+18 1 10 9 13
+19 12 4 9 13
+20 5 10 13 14
+21 8 13 12 14
+22 12 14 11 7
+23 9 12 11 3
+24 10 9 11 2
+$EndElements
diff --git a/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.xml b/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.xml
new file mode 100644
index 00000000..1e179a81
--- /dev/null
+++ b/test/user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.xml
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/test/user/user_flex_test.cc b/test/user/user_flex_test.cc
index 67dfb8f6..a43b93f0 100644
--- a/test/user/user_flex_test.cc
+++ b/test/user/user_flex_test.cc
@@ -697,6 +697,18 @@ TEST_F(UserFlexTest, LoadMSHASCII_41_MissingElement_Fail) {
mj_deleteModel(m);
}
+TEST_F(UserFlexTest,
+ LoadMSHASCII_41_MismatchBetweenMaxNodesAndNodesInBlock_Fail) {
+ const std::string xml_path =
+ GetTestDataFilePath(
+ "user/testdata/malformed_cube_41_ascii_mismatch_between_max_nodes_and_nodes_in_block.xml");
+ std::array error;
+ mjModel* m = mj_loadXML(xml_path.c_str(), 0, error.data(), error.size());
+ EXPECT_THAT(error.data(), HasSubstr(
+ "XML Error: Error: Maximum number of nodes must be equal to number of nodes in a block\nElement 'flexcomp', line 22\n"));
+ mj_deleteModel(m);
+}
+
TEST_F(UserFlexTest, LoadMSHASCII_22_MissingNumNodes_Fail) {
const std::string xml_path =
GetTestDataFilePath(