Skip to content

Commit

Permalink
feat: always check stack size when popping
Browse files Browse the repository at this point in the history
  • Loading branch information
gudzpoz committed Jun 28, 2024
1 parent 6817d5c commit 9eea382
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ private void testStackOperations() {
try (Lua L = constructor.get()) {
assertEquals(0, L.getTop());
assertEquals(0, L.newThread().getTop());
assertThrowsLua(LuaError.MEMORY, () -> L.pop(-1), "invalid number of items to pop");
assertThrowsLua(LuaError.MEMORY, () -> L.pop(10), "invalid number of items to pop");
L.setTop(0);
assertThrowsLua(LuaError.MEMORY, () -> L.pop(1), "invalid number of items to pop");
}

Lua sub = L.newThread();
Expand Down
6 changes: 6 additions & 0 deletions luajava/src/main/java/party/iroiro/luajava/AbstractLua.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ public void insert(int index) {

@Override
public void pop(int n) {
if (n < 0 || getTop() < n) {
throw new LuaException(
LuaException.LuaError.MEMORY,
"invalid number of items to pop"
);
}
C.lua_pop(L, n);
}

Expand Down

0 comments on commit 9eea382

Please sign in to comment.