> 'lua_next' works only with tables. Both cases are wrong.
I do not think that this is stated in the manual. That is, "if you call this function for a value on the stack other than a table, the behaviour is undefined".
In retrospect, the manual does say that "As in most C libraries, the Lua API functions do not check their arguments for validity or consistency.". However, to me, that means that the arguments (in the C language) supplied to the function must be valid, not necessarily that there are expectations about what the stack contains except as discussed in the Valid/Acceptable indices section. lua_next is document to raise Lua errors (not hard crashes), so my natural expectation from reading all of that was that it should raise a Lua error when given a nil, and do the same thing when given what was 'none'.
> You can compile Lua with LUA_USE_APICHECK on (see 'luaconf.h') to detect that kind of error in API calls.
Thanks, I will try to use the tip. Unfortunately, the error in this particular case was only found in production, and so even when leaving the API checks in production code, we would still have a crash.