LuaL_loadbuffer doesn't appear to use name parameter for error messages

  • From: Sam Putman <atmanistan@xxxxxxxxx>
  • To: LuaJIT <luajit@xxxxxxxxxxxxx>
  • Date: Thu, 9 May 2019 22:07:22 +0200

I can't tell if this is intended behavior or if I'm doing something wrong,
so I figured I would ask.

Here's the minimal example:

```min.c
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "luajit.h"

const char ERROR_BYTES[] = {'\x1b', '\x4c', '\x4a', '\x2', '\x0', '\x29',
'\x65', '\x72', '\x72', '\x6f', '\x72', '\x20', '\x22', '\x79', '\x6f',
'\x75', '\x20', '\x68', '\x61', '\x76', '\x65', '\x20', '\x6d', '\x61',
'\x64', '\x65', '\x20', '\x61', '\x20', '\x74', '\x65', '\x72', '\x72',
'\x69', '\x62', '\x6c', '\x65', '\x20', '\x6d', '\x69', '\x73', '\x74',
'\x61', '\x6b', '\x65', '\x21', '\x22', '\x47', '\x2', '\x0', '\x2', '\x0',
'\x2', '\x0', '\x4', '\x5', '\x0', '\x1', '\x36', '\x0', '\x0', '\x0',
'\x27', '\x1', '\x1', '\x0', '\x42', '\x0', '\x2', '\x1', '\x4b', '\x0',
'\x1', '\x0', '\x26', '\x79', '\x6f', '\x75', '\x20', '\x68', '\x61',
'\x76',
'\x65', '\x20', '\x6d', '\x61', '\x64', '\x65', '\x20', '\x61', '\x20',
'\x74', '\x65', '\x72', '\x72', '\x69', '\x62', '\x6c', '\x65', '\x20',
'\x6d', '\x69', '\x73', '\x74', '\x61', '\x6b', '\x65', '\x21', '\xa',
'\x65', '\x72', '\x72', '\x6f', '\x72', '\x1', '\x1', '\x1', '\x1', '\x0',
'\x0'};

const char * ERROR_NAME = "problem";

// Print an error.
static int lua_die(lua_State *L, int errno) {
    fprintf(stderr, "err #%d: %s\n", errno, lua_tostring(L, -1));
    return errno;
}

static int debug_load(lua_State *L, const char bytecode[], int byte_len,
const char * name) {
    lua_getglobal(L, "debug");
    lua_getfield(L, -1, "traceback");
    lua_replace(L, -2);
    int status = luaL_loadbuffer(L, bytecode, byte_len, name);
    if (status != 0) {
       return lua_die(L, status);
    }
    int ret = lua_pcall(L, 0, 0, -2);
    if (ret != 0) {
        return lua_die(L, ret);
    }
    lua_pop(L, 1);
    return ret;
}

int main(int argc, char *argv[]) {
    lua_State *L = luaL_newstate();
    if (!L) {
        return 1;
    }
    luaL_openlibs(L);
    debug_load(L, ERROR_BYTES, sizeof ERROR_BYTES, ERROR_NAME);
    lua_close(L); // Close Lua
    return 0;
}
```

The ERROR_BYTES field is a loaded and dumped version of this program:

```lua
error "you have made a terrible mistake!"
```

I compile and link with this, you will need your own paths of course:

```sh
cc -c -Ibuild/ -Ilib/ -Isrc/ src/minimal.c -o build/minimal.o -std=c99
cc -o min build/minimal.o build/libluajit.a -Ibuild/ -Ilib/ -lm
-pagezero_size 10000 -image_base 100000000
```

The pagezero_size and image_base parameters are mac-specific, IIRC.

The output is the following:

```output
err #2: [string "error "you have made a terrible mistake!""]:1: you have
made a terrible mistake!
stack traceback:
[C]: in function 'error'
[string "error "you have made a terrible mistake!""]:1: in main chunk
```

I would expect an error looking like `[string problem]:1:` etc.

Any help would be most appreciated.

cheers,
-Sam

Other related posts: