Stack Overflow if Library is named taboo.so

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Stack Overflow if Library is named taboo.so

Chris Tetreault
I had such a strange issue, I felt compelled to share. I'm working on a simple lua C library for a blog entry I'm working on, and apparently you can't name a lua C library "taboo". The following is my library code:

#ifndef TABOO_H
#define    TABOO_H

#ifdef    __cplusplus
extern "C" {
#endif

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
   
    static int test_string(lua_State * L);
    int luaopen_taboo(lua_State * L);
   

#ifdef    __cplusplus
}
#endif

 -----------------------------------------------------

#include <string.h>
#include "taboo.h"

static int test_string(lua_State * L){
    const char * test_val = lua_tostring(L, -1);
    if (strcmp("foo", test_val) || strcmp("bar", test_val)){
        lua_pushboolean(L, 1);
        return 1;
    }
    else{
        lua_pushboolean(L, 0);
        return 1;
    }
    return 0;
}


static const struct luaL_Reg test [] = {
    {"test_string", test_string},
    {NULL, NULL}
};

int luaopen_taboo(lua_State * L){
    luaL_newlib(L, test);
    lua_setglobal(L, "test");
    return 0;
}

... installed like so ...

sudo mv NetBeansProjects/taboo/dist/Debug/GNU-Linux-x86/libtaboo.so /usr/local/lib/lua/5.2/abcde.so

... and my script ...

#!/home/chris/lua/bin/lua

require("taboo")

print(test.test_string("foo"))
print(test.test_string("words"))

... when run, outputs ...

./taboo.lua
/home/chris/lua/bin/lua: error loading module 'taboo' from file './taboo.lua':
    ./taboo.lua:5: too many C levels (limit is 200) in main function near '"foo"'
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ...
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in ?

If I change the library name to literally anything else, it works fine. I'm fairly new to lua, so I may be missing something. But I can't think of any reason why this should be happening. Have I found some obscure bug, or am I on crack?
Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Kaj Eijlers
Aren't you requiring taboo.lua from taboo.lua?


On Thu, May 30, 2013 at 11:54 PM, Chris Tetreault <[hidden email]> wrote:
I had such a strange issue, I felt compelled to share. I'm working on a simple lua C library for a blog entry I'm working on, and apparently you can't name a lua C library "taboo". The following is my library code:

#ifndef TABOO_H
#define    TABOO_H

#ifdef    __cplusplus
extern "C" {
#endif

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
   
    static int test_string(lua_State * L);
    int luaopen_taboo(lua_State * L);
   

#ifdef    __cplusplus
}
#endif

 -----------------------------------------------------

#include <string.h>
#include "taboo.h"

static int test_string(lua_State * L){
    const char * test_val = lua_tostring(L, -1);
    if (strcmp("foo", test_val) || strcmp("bar", test_val)){
        lua_pushboolean(L, 1);
        return 1;
    }
    else{
        lua_pushboolean(L, 0);
        return 1;
    }
    return 0;
}


static const struct luaL_Reg test [] = {
    {"test_string", test_string},
    {NULL, NULL}
};

int luaopen_taboo(lua_State * L){
    luaL_newlib(L, test);
    lua_setglobal(L, "test");
    return 0;
}

... installed like so ...

sudo mv NetBeansProjects/taboo/dist/Debug/GNU-Linux-x86/libtaboo.so /usr/local/lib/lua/5.2/abcde.so

... and my script ...

#!/home/chris/lua/bin/lua

require("taboo")

print(test.test_string("foo"))
print(test.test_string("words"))

... when run, outputs ...

./taboo.lua
/home/chris/lua/bin/lua: error loading module 'taboo' from file './taboo.lua':
    ./taboo.lua:5: too many C levels (limit is 200) in main function near '"foo"'
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ...
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in ?

If I change the library name to literally anything else, it works fine. I'm fairly new to lua, so I may be missing something. But I can't think of any reason why this should be happening. Have I found some obscure bug, or am I on crack?

Reply | Threaded
Open this post in threaded view
|

RE: Stack Overflow if Library is named taboo.so

Chris Tetreault
Yep, I am...

mv'd taboo.lua to testTaboo.lua and it behaves as expected. Thanks for the feedback!


Date: Fri, 31 May 2013 00:14:35 -0400
From: [hidden email]
To: [hidden email]
Subject: Re: Stack Overflow if Library is named taboo.so

Aren't you requiring taboo.lua from taboo.lua?


On Thu, May 30, 2013 at 11:54 PM, Chris Tetreault <[hidden email]> wrote:
I had such a strange issue, I felt compelled to share. I'm working on a simple lua C library for a blog entry I'm working on, and apparently you can't name a lua C library "taboo". The following is my library code:

#ifndef TABOO_H
#define    TABOO_H

#ifdef    __cplusplus
extern "C" {
#endif

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
   
    static int test_string(lua_State * L);
    int luaopen_taboo(lua_State * L);
   

#ifdef    __cplusplus
}
#endif

 -----------------------------------------------------

#include <string.h>
#include "taboo.h"

static int test_string(lua_State * L){
    const char * test_val = lua_tostring(L, -1);
    if (strcmp("foo", test_val) || strcmp("bar", test_val)){
        lua_pushboolean(L, 1);
        return 1;
    }
    else{
        lua_pushboolean(L, 0);
        return 1;
    }
    return 0;
}


static const struct luaL_Reg test [] = {
    {"test_string", test_string},
    {NULL, NULL}
};

int luaopen_taboo(lua_State * L){
    luaL_newlib(L, test);
    lua_setglobal(L, "test");
    return 0;
}

... installed like so ...

sudo mv NetBeansProjects/taboo/dist/Debug/GNU-Linux-x86/libtaboo.so /usr/local/lib/lua/5.2/abcde.so

... and my script ...

#!/home/chris/lua/bin/lua

require("taboo")

print(test.test_string("foo"))
print(test.test_string("words"))

... when run, outputs ...

./taboo.lua
/home/chris/lua/bin/lua: error loading module 'taboo' from file './taboo.lua':
    ./taboo.lua:5: too many C levels (limit is 200) in main function near '"foo"'
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ...
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in function 'require'
    ./taboo.lua:3: in main chunk
    [C]: in ?

If I change the library name to literally anything else, it works fine. I'm fairly new to lua, so I may be missing something. But I can't think of any reason why this should be happening. Have I found some obscure bug, or am I on crack?

Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Kaj Eijlers
It's funny how sometimes once you lock in on the perceived cause you totally miss the obvious. Rubber duck debugging can really help here.


(saying that, I often fall in the same trap, and feel quite embarrassed when I ask a coworker and the answer is something I should have seen, and would have seen, if it wasn't on *my* monitor)

Glad to have helped :o)

Kaj


Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Paul K
In reply to this post by Chris Tetreault
Hi Chris,

> If I change the library name to literally anything else, it works fine. I'm fairly new to lua, so I may be missing something. But I can't think of any reason why this should be happening. Have I found some obscure bug, or am I on crack?

You named your file taboo.lua and are trying to load a module with the
same name. "require" loads the same file, it gets executed, hits
"require", it loads the same file and so on until you hit recursion
limit.

Rename your script to something else.

Paul.

On Thu, May 30, 2013 at 8:54 PM, Chris Tetreault
<[hidden email]> wrote:

> I had such a strange issue, I felt compelled to share. I'm working on a
> simple lua C library for a blog entry I'm working on, and apparently you
> can't name a lua C library "taboo". The following is my library code:
>
> #ifndef TABOO_H
> #define    TABOO_H
>
> #ifdef    __cplusplus
> extern "C" {
> #endif
>
> #include <lua.h>
> #include <lualib.h>
> #include <lauxlib.h>
>
>     static int test_string(lua_State * L);
>     int luaopen_taboo(lua_State * L);
>
>
> #ifdef    __cplusplus
> }
> #endif
>
>  -----------------------------------------------------
>
> #include <string.h>
> #include "taboo.h"
>
> static int test_string(lua_State * L){
>     const char * test_val = lua_tostring(L, -1);
>     if (strcmp("foo", test_val) || strcmp("bar", test_val)){
>         lua_pushboolean(L, 1);
>         return 1;
>     }
>     else{
>         lua_pushboolean(L, 0);
>         return 1;
>     }
>     return 0;
> }
>
>
> static const struct luaL_Reg test [] = {
>     {"test_string", test_string},
>     {NULL, NULL}
> };
>
> int luaopen_taboo(lua_State * L){
>     luaL_newlib(L, test);
>     lua_setglobal(L, "test");
>     return 0;
> }
>
> ... installed like so ...
>
> sudo mv NetBeansProjects/taboo/dist/Debug/GNU-Linux-x86/libtaboo.so
> /usr/local/lib/lua/5.2/abcde.so
>
> ... and my script ...
>
> #!/home/chris/lua/bin/lua
>
> require("taboo")
>
> print(test.test_string("foo"))
> print(test.test_string("words"))
>
> ... when run, outputs ...
>
> ./taboo.lua
> /home/chris/lua/bin/lua: error loading module 'taboo' from file
> './taboo.lua':
>     ./taboo.lua:5: too many C levels (limit is 200) in main function near
> '"foo"'
> stack traceback:
>     [C]: in ?
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ...
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in function 'require'
>     ./taboo.lua:3: in main chunk
>     [C]: in ?
>
> If I change the library name to literally anything else, it works fine. I'm
> fairly new to lua, so I may be missing something. But I can't think of any
> reason why this should be happening. Have I found some obscure bug, or am I
> on crack?

Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Leo Razoumov
On Fri, May 31, 2013 at 1:19 AM, Paul K <[hidden email]> wrote:
> Hi Chris,
>
>> If I change the library name to literally anything else, it works fine. I'm fairly new to lua, so I may be missing something. But I can't think of any reason why this should be happening. Have I found some obscure bug, or am I on crack?
>
> You named your file taboo.lua and are trying to load a module with the
> same name. "require" loads the same file, it gets executed, hits
> "require", it loads the same file and so on until you hit recursion
> limit.
>

Isn't  require supposed to set  package.loaded[modname]  to some true
value *before* it executes the module chunk to prevent a possibility
of an infinite recursion? If it is not the case and require can cause
an infinite recursion then it is a bug in Lua module system.
Requiring modules can produce cycles  (directly or indirectly) and a
package loader should be prepared to handle this possibility.

--Leo--

Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Owen Shepherd
Leo Razoumov wrote:

Isn't  require supposed to set  package.loaded[modname]  to some true
value*before*  it executes the module chunk to prevent a possibility
of an infinite recursion? If it is not the case and require can cause
an infinite recursion then it is a bug in Lua module system.
Requiring modules can produce cycles  (directly or indirectly) and a
package loader should be prepared to handle this possibility.


There is no appropriate value for package.loaded to be set to until the package code has finished executing. If require set it to some placeholder value, it would just cause errors in other portions of the application.
Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Jay Carlson
On May 31, 2013, at 10:07 AM, Owen Shepherd wrote:

> Leo Razoumov wrote:
>>
>> Isn't  require supposed to set  package.loaded[modname]  to some true
>> value*before*  it executes the module chunk to prevent a possibility
>> of an infinite recursion?

> There is no appropriate value for package.loaded to be set to until the package code has finished executing. If require set it to some placeholder value, it would just cause errors in other portions of the application.

If capturing the contents of modules were prohibited by convention, this would not be a problem in most cases. This is yet another issue for the localized Lua dialect.

Lua has a single mechanism for naming values symbolically: local. This binds too tightly. In many cases I would prefer #define.

Note that

  define tinsert = table.insert

could have strictly lexical scope, thus not violating "no macros."

But this is not that far from

  static tinsert = table.insert

which is macro-expanded into

  local tinsert
  local function _initializer_1() tinsert=table.insert end
  _ENV.initializer(_initializer_1)

which allows separation of initialization from load; potentially the module system could defer initialization until upstream modules were at least loaded. (The primary goal of separating initializers is to make module reload do something useful in localized Lua, as well as hoisting local symbolic names for things out of loops.)

Jay
Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

Leo Razoumov
On Fri, May 31, 2013 at 10:41 AM, Jay Carlson <[hidden email]> wrote:

> On May 31, 2013, at 10:07 AM, Owen Shepherd wrote:
>
>> Leo Razoumov wrote:
>>>
>>> Isn't  require supposed to set  package.loaded[modname]  to some true
>>> value*before*  it executes the module chunk to prevent a possibility
>>> of an infinite recursion?
>
> There is no appropriate value for package.loaded to be set to until the package code has finished executing. If require set it to some placeholder
> value, it would just cause errors in other portions of the application.

Yes, it is a touch cookie. But one cannot ignore a possibility that
A->B->C->A  and here is a cycle. At least a loader should be able to
identify a cycle and report back an informative message rather than
"recursion depth exceeded".

--Leo--

Reply | Threaded
Open this post in threaded view
|

Re: Stack Overflow if Library is named taboo.so

David Heiko Kolf-2
In reply to this post by Leo Razoumov
Leo Razoumov wrote:
> Isn't  require supposed to set  package.loaded[modname]  to some true
> value *before* it executes the module chunk to prevent a possibility
> of an infinite recursion? If it is not the case and require can cause
> an infinite recursion then it is a bug in Lua module system.
> Requiring modules can produce cycles  (directly or indirectly) and a
> package loader should be prepared to handle this possibility.

The old "module" function did that, it created a module table and set
package.loaded[modname] right at the start of the module.

Without the "module" function you would have to do it yourself:

    local M = {}
    package.loaded[...] = M

    -- implement the module, cyclic requirements are now valid.

    return M


Best regards,

David Kolf