Skip to content

Commit

Permalink
lua: Load bit and rex_pcre2 as though through require
Browse files Browse the repository at this point in the history
Use luaL_requiref to load the bit and rex_pcre2 modules, which
adds them to the package.loaded table as though through require,
as Lua does with its included standard libraries.

This causes `require("bit")` and `require("rex_pcre2")` statements
in Lua dissectors to behave as expected, if albeit usually unecessary.
The tables are still added to the global environment as before.

Fix #20213
  • Loading branch information
johnthacker authored and AndersBroman committed Nov 14, 2024
1 parent 5e277cb commit 0488f39
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions doc/release-notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ The following features are either new or have been significantly updated since v
* DNP 3 (Distributed Network Protocol 3) is now supported in the Conversations
and Endpoints table dialogs.

* The Lua supplied preloaded libraries `bit` and `rex_pcre2` are loaded in
a way that adds them to the `package.loaded` table, as though through
`require`, so that `require("bit")` and `require("rex_pcre2")` statements
in Lua dissectors, while usually superfluous, behave as expected. wsbuglink:20213[]

* The Windows installers now ship with Npcap 1.80.
They previously shipped with Npcap 1.79.

Expand Down
10 changes: 6 additions & 4 deletions doc/wsdg_src/wsdg_lua_support.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ in replacement for code written to use that library by simply replacing a
`bit32 = require("bit32")` statement with `bit32 = bit`.

[NOTE]
The library is already loaded in the global environment as the `bit` table;
Lua dissectors should not contain a `require("bit")` statement, which will give an
error (unless another copy of the BitOp library is also accessible to the system Lua
outside the Wireshark distributed code.)
The library is already loaded in the global environment as the `bit` table; a
statement like `local bit = require("bit")` is not necessary in Lua dissectors.
On Wireshark versions prior to 4.6 this will give an error (unless another copy
of the BitOp library is also accessible to the system Lua outside the Wireshark
distributed code.) On Wireshark version 4.6 and later such statements behave as
expected but are largely superfluous.
3 changes: 1 addition & 2 deletions epan/wslua/lua_bitop.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ DIAG_ON(unreachable-code)
}

luaL_newlib(L, bit_funcs);
lua_setglobal(L, "bit"); /* added for wireshark */
return 0; /* changed from 1 to 0 for wireshark, since lua_setglobal now pops the table */
return 1;
}

11 changes: 6 additions & 5 deletions epan/wslua/make-reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ def generate_register_wslua_c(classes, functions, internal_functions):
output_lines += ["\tlua_pushcfunction(L, func);"]
output_lines += ["\tlua_call(L, 0, 0);"]
output_lines += ["}\n"]
output_lines += ["static void wslua_reg_library(lua_State* L, const char *name, lua_CFunction func) {"]
output_lines += ['\tluaL_requiref(L, name, func, 1);']
output_lines += ['\tlua_pop(L, 1); /* remove lib */']
output_lines += ["}\n"]
output_lines += ["void wslua_register_classes(lua_State* L) {"]
for lua_class in classes:
output_lines += ["\twslua_reg_module(L, \"{lua_class}\", {lua_class}_register);".format(lua_class=lua_class)]

output_lines += ["\twslua_reg_module(L, \"bit\", luaopen_bit);"]
output_lines += ["\tlua_pushcfunction(L, luaopen_rex_pcre2);"]
output_lines += ["\tlua_call(L, 0, 1);"]
output_lines += ["\tlua_setglobal(L, \"rex_pcre2\");"]
output_lines += ['\twslua_reg_library(L, "bit", luaopen_bit);']
output_lines += ['\twslua_reg_library(L, "rex_pcre2", luaopen_rex_pcre2);']
output_lines += ["}\n"]

output_lines += ["void wslua_register_functions(lua_State* L) {"]
Expand Down

0 comments on commit 0488f39

Please sign in to comment.