about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-12-16 20:53:50 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-12-16 20:53:50 -0800
commit2a6786fee53bc9be7fe9f0bccc6ae651c0db36b5 (patch)
tree32689f0a793d85875c0f9823754c9c881b9f949f /src/lua.c
parentf979002939a7a7d5f323eb1a3e583de2cab542e7 (diff)
downloadteliva-2a6786fee53bc9be7fe9f0bccc6ae651c0db36b5.tar.gz
fix another leak in the Lua stack
This fixes a segfault when scanning through a long history of recent
changes (say > 20 changes)
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/lua.c b/src/lua.c
index 5e67928..97b449e 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -518,6 +518,7 @@ void render_recent_changes (lua_State *L, int start_index) {
   attrset(A_BOLD);
   mvaddstr(1, 0, "Recent changes");
   attrset(A_NORMAL);
+  int oldtop = lua_gettop(L);
   lua_getglobal(L, "teliva_program");
   int history_array = lua_gettop(L);
   int history_array_size = luaL_getn(L, history_array);
@@ -564,13 +565,19 @@ void render_recent_changes (lua_State *L, int start_index) {
       const char *definition_contents = lua_tostring(L, -1);
       y = render_wrapped_lua_text(y, 0, COLS, definition_contents);
       y++;
-      if (y >= LINES-1) break;
+      if (y >= LINES-1) break;  /* leave cruft on the stack */
     }
+    lua_settop(L, t);  /* clean up cruft on the stack */
     lua_pop(L, 1);  // history element
     y++;
     if (y >= LINES-1) break;
   }
-  lua_pop(L, 1);
+  lua_pop(L, 1);  // history array
+  if (lua_gettop(L) != oldtop) {
+    endwin();
+    printf("render_recent_changes: memory leak %d -> %d\n", oldtop, lua_gettop(L));
+    exit(1);
+  }
   recent_changes_menu(start_index, history_array_size);
   refresh();
 }