Rust solves this. (Fix use-after-free bug in Lua string handling)
Browse files
+20 −13 src/config/lua.zig
| @@ -743,7 +743,7 @@ fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 743 | 743 | fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int { |
| 744 | 744 | const cfg = config orelse return 0; |
| 745 | 745 | const s = state orelse return 0; |
| 746 | - if (get_string_arg(s, 1)) |font| { | |
| 746 | + if (dupe_lua_string(s, 1)) |font| { | |
| 747 | 747 | cfg.font = font; |
| 748 | 748 | } |
| 749 | 749 | return 0; |
| @@ -784,7 +784,7 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | ||
| 784 | 784 | c.lua_settop(state, -2); |
| 785 | 785 | |
| 786 | 786 | _ = c.lua_getfield(state, idx, "format"); |
| 787 | - const format = get_lua_string(state, -1) orelse ""; | |
| 787 | + const format = dupe_lua_string(state, -1) orelse ""; | |
| 788 | 788 | c.lua_settop(state, -2); |
| 789 | 789 | |
| 790 | 790 | _ = c.lua_getfield(state, idx, "interval"); |
| @@ -812,17 +812,17 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | ||
| 812 | 812 | } else if (std.mem.eql(u8, block_type_str, "DateTime")) { |
| 813 | 813 | block.block_type = .datetime; |
| 814 | 814 | _ = c.lua_getfield(state, idx, "__arg"); |
| 815 | - block.datetime_format = get_lua_string(state, -1); | |
| 815 | + block.datetime_format = dupe_lua_string(state, -1); | |
| 816 | 816 | c.lua_settop(state, -2); |
| 817 | 817 | } else if (std.mem.eql(u8, block_type_str, "Shell")) { |
| 818 | 818 | block.block_type = .shell; |
| 819 | 819 | _ = c.lua_getfield(state, idx, "__arg"); |
| 820 | - block.command = get_lua_string(state, -1); | |
| 820 | + block.command = dupe_lua_string(state, -1); | |
| 821 | 821 | c.lua_settop(state, -2); |
| 822 | 822 | } else if (std.mem.eql(u8, block_type_str, "Static")) { |
| 823 | 823 | block.block_type = .static; |
| 824 | 824 | _ = c.lua_getfield(state, idx, "__arg"); |
| 825 | - if (get_lua_string(state, -1)) |text| { | |
| 825 | + if (dupe_lua_string(state, -1)) |text| { | |
| 826 | 826 | block.format = text; |
| 827 | 827 | } |
| 828 | 828 | c.lua_settop(state, -2); |
| @@ -831,19 +831,19 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | ||
| 831 | 831 | _ = c.lua_getfield(state, idx, "__arg"); |
| 832 | 832 | if (c.lua_type(state, -1) == c.LUA_TTABLE) { |
| 833 | 833 | _ = c.lua_getfield(state, -1, "charging"); |
| 834 | - block.format_charging = get_lua_string(state, -1); | |
| 834 | + block.format_charging = dupe_lua_string(state, -1); | |
| 835 | 835 | c.lua_settop(state, -2); |
| 836 | 836 | |
| 837 | 837 | _ = c.lua_getfield(state, -1, "discharging"); |
| 838 | - block.format_discharging = get_lua_string(state, -1); | |
| 838 | + block.format_discharging = dupe_lua_string(state, -1); | |
| 839 | 839 | c.lua_settop(state, -2); |
| 840 | 840 | |
| 841 | 841 | _ = c.lua_getfield(state, -1, "full"); |
| 842 | - block.format_full = get_lua_string(state, -1); | |
| 842 | + block.format_full = dupe_lua_string(state, -1); | |
| 843 | 843 | c.lua_settop(state, -2); |
| 844 | 844 | |
| 845 | 845 | _ = c.lua_getfield(state, -1, "battery_name"); |
| 846 | - block.battery_name = get_lua_string(state, -1); | |
| 846 | + block.battery_name = dupe_lua_string(state, -1); | |
| 847 | 847 | c.lua_settop(state, -2); |
| 848 | 848 | } |
| 849 | 849 | c.lua_settop(state, -2); |
| @@ -988,7 +988,7 @@ fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]co | ||
| 988 | 988 | fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 989 | 989 | const cfg = config orelse return 0; |
| 990 | 990 | const s = state orelse return 0; |
| 991 | - if (get_string_arg(s, 1)) |term| { | |
| 991 | + if (dupe_lua_string(s, 1)) |term| { | |
| 992 | 992 | cfg.terminal = term; |
| 993 | 993 | } |
| 994 | 994 | return 0; |
| @@ -1025,7 +1025,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 1025 | 1025 | var i: usize = 0; |
| 1026 | 1026 | while (i < len and i < 9) : (i += 1) { |
| 1027 | 1027 | _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); |
| 1028 | - if (get_lua_string(s, -1)) |tag_str| { | |
| 1028 | + if (dupe_lua_string(s, -1)) |tag_str| { | |
| 1029 | 1029 | cfg.tags[i] = tag_str; |
| 1030 | 1030 | } |
| 1031 | 1031 | c.lua_settop(s, -2); |
| @@ -1037,7 +1037,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 1037 | 1037 | fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1038 | 1038 | const cfg = config orelse return 0; |
| 1039 | 1039 | const s = state orelse return 0; |
| 1040 | - if (get_string_arg(s, 1)) |cmd| { | |
| 1040 | + if (dupe_lua_string(s, 1)) |cmd| { | |
| 1041 | 1041 | cfg.add_autostart(cmd) catch return 0; |
| 1042 | 1042 | } |
| 1043 | 1043 | return 0; |
| @@ -1054,7 +1054,7 @@ fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 1054 | 1054 | const cfg = config orelse return 0; |
| 1055 | 1055 | const s = state orelse return 0; |
| 1056 | 1056 | const name = get_string_arg(s, 1) orelse return 0; |
| 1057 | - const symbol = get_string_arg(s, 2) orelse return 0; | |
| 1057 | + const symbol = dupe_lua_string(s, 2) orelse return 0; | |
| 1058 | 1058 | |
| 1059 | 1059 | const layout_map = .{ |
| 1060 | 1060 | .{ "tiling", &cfg.layout_tile_symbol }, |
| @@ -1158,6 +1158,13 @@ fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 { | ||
| 1158 | 1158 | return std.mem.span(cstr); |
| 1159 | 1159 | } |
| 1160 | 1160 | |
| 1161 | +fn dupe_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 { | |
| 1162 | + const cfg = config orelse return null; | |
| 1163 | + const lua_str = get_lua_string(state, idx) orelse return null; | |
| 1164 | + const duped = cfg.allocator.dupe(u8, lua_str) catch return null; | |
| 1165 | + return duped; | |
| 1166 | +} | |
| 1167 | + | |
| 1161 | 1168 | fn parse_color(state: *c.lua_State, idx: c_int) u32 { |
| 1162 | 1169 | const lua_type = c.lua_type(state, idx); |
| 1163 | 1170 | if (lua_type == c.LUA_TNUMBER) { |