Skip to content

Commit aae37f8

Browse files
author
Jacob Crabill
committed
bugfix: Check for invalid widths from Lua
If the nvim APIs return an invalid buffer width, the value passed into Zig-land could be < 0, leading to an invalid integer cast.
1 parent e613f91 commit aae37f8

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/app/lua_api.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export fn render_markdown(lua: ?*LuaState) callconv(.c) c_int {
5252
// Number of columns to render (output width).
5353
// We handle the case of the user not providing the argument by defaulting to 100.
5454
const no_arg: bool = (c.lua_isnone(lua, 2) or c.lua_isnil(lua, 2));
55-
const columns: usize = if (no_arg) 100 else @intCast(c.lua_tointeger(lua, 2));
55+
const raw_cols: i64 = if (no_arg) 100 else @intCast(std.math.clamp(c.lua_tointeger(lua, 2), 10, 120));
56+
const columns: usize = if (raw_cols <= 0) 100 else @intCast(std.math.clamp(c.lua_tointeger(lua, 2), 10, 120));
5657

5758
// Parse the input text
5859
const opts = zd.parser.ParserOpts{ .copy_input = false, .verbose = false };
@@ -75,8 +76,8 @@ export fn render_markdown(lua: ?*LuaState) callconv(.c) c_int {
7576
const render_opts = zd.render.RangeRenderer.Config{
7677
.root_dir = null, // TODO opts.document_dir,
7778
.indent = 2,
78-
.width = columns,
79-
.max_image_cols = if (columns > 4) columns - 4 else columns,
79+
.width = @intCast(columns),
80+
.max_image_cols = if (columns > 4) @intCast(columns - 4) else @intCast(columns),
8081
.termsize = tsize,
8182
};
8283
var r_renderer = zd.render.RangeRenderer.init(&alloc_writer.writer, alloc, render_opts);

0 commit comments

Comments
 (0)