From a4785102dc58ce9c8facb716f18d3aaad0538cba Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 11 Mar 2021 11:23:38 +0100 Subject: object: prevent registering the same ressource type multiple times When a module registering custom ressource types, such as "fs.so", is required multiple times we need to ensure that only one instance of a given ressource type is registered, otherwise objects created after subsequent requires will cease to function since the internal type prototype mismatches. Signed-off-by: Jo-Philipp Wich --- object.c | 7 +++++++ tests/03_bugs/05_duplicate_ressource_type | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/03_bugs/05_duplicate_ressource_type diff --git a/object.c b/object.c index c0a5227..df81fda 100644 --- a/object.c +++ b/object.c @@ -357,6 +357,13 @@ static uc_ressource_types res_types; uc_ressource_type * uc_ressource_type_add(const char *name, uc_prototype *proto, void (*freefn)(void *)) { + uc_ressource_type *existing; + + existing = uc_ressource_type_lookup(name); + + if (existing) + return existing; + uc_vector_grow(&res_types); res_types.entries[res_types.count].name = name; diff --git a/tests/03_bugs/05_duplicate_ressource_type b/tests/03_bugs/05_duplicate_ressource_type new file mode 100644 index 0000000..21166b2 --- /dev/null +++ b/tests/03_bugs/05_duplicate_ressource_type @@ -0,0 +1,31 @@ +When requiring a C module that registers custom ressource types multiple +times, ressource values instantiated after subsequent requires of the +same extensions didn't properly function since the internal type prototype +was resolved to the initial copy and subsequently discarded due to an index +mismatch. + +-- Testcase -- +{% + fs = require("fs"); + fd = fs.open("README.md"); + + printf("fd.read() #1: %s\n", + fd.read("line") ? "working" : "not working (" + fd.error() + ")"); + + fd.close(); + + + fs = require("fs"); + fd = fs.open("README.md"); + + printf("fd.read() #2: %s\n", + fd.read("line") ? "working" : "not working (" + fd.error() + ")"); + + fd.close(); +%} +-- End -- + +-- Expect stdout -- +fd.read() #1: working +fd.read() #2: working +-- End -- -- cgit v1.2.3