Nmap Development mailing list archives

Re: [NSE] Unused library cleanup


From: Patrick Donnelly <batrick () batbytes com>
Date: Mon, 9 Jul 2012 16:33:25 -0400

Hi Daniel,

On Sun, Jul 8, 2012 at 9:27 AM, Daniel Miller <bonsaiviking () gmail com> wrote:
I wrote a small Perl script (attached) that checks for Lua libraries
required but not used (for one definition of used).

I suggest you maybe look at
/nmap-private-dev/misc-scripts/nse_remove_require and
/nmap-private-dev/misc-scripts/nse_check_globals.

The first script removes all occurrences of require so that
nse_check_globals can add and alphabetize all requires.
nse_remove_require is also incidentally useful for removing unneeded
requires. Unfortunately nse_remove_require needs some work as it
currently removes require lines that it should not, e.g. commented
requires:

-- @usage
--   require "foo"

You may want to improve upon nse_remove_require so that it is more
foolproof. So far, I know it should not remove require statements like
this:

o Lines which are commented (space then "--").
o Move silent_require statements to top of script.
o Don't remove instances of require "strict"
o Don't remove requires that are commented with "cycle" (smb.lua)

I played around with vim regular expressions and have given up. I
think something like perl or Lua should easily work but haven't gotten
around to fixing it.

2. I'm not sure if the "method"-style of function use is affected by
having or not having a local reference to a library. For example,
string.format("%d", 3) runs faster if local string = require "string".
But is the same true for ("%d"):format(3)?

No. The string type has a global metatable shared by all strings which
is set by the Lua string library [1]. This is why it's possible to
index string methods from a string. Whether or not you have a local
reference to the string library is irrelevant. To illustrate:

$ bin/lua
Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
print(require "string", getmetatable("").__index)
table: 0x205aca0        table: 0x205aca0
print(require "string".format, getmetatable("").__index.format, ("").format)
function: 0x41dad0      function: 0x41dad0      function: 0x41dad0

[1] http://www.lua.org/source/5.2/lstrlib.c.html#createmetatable

3. packetdecoders.lua had requires inside functions, which I think is
not a great idea, since those functions should try to be as fast as
possible. I moved them to the top of the file instead.

Be careful because there are some libraries (smb.lua is the only one I
know of) that need to do this. It looks like your fix for
packetdecoders is good though.

-- 
- Patrick Donnelly
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


Current thread: