1
votes

I'm test call c/c++ module in lua(5.1). Without vector the code works well.But when i include stl/vector and declare a vector variable in Function add, then the program cause segmentation fault.

libvec.so code:

#include <vector>
#include <list>
#include "lua.hpp"
#include <algorithm>
#include <iostream>
#include <string>
#include <map>

extern "C" int add(lua_State* L)
{
    double op1 = luaL_checknumber(L, 1);
    double op2 = luaL_checknumber(L, 2);
    //std::vector<double> vec;
    //vec.push_back(op1);
    //vec.push_back(op2);
    //lua_pushnumber(L, vec[0]+vec[1]);
    lua_pushnumber(L, op1 + op2);
    return 1;
}

static luaL_Reg mylibs[] = {
    {"add", add}
};

extern "C" int luaopen_libvec(lua_State *L){
    luaL_register(L, "libvec", mylibs);
    return 1;
}

test.lua code:

local vec = require 'libvec'
print(vec.add(1,212))

then i try to gdb lua/run test.lua, then get

strlen () at ../sysdeps/x86_64/strlen.S:106
106 ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0  strlen () at ../sysdeps/x86_64/strlen.S:106
#1  0x000000000040596e in lua_setfield ()
#2  0x0000000000412f75 in luaL_openlib ()
#3  0x00007ffff6e8e531 in luaopen_libvec () from ./libvec.so
#4  0x00000000004080d8 in luaD_precall ()
#5  0x00000000004084e4 in luaD_call ()
#6  0x0000000000405c95 in lua_call ()
#7  0x000000000041dcaa in ll_require ()
#8  0x00000000004080d8 in luaD_precall ()
#9  0x00000000004116d2 in luaV_execute ()
#10 0x000000000040852d in luaD_call ()
#11 0x000000000040782b in luaD_rawrunprotected ()
#12 0x000000000040868b in luaD_pcall ()
#13 0x0000000000405d26 in lua_pcall ()
#14 0x0000000000403ecc in docall ()
#15 0x00000000004048a9 in pmain ()
#16 0x00000000004080d8 in luaD_precall ()
#17 0x00000000004084e4 in luaD_call ()
#18 0x000000000040782b in luaD_rawrunprotected ()
#19 0x000000000040868b in luaD_pcall ()
#20 0x0000000000405db5 in lua_cpcall ()
#21 0x0000000000403b94 in main ()

i have test declare std::map and crash too while std::string works well...

2
What are you passing to vec.add()? and what it is expecting? - user8235810
@mfromla just run lua test.lua - YetAnotherOldBoy

2 Answers

2
votes

From the documentation of luaL_register:

Any array of luaL_Reg must end with an sentinel entry in which both name and func are NULL.

This is not the case here, and I can imagine the addition of std::vector methods to push aside some null bytes that happened to be there previously.

tl;dr: add a {NULL, NULL} entry to myLibs.

2
votes

From the documentation https://www.lua.org/manual/5.1/manual.html#luaL_ref:

Type for arrays of functions to be registered by luaL_register. name is the function name and func is a pointer to the function. Any array of luaL_Reg must end with an sentinel entry in which both name and func are NULL.

luaL_register is iterating over your array until it finds the sentinel value, as it doesn't find one it reads past the end of your array.

The correct array is:

static luaL_Reg mylibs[] = {
    {"add", add},
    {NULL, NULL}
};