aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorRandy Palamar <palamar@ualberta.ca>2023-07-03 00:35:29 -0600
committerRandy Palamar <palamar@ualberta.ca>2023-07-18 21:20:40 -0600
commit47ac03a911eff0f743d1ee45efc51fc8f68dcbb8 (patch)
tree94a82cef5330cde19f485419ed376b81a3cdc2d9 /vis-lua.c
parent80fbb7e1ead4d16f1f4d511d30df18eeb65ceafa (diff)
downloadvis-47ac03a911eff0f743d1ee45efc51fc8f68dcbb8.tar.gz
vis-47ac03a911eff0f743d1ee45efc51fc8f68dcbb8.tar.xz
vis:pipe(): don't segfault if vis->win isn't present
When `file` was made optional the variable was changed to be initialized by `vis->win->file` instead of a known safe file pointer. If `vis:pipe()` is called based on the event `FILE_OPEN` the `file` parameter can be valid even when `vis->win` is not yet present. Assuming `file` was provided this would be handled later on if vis didn't segfault. By initializing to NULL when `vis->win` isn't present `file` is given a chance be handled later. In the case where `file` wasn't given and `vis->win` is missing an error is thrown and `vis:pipe()` exits. fixes #1107 - Lua API: vis:pipe() causes a segfault when called before a window is open
Diffstat (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/vis-lua.c b/vis-lua.c
index e304dd7..ab7f6f4 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -1340,7 +1340,7 @@ static int pipe_func(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
int cmd_idx = 4;
char *out = NULL, *err = NULL;
- File *file = vis->win->file;
+ File *file = vis->win ? vis->win->file : NULL;
Filerange range = text_range_new(0, 0);
if (lua_gettop(L) <= 3) {
cmd_idx = 2;
@@ -1350,6 +1350,10 @@ static int pipe_func(lua_State *L) {
}
const char *cmd = luaL_checkstring(L, cmd_idx);
bool fullscreen = lua_isboolean(L, cmd_idx + 1) && lua_toboolean(L, cmd_idx + 1);
+
+ if (!file)
+ return luaL_error(L, "vis:pipe(cmd = '%s'): win not open, file can't be nil", cmd);
+
int status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err, fullscreen);
lua_pushinteger(L, status);
if (out)