Home page logo

nmap-dev logo Nmap Development mailing list archives

Re: NSE exceptions: false or nil?
From: Stoiko Ivanov <stoiko () xover htu tuwien ac at>
Date: Fri, 2 Nov 2007 13:00:24 +0100


On Thu, Nov 01, 2007 at 10:40:22PM -0700, David Fifield wrote:
Are NSE exceptions supposed to return false or nil? At
http://insecure.org/nmap/nse/nse-api.html#nse-exceptions it says that
the first thing returned is a Boolean value. But trying to catch an
exception from connect fails, as the attached example script (test.nse)
shows. It produces this message:

      Initiating SCRIPT ENGINE at 22:26
      false   Name or service not known
      SCRIPT ENGINE: In: nse_nmaplib.cc:474 Trying to finalize a non conforming function. Are you sure you return 
true on success followed by the remaining return values and nil on failure followed by an error string?

We see that connect returns (false, "Name or service not known"), but
this is "non conforming." Should NSE's internal functions be modified to
return nil instead of false, or should false be considered an exception
marker? Does the patch I attached do the right thing?
I think the latter would be more convenient. 
Since the network I/O functions return true on success I would expect a false 
value to indicate an exception. 
AFAIK this patch shouldn't cause any problems with the remaining NSE.

David Fifield

Index: nse_nmaplib.cc
--- nse_nmaplib.cc    (revision 6111)
+++ nse_nmaplib.cc    (working copy)
@@ -460,7 +460,7 @@
 static int l_exc_finalize(lua_State *l) {
-     if (lua_isnil(l, 1)) {
+     if (lua_isnil(l, 1) || !lua_toboolean(l, 1)) {
This looks good to me, however if I recall correctly the lua_isnil() is not
really needed, because lua_toboolean() should return false if there is nil
at the specified position on the stack.

              lua_pushvalue(l, lua_upvalueindex(1));
              lua_call(l, 0, 0);
              lua_settop(l, 2);


Sent through the nmap-dev mailing list
Archived at http://SecLists.Org

  By Date           By Thread  

Current thread:
[ Nmap | Sec Tools | Mailing Lists | Site News | About/Contact | Advertising | Privacy ]