lua-users home
lua-l archive

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


It was thus said that the Great Dibyendu Majumdar once stated:
> Hi Sean,
> 
> On 20 June 2017 at 01:13, Sean Conner <sean@conman.org> wrote:
> >   That's something my own C compiler module [1] does:
> >
> >         local cc = require "org.conman.cc"
> >
> >         local CODE = [[
> >         #include <stdio.h>
> >         #include <lua.h>
> >
> >         int TestFunc(lua_State *L)
> >         {
> >           puts("hello world!\n");
> >           return 0;
> >         }
> >         ]]
> >
> >         local f = cc.compile("TestFunc",CODE)
> >         f()
> >
> >   -spc
> >
> 
> How do you handle following code:
> 
> local testfunc = [[
> struct lua_State;
> extern int puts(const char *);
> extern int TestFunc(void);
> 
> int TestFunc(void)
> {
> puts("hello world!\n");
> return 0;
> }
> ]]

  It runs fine.  It initially surprised me, but on retrospect, it shouldn't. 
TestFunc() doesn't use its parameter, so it never tries to dereference it. 
Had there been any Lua functions though, and yes, it would most likely
coredump (if I was lucky).
 
> I try to detect that the function has the expected prototype so that
> above is rejected as below.

  I would have to read up to see if TCC has a function for that.  I'm not
sure if it does.

> Anyway the issue with allowing users to write C code is basically that
> it becomes very easy to crash the system. This is why I have not been
> keen on providing automated FFI support in Ravi.

  Heck, allowing C it becomes very easy to crash the system.  If a user
wants to use C, well, how much do time and effort is it worth to keep the
user from getting into trouble?  Yes, you can detect some problems (like a
SawStop table saw [1]) but not all tools, um, code, can be made safe, um,
detected.

  Don't coddle your users.  Tell up front their fingers *will* be sliced off
if they don't pay attention and it's not your fault.  

  -spc (Heck, I never use my TCC module in production code, and rarely even
	when playing around ... )

[1]	https://www.youtube.com/watch?v=eiYoBbEZwlk