Skip to content

Commit

Permalink
Prevent class name clashes with paths in Lua objects (#10 revisited).
Browse files Browse the repository at this point in the history
  • Loading branch information
agraef committed Jan 9, 2023
1 parent 261c00c commit e28dc6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ pd._clearrequirepath = function()
package.cpath = pd._packagecpath
end

-- check whether a class already exists
pd._check = function (name)
return nil ~= pd._classes[name]
end

-- constructor dispatcher
pd._constructor = function (name, atoms)
if nil ~= pd._classes[name] then
Expand Down
21 changes: 21 additions & 0 deletions pdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,27 @@ static int pdlua_loader_fromfd
t_pdlua_readerdata reader;

PDLUA_DEBUG("pdlua_loader: stack top %d", lua_gettop(__L));
// First check whether the class has already been created under a
// different path.
lua_getglobal(__L, "pd");
lua_getfield(__L, -1, "_check");
lua_pushstring(__L, name);
// We expect false here, indicating that the class hasn't been registered
// yet and thus we can safely go ahead and load the file. Everything else
// indicates an error. To be on the safe side, we also assume this if the
// call fails or returns anything else but a boolean value.
int res = lua_pcall(__L, 1, 1, 0) ||
!lua_isboolean(__L, -1) || lua_toboolean(__L, -1);
lua_pop(__L, 2);/* pop the result and the global "pd" */
// If res indicates an error, there's really nothing that we can do except
// bail out with failure. Everything else will leave the loader in an
// unstable state and wreak havoc further down the line. Thus we can't
// create the object even if the basename class was registered earlier.
if (res) {
pd_error(NULL, "lua: error loading `%s': class already exists", name);
return 0;
}
// If we come here we're good; go ahead and load the Lua source.
class_set_extern_dir(gensym(dirbuf));
pdlua_setrequirepath(__L, dirbuf);
reader.fd = fd;
Expand Down

0 comments on commit e28dc6d

Please sign in to comment.