[gjs/gnome-40] overrides/GLib: Guard Error.new_literal against invalid domains



commit 5b51db6544755ef34c06b163e2e38c7622bca731
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Aug 11 14:14:58 2021 +0200

    overrides/GLib: Guard Error.new_literal against invalid domains
    
    g_error_new_literal() only enforces the precondition that the domain
    must be greater than 0, but will crash later if it doesn't correspond
    to a valid GQuark.
    
    We can check for that in an override and throw an error if it doesn't.
    
    (If the GQuark is valid, it may or may not correspond to an error domain.
    We can't tell, but while a domain of `GLib.quark_from_string('void')` is
    weird, it doesn't do any harm)
    
    https://gitlab.gnome.org/GNOME/gjs/-/issues/433

 installed-tests/js/testExceptions.js | 22 +++++++++++-----------
 modules/core/overrides/GLib.js       | 10 ++++++++++
 2 files changed, 21 insertions(+), 11 deletions(-)
---
diff --git a/installed-tests/js/testExceptions.js b/installed-tests/js/testExceptions.js
index 662b5297..fbc32bc2 100644
--- a/installed-tests/js/testExceptions.js
+++ b/installed-tests/js/testExceptions.js
@@ -206,16 +206,16 @@ describe('thrown GError', function () {
     });
 });
 
-describe('Returned GError', function () {
-    it('can be nullable', function () {
-        // expect a critical about passing in an invalid error domain, but this
-        // should not crash or log a critical about freeing a null GError
-        GLib.test_expect_message('GLib', GLib.LogLevelFlags.LEVEL_CRITICAL,
-            "g_error_new_literal: assertion 'domain != 0' failed");
-
-        expect(GLib.Error.new_literal(0, 0, 'message')).toBeNull();
-
-        GLib.test_assert_expected_messages_internal('GLib', 'testExceptions.js',
-            0, 'testGErrorMessages');
+describe('GError.new_literal', function () {
+    it('constructs a valid GLib.Error', function () {
+        const e = GLib.Error.new_literal(
+            Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED, 'message');
+        expect(e instanceof GLib.Error).toBeTruthy();
+        expect(e.code).toEqual(Gio.IOErrorEnum.FAILED);
+        expect(e.message).toEqual('message');
+    });
+    it('does not accept invalid domains', function () {
+        expect(() => GLib.Error.new_literal(0, 0, 'message'))
+            .toThrowError(/0 is not a valid domain/);
     });
 });
diff --git a/modules/core/overrides/GLib.js b/modules/core/overrides/GLib.js
index 5e3800a9..3dfc85d3 100644
--- a/modules/core/overrides/GLib.js
+++ b/modules/core/overrides/GLib.js
@@ -275,6 +275,16 @@ function _init() {
         return false;
     };
 
+    // Guard against domains that aren't valid quarks and would lead
+    // to a crash
+    const quarkToString = this.quark_to_string;
+    const realNewLiteral = this.Error.new_literal;
+    this.Error.new_literal = function (domain, code, message) {
+        if (quarkToString(domain) === null)
+            throw new TypeError(`Error.new_literal: ${domain} is not a valid domain`);
+        return realNewLiteral(domain, code, message);
+    };
+
     this.Variant._new_internal = function (sig, value) {
         let signature = Array.prototype.slice.call(sig);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]