lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Leo Razoumov wrote:
> the following code crashes Lua-5.1.2 and Lua-5.1.3 (luajit)

That's not a crash. If Lua crashed, you would be getting an error
message from the operating system, or perhaps from a runtime library
linked into the Lua executable. What you saw is a graceful script
failure: Lua halted execution of a script because of a runtime error,
and printed a nice error message with a stack trace.

> I still cannot figure it out while one
> hole breaks table.concat but two holes work fine.


>From the definition of the length operator:
"If the array has 'holes' (that is, nil values between other non-nil values),
then #t may be any of the indices that directly precedes a nil value"

>From the definition of table.concat:
"returns table[i]..sep..table[i+1] ··· sep..table[j]"
"default for j is the length of the table"

You're not providing 'j', so it ends up being #t. In your first
example, j ended up being 4, so concat failed on table[i+1] (as it
should, given the definition of concat). In your second example, you
got lucky and j ended up being 1.

> It is either a bug in Lua implementation or a bug in Lua documentation.

Neither. You can see from the definition of concat that it requires a
contiguous array. You didn't give it one.

The manual doesn't specific exactly how concat will fail if you don't
give it correct input (though we expect it to fail gracefully and not
crash, and it does). The fact that it *seemed* to work for one
instance of bad input doesn't mean anything.

I just read the link Petite provided, and it's 100% apropos. Go read it. :)