This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

bad argument #1 to ‘get_index’ (index out of range) in Wireshark Lua dissector

0

In my dissector I have this code

local defaultdata = data_tvb():bytes()
local newdata = ByteArray.new()
newdata:set_size(defaultdata:len())
for i=0,defaultdata:len()-2 do 
local var = bit.band((bit.lshift(defaultdata:get_index(i), 1) + bit.rshift(defaultdata:get_index(i+1), 7)), 0xff)
newdata:set_index(i, var) end
local var = bit.band((bit.lshift(defaultdata:get_index(defaultdata:len()-1), 1) + bit.rshift(defaultdata:get_index(0), 7)), 0xff)
newdata:set_index(defaultdata:len()-1,var)
data_tvb = ByteArray.tvb(newdata, "Decoded") end

My problem is in second bitwise operation in get_index function.

I know, that problem might be in get_index(0) or get_index(defaultdata:len()-1) because in Lua there is no element of the zero index(not that of C) but nothing actually works with another values.

With any values I got this message: bad argument #1 to 'get_index' (index out of range) So, as I mentioned above, part, that not depend on this code work correctly.

asked 13 Feb '14, 02:00

im_infamous's gravatar image

im_infamous
10115
accept rate: 100%


2 Answers:

0

Problem solved. The matter is that my dissector don't cover cases of null application protocol payload, and thats why i got these errors. The solution is to add one "if" statement, that checks out length of the payload. Hadriel, thank you very much for your help.

answered 14 Feb '14, 02:03

im_infamous's gravatar image

im_infamous
10115
accept rate: 100%

edited 14 Feb '14, 02:07

2

ByteArray:set_index() and get_index() actually use 0-based indexing, not normal Lua 1-based indexing. The only time you should see that "index out of range" error is if the index number you used in ByteArray:get_index() was either negative, or it's greater than or equal to the length of the Byte Array. Which exact line in that code snippet is causing you the error? Also, just to be clear, your code snippet is this, right?:

    local defaultdata = data_tvb():bytes()
    local newdata = ByteArray.new()
    newdata:set_size(defaultdata:len())
    for i=0,defaultdata:len()-2 do 
        local var = bit.band((bit.lshift(defaultdata:get_index(i), 1) + bit.rshift(defaultdata:get_index(i+1), 7)), 0xff)
        newdata:set_index(i, var) 
    end
    local var = bit.band((bit.lshift(defaultdata:get_index(defaultdata:len()-1), 1) + bit.rshift(defaultdata:get_index(0), 7)), 0xff)
    newdata:set_index(defaultdata:len()-1,var)
    data_tvb = ByteArray.tvb(newdata, "Decoded")
end

answered 13 Feb '14, 10:02

Hadriel's gravatar image

Hadriel
2.7k2939
accept rate: 18%

Thanks for your reply! Yep, we are talking about that snippet. The error now is in the second line and sounds like "attempt to index ByteArray global (a nil value)"

(13 Feb '14, 10:38) im_infamous

What wireshark version/release are you using?

(13 Feb '14, 11:10) Hadriel

Im using wireshark portable 1.8.2

(13 Feb '14, 20:55) im_infamous

When evaluating lua in wireshark, I got message "bad argument #1 to 'get index' (index out of range) and when it comes to lua executor "attempt to index global ByteArray(a nil value)"

(13 Feb '14, 22:59) im_infamous
1

Oh, you were trying this in just the stand-alone lua interpreter? (i.e. outside of wireshark) Then yes, of course it would give that "a nil value" error. That error is telling you there's no global variable named "ByteArray", and thus no table to try calling "ByteArray.new()" on. There is no such global variable in that, because ByteArray is a table created by Wireshark, not something Lua comes with. That's why I was asking what version you were running, because it didn't make sense that it couldn't find a ByteArry table - but if you got that error when you were just running the Lua interpreter by itself, then sure it won't work. As for the other error - the "index out of range" one, I'll look into the 1.8.2 code but basically it means what I had said: the index number you're giving it is either negative or greater than or equal to the length of the byte array.

(14 Feb '14, 00:34) Hadriel
1

Is it possible the Tvb only had one or zero bytes in it? In other words, your call to 'data_tvb():bytes()', which returns a ByteArray from the Tvb - could it just be only one byte? If so, your for-loop will get messed up and be trying to get_index() too large a number.

Regardless, try putting some print() statements in the code to see what the values of those things are. Like 'print("len="..defaultdata:len())'

(14 Feb '14, 00:49) Hadriel
showing 5 of 6 show 1 more comments