diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2023-02-27 21:20:58 -0800 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2023-03-04 23:40:32 -0800 |
commit | 9d7b57221d405fda3a317949b4aadfa2d1116210 (patch) | |
tree | 799535ec3ad874cb82e1b98c838d99e238652e9a | |
parent | e9dfa02f1d7df0ef2155600633b833efb3fa5477 (diff) | |
download | gjs-9d7b57221d405fda3a317949b4aadfa2d1116210.tar.gz |
repo: Check that GI module override function is callable
Previously, we would return the override function if it was null, or a
non-callable object, and then try to call it. Add tests for this case.
-rw-r--r-- | gi/repo.cpp | 5 | ||||
-rw-r--r-- | installed-tests/js/jsunit.gresources.xml | 4 | ||||
-rw-r--r-- | installed-tests/js/meson.build | 1 | ||||
-rw-r--r-- | installed-tests/js/modules/badOverrides2/.eslintrc.yml | 8 | ||||
-rw-r--r-- | installed-tests/js/modules/badOverrides2/GIMarshallingTests.js | 5 | ||||
-rw-r--r-- | installed-tests/js/modules/badOverrides2/Gio.js | 5 | ||||
-rw-r--r-- | installed-tests/js/modules/badOverrides2/Regress.js | 5 | ||||
-rw-r--r-- | installed-tests/js/modules/badOverrides2/WarnLib.js | 3 | ||||
-rw-r--r-- | installed-tests/js/testImporter.js | 2 | ||||
-rw-r--r-- | installed-tests/js/testImporter2.js | 39 |
10 files changed, 75 insertions, 2 deletions
diff --git a/gi/repo.cpp b/gi/repo.cpp index 27e78b5e..92d67d65 100644 --- a/gi/repo.cpp +++ b/gi/repo.cpp @@ -556,9 +556,12 @@ lookup_override_function(JSContext *cx, goto fail; } + // If the override module is present, it must have a callable _init(). An + // override module without _init() is probably unintentional. (function + // being undefined means there was no override module.) if (!gjs_object_require_property(cx, module, "override module", atoms.init(), function) || - !function.isObjectOrNull()) { + !function.isObject() || !JS::IsCallable(&function.toObject())) { gjs_throw(cx, "Unexpected value for _init in overrides module"); goto fail; } diff --git a/installed-tests/js/jsunit.gresources.xml b/installed-tests/js/jsunit.gresources.xml index 209274fc..75c54c90 100644 --- a/installed-tests/js/jsunit.gresources.xml +++ b/installed-tests/js/jsunit.gresources.xml @@ -13,6 +13,10 @@ <file>modules/badOverrides/Gio.js</file> <file>modules/badOverrides/Regress.js</file> <file>modules/badOverrides/WarnLib.js</file> + <file>modules/badOverrides2/GIMarshallingTests.js</file> + <file>modules/badOverrides2/Gio.js</file> + <file>modules/badOverrides2/Regress.js</file> + <file>modules/badOverrides2/WarnLib.js</file> <file>modules/subBadInit/__init__.js</file> <file>modules/subErrorInit/__init__.js</file> <file>modules/data.txt</file> diff --git a/installed-tests/js/meson.build b/installed-tests/js/meson.build index 5520a19d..6db887d2 100644 --- a/installed-tests/js/meson.build +++ b/installed-tests/js/meson.build @@ -127,6 +127,7 @@ jasmine_tests = [ 'GObjectValue', 'GTypeClass', 'Importer', + 'Importer2', 'Introspection', 'Lang', 'LegacyByteArray', diff --git a/installed-tests/js/modules/badOverrides2/.eslintrc.yml b/installed-tests/js/modules/badOverrides2/.eslintrc.yml new file mode 100644 index 00000000..b1c10f5d --- /dev/null +++ b/installed-tests/js/modules/badOverrides2/.eslintrc.yml @@ -0,0 +1,8 @@ +--- +# SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +# SPDX-FileCopyrightText: 2018 Philip Chimento <philip.chimento@gmail.com> +rules: + no-throw-literal: 'off' # these are intended to be bad code + no-unused-vars: + - error + - varsIgnorePattern: ^_init$ diff --git a/installed-tests/js/modules/badOverrides2/GIMarshallingTests.js b/installed-tests/js/modules/badOverrides2/GIMarshallingTests.js new file mode 100644 index 00000000..6cf2dfa9 --- /dev/null +++ b/installed-tests/js/modules/badOverrides2/GIMarshallingTests.js @@ -0,0 +1,5 @@ +// Sabotage the import of imports.gi.GIMarshallingTests! +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Philip Chimento <philip.chimento@gmail.com> + +var _init = {thingsThatThisObjectIsNot: ['callable']}; diff --git a/installed-tests/js/modules/badOverrides2/Gio.js b/installed-tests/js/modules/badOverrides2/Gio.js new file mode 100644 index 00000000..9758a7c0 --- /dev/null +++ b/installed-tests/js/modules/badOverrides2/Gio.js @@ -0,0 +1,5 @@ +// Sabotage the import of imports.gi.Gio! +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Philip Chimento <philip.chimento@gmail.com> + +var _init = null; diff --git a/installed-tests/js/modules/badOverrides2/Regress.js b/installed-tests/js/modules/badOverrides2/Regress.js new file mode 100644 index 00000000..e4252f67 --- /dev/null +++ b/installed-tests/js/modules/badOverrides2/Regress.js @@ -0,0 +1,5 @@ +// Sabotage the import of imports.gi.Regress! +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Philip Chimento <philip.chimento@gmail.com> + +var _init; diff --git a/installed-tests/js/modules/badOverrides2/WarnLib.js b/installed-tests/js/modules/badOverrides2/WarnLib.js new file mode 100644 index 00000000..b2c93b05 --- /dev/null +++ b/installed-tests/js/modules/badOverrides2/WarnLib.js @@ -0,0 +1,3 @@ +// Sabotage the import of imports.gi.WarnLib! +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Philip Chimento <philip.chimento@gmail.com> diff --git a/installed-tests/js/testImporter.js b/installed-tests/js/testImporter.js index 8380af20..f03bf634 100644 --- a/installed-tests/js/testImporter.js +++ b/installed-tests/js/testImporter.js @@ -32,7 +32,7 @@ describe('GI importer', function () { expect(() => imports.gi.Regress).toThrow('💩'); }); - it("throws an exception when the overrides _init isn't a function", function () { + it('throws an exception when the overrides _init is a primitive', function () { expect(() => imports.gi.Gio).toThrowError(/_init/); }); }); diff --git a/installed-tests/js/testImporter2.js b/installed-tests/js/testImporter2.js new file mode 100644 index 00000000..b4e71225 --- /dev/null +++ b/installed-tests/js/testImporter2.js @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Philip Chimento <philip.chimento@gmail.com> + +// This test is in a separate file instead of testImporter.js, because it tests +// loading overrides for g-i modules, and in the original file we have literally +// run out of g-i modules to override -- at least, the ones that we can assume +// will be present on any system where GJS is compiled. + +describe('GI importer', function () { + describe('on failure', function () { + // For these tests, we provide special overrides files to sabotage the + // import, at the path resource:///org/gjs/jsunit/modules/badOverrides2. + let oldSearchPath; + beforeAll(function () { + oldSearchPath = imports.overrides.searchPath.slice(); + imports.overrides.searchPath = ['resource:///org/gjs/jsunit/modules/badOverrides2']; + }); + + afterAll(function () { + imports.overrides.searchPath = oldSearchPath; + }); + + it("throws an exception when the overrides _init isn't a function", function () { + expect(() => imports.gi.GIMarshallingTests).toThrowError(/_init/); + }); + + it('throws an exception when the overrides _init is null', function () { + expect(() => imports.gi.Gio).toThrowError(/_init/); + }); + + it('throws an exception when the overrides _init is undefined', function () { + expect(() => imports.gi.Regress).toThrowError(/_init/); + }); + + it('throws an exception when the overrides _init is missing', function () { + expect(() => imports.gi.WarnLib).toThrowError(/_init/); + }); + }); +}); |