Prevent integer overflow when computing texture sizes and allocating memory for them.

Correctly check for failed allocations when resizing std::vector

PiperOrigin-RevId: 659488680
Change-Id: I7bf4c0b416e96eda2baec32b21be78456604f4b0
This commit is contained in:
Nimrod Gileadi
2024-08-05 02:58:02 -07:00
committed by Copybara-Service
parent ff5d339e48
commit 6a8e28f0e9
2 changed files with 92 additions and 9 deletions
+46
View File
@@ -890,6 +890,52 @@ TEST_F(XMLReaderTest, MaterialTextureFailTest) {
"cannot have texture sub-elements"));
}
TEST_F(XMLReaderTest, LargeTextureTest) {
static constexpr char xml[] = R"(
<mujoco>
<asset>
<!--
Use a texture width that exceeds the maximum texture size. For cube
textures, the height is ignored and set to width*6. The default number of
channels is 3.
The width in this test is chosen so that 6*width*width*3 is too large to
be represented as an integer.
-->
<texture name="tex" builtin="gradient" width="10923" height="2"/>
</asset>
</mujoco>
)";
std::array<char, 1024> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(model, IsNull());
mj_deleteModel(model);
}
TEST_F(XMLReaderTest, HugeTextureTest) {
static constexpr char xml[] = R"(
<mujoco>
<asset>
<!--
Use a texture width that exceeds the maximum texture size. For cube
textures, the height is ignored and set to width*6. The default number of
channels is 3.
The width in this test is chosen so that 6*width*width*3 is so large that
it overflows and becomes a positive integer.
-->
<texture name="tex" builtin="gradient" width="15447" height="2"/>
</asset>
</mujoco>
)";
std::array<char, 1024> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(model, IsNull());
mj_deleteModel(model);
}
TEST_F(XMLReaderTest, IncludeAssetsTest) {
static constexpr char xml[] = R"(
<mujoco>