lua-users home
lua-l archive

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


Hi All
I have a small c code, it is crashing when invoking lua_call.
Corresponding lua script (without c code works fine)
When same thing trying to from a C code with Lua embedded getting crash.

Please somebody help why it is crasing..

Code as below.

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua5.2/lua.h>
#include <lua5.2/lualib.h>
#include <lua5.2/lualib.h>

const char *c_function = "function modify_content_length(msg, length) "
                         "local offset = string.find(msg, '\r\n\r\n\', 1, true) "
                         "if(offset == nil) then return 'not found' "
                         "else "
                         "local n = length - offset - 4 + 1 "
                         "msg1 = msg:gsub('(Content%-Length%s*:)','%1 '..n.. '') "
                         "return msg1 "
                         "end "
                         "end";

int main(void)
{
  lua_State *L;
  char *r;

  int length = 0;
  char *msg = "INVITE\r\nContent-Length : \r\n\r\nv=0\r\n";

  L = (lua_State *) luaL_newstate();
  luaL_openlibs(L);

  luaL_loadstring(L,c_function);
  lua_pcall(L,0,0,0);

  length = strlen(msg);
  lua_getglobal(L,"modify_content_length");
  lua_pushinteger(L,length);
  lua_pushstring(L, msg);
  lua_call(L,2,1);

  r = (char *)luaL_checklstring(L,-1);
  printf("\n\nresult: %s\n\n",r);

  lua_close(L);
  return EXIT_SUCCESS;
}