Skip to content

Commit ff8e868

Browse files
committed
Correctly make vector div by float 0 in a var throw math error in LSL
And add tests so we don't forget it again :) Fixes #78
1 parent 51d1e48 commit ff8e868

7 files changed

Lines changed: 35 additions & 4 deletions

File tree

VM/src/lvmexecute.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,11 +1788,15 @@ static void luau_execute(lua_State* L)
17881788
setnvalue(ra, res);
17891789
VM_NEXT();
17901790
}
1791-
else if (ttisvector(rb) && ttisnumber(rc) && !LUAU_IS_LSL_VM(L))
1791+
else if (ttisvector(rb) && ttisnumber(rc))
17921792
{
1793-
// ServerLua: we use different logic in the LSL case.
17941793
const float* vb = vvalue(rb);
17951794
float vc = cast_to(float, nvalue(rc));
1795+
// ServerLua: In LSL (Mono), vector division by zero is a runtime error.
1796+
if (LUAU_UNLIKELY(LUAU_IS_LSL_VM(L) && vc == 0.0))
1797+
{
1798+
VM_PROTECT(luaG_runerrorL(L, MATH_ERROR_STR));
1799+
}
17961800
setvvalue(ra, vb[0] / vc, vb[1] / vc, vb[2] / vc, vb[3] / vc);
17971801
VM_NEXT();
17981802
}

tests/LSL.test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,8 +1085,11 @@ TEST_CASE("Integer MulAssign Float Valid Contexts")
10851085
TEST_CASE("Division By Zero Error")
10861086
{
10871087
runConformance("div_by_zero.lsl", nullptr, nullptr, nullptr, "Math error");
1088-
runConformance("float_div_by_zero.lsl", nullptr, nullptr, nullptr, "Math error");
1089-
runConformance("vector_div_by_zero.lsl", nullptr, nullptr, nullptr, "Math error");
1088+
runConformance("float_div_by_zero_var.lsl", nullptr, nullptr, nullptr, "Math error");
1089+
runConformance("float_div_by_zero_k.lsl", nullptr, nullptr, nullptr, "Math error");
1090+
runConformance("float_div_by_zero_k_rev.lsl", nullptr, nullptr, nullptr, "Math error");
1091+
runConformance("vector_div_by_zero_k.lsl", nullptr, nullptr, nullptr, "Math error");
1092+
runConformance("vector_div_by_zero_var.lsl", nullptr, nullptr, nullptr, "Math error");
10901093
runConformance("zero_vector_div_by_zero.lsl", nullptr, nullptr, nullptr, "Math error");
10911094
runConformance("inf_div_inf.lsl", nullptr, nullptr, nullptr, "Math error");
10921095
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test that float division by zero throws a runtime error
2+
default {
3+
state_entry() {
4+
float a = 1.0;
5+
float c = a / 0.0; // This should error
6+
print((string)c);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test that float division by zero throws a runtime error
2+
default {
3+
state_entry() {
4+
float a = 0.0;
5+
float c = 1.0 / a; // This should error
6+
print((string)c);
7+
}
8+
}
File renamed without changes.
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test that vector division by zero throws a runtime error
2+
default {
3+
state_entry() {
4+
float y = 0.0;
5+
vector v = <1, 1, 1> / y; // This should error
6+
print((string)v);
7+
}
8+
}

0 commit comments

Comments
 (0)