diff options
author | Ferenc Wágner <wferi@niif.hu> | 2014-10-12 08:56:01 +0200 |
---|---|---|
committer | Ferenc Wágner <wferi@niif.hu> | 2014-10-12 10:07:49 +0200 |
commit | 428d9a5bd15844c072044857c8ef78b873c9ee58 (patch) | |
tree | 5b5a5481dce602851f0738d26ddd805b40ef595e | |
parent | 5814320381f1305264a57c9f3ab23899135539be (diff) | |
download | syslinux-428d9a5bd15844c072044857c8ef78b873c9ee58.tar.gz syslinux-428d9a5bd15844c072044857c8ef78b873c9ee58.tar.xz syslinux-428d9a5bd15844c072044857c8ef78b873c9ee58.zip |
lua: represent syslinux files as full userdata
Light userdata don't have individual metatables, thus they aren't
suitable for type checking. Also, a200ad6d replaced relying on reused
memory (from luaL_checkstring) with writing to uninitialized pointers;
copy the filename instead into freshly allocated memory.
Signed-off-by: Ferenc Wágner <wferi@niif.hu>
-rw-r--r-- | com32/lua/src/syslinux.c | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/com32/lua/src/syslinux.c b/com32/lua/src/syslinux.c index c5e5b037..97131095 100644 --- a/com32/lua/src/syslinux.c +++ b/com32/lua/src/syslinux.c @@ -44,10 +44,10 @@ int __parse_argv(char ***argv, const char *str); -#define SYSLINUX_FILE "syslinux_file" +static const char SYSLINUX_FILE[] = "syslinux_file"; typedef struct syslinux_file { - char *data; + void *data; char *name; size_t size; } syslinux_file; @@ -241,26 +241,18 @@ static int sl_run_kernel_image(lua_State * L) static int sl_loadfile(lua_State * L) { - const char *filename = luaL_checkstring(L, 1); - syslinux_file *file; - - void *file_data; - size_t file_len; - - if (loadfile(filename, &file_data, &file_len)) { - lua_pushstring(L, "Could not load file"); - lua_error(L); + size_t name_len; + const char *filename = luaL_checklstring (L, 1, &name_len); + syslinux_file *file = lua_newuserdata (L, sizeof (syslinux_file)); + + file->name = malloc (name_len+1); + if (!file->name) return luaL_error (L, "Out of memory"); + memcpy (file->name, filename, name_len+1); + if (loadfile (file->name, &file->data, &file->size)) { + free (file->name); + return luaL_error (L, "Could not load file"); } - - file = malloc(sizeof(syslinux_file)); - strlcpy(file->name,filename,sizeof(syslinux_file)); - file->size = file_len; - file->data = file_data; - - lua_pushlightuserdata(L, file); - luaL_getmetatable(L, SYSLINUX_FILE); - lua_setmetatable(L, -2); - + luaL_setmetatable (L, SYSLINUX_FILE); return 1; } |