[syslinux:master] lua: represent syslinux files as full userdata

syslinux-bot for Ferenc Wágner wferi at niif.hu
Sat Feb 14 11:09:20 PST 2015


Commit-ID:  428d9a5bd15844c072044857c8ef78b873c9ee58
Gitweb:     http://www.syslinux.org/commit/428d9a5bd15844c072044857c8ef78b873c9ee58
Author:     Ferenc Wágner <wferi at niif.hu>
AuthorDate: Sun, 12 Oct 2014 08:56:01 +0200
Committer:  Ferenc Wágner <wferi at niif.hu>
CommitDate: Sun, 12 Oct 2014 10:07:49 +0200

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 at niif.hu>

---
 com32/lua/src/syslinux.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)

diff --git a/com32/lua/src/syslinux.c b/com32/lua/src/syslinux.c
index c5e5b03..9713109 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;
 }
 


More information about the Syslinux-commits mailing list