[gjs: 6/15] js: Handle errors in gjs_intern_string_to_id()



commit 085cc9cf271af48fc7358595fa7a6d438c8be10b
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Dec 9 20:47:53 2018 -0800

    js: Handle errors in gjs_intern_string_to_id()
    
    Previously, errors were not being handled here. We use JSID_VOID as the
    return value for the error case, as JSID_VOID should never be returned
    when interning a string to a jsid.

 gi/fundamental.cpp        |  2 ++
 gi/object.cpp             | 21 ++++++++++++++++-----
 gi/param.cpp              |  7 +++----
 gi/repo.cpp               |  2 ++
 gjs/importer.cpp          | 10 ++++++++--
 gjs/jsapi-class.h         |  2 ++
 gjs/jsapi-util-string.cpp |  2 ++
 gjs/jsapi-util.h          |  2 +-
 8 files changed, 36 insertions(+), 12 deletions(-)
---
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 5cecadc7..ad2e0e98 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -231,6 +231,8 @@ find_fundamental_constructor(JSContext          *context,
 
             name = g_base_info_get_name((GIBaseInfo *) func_info);
             constructor_name.set(gjs_intern_string_to_id(context, name));
+            if (constructor_name == JSID_VOID)
+                return nullptr;
 
             return func_info;
         }
diff --git a/gi/object.cpp b/gi/object.cpp
index fb46647c..5a5e7b86 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1013,7 +1013,10 @@ bool ObjectPrototype::new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
 
             if (flags & GI_FUNCTION_IS_METHOD) {
                 const char* name = meth_info.name();
-                if (!properties.append(gjs_intern_string_to_id(cx, name))) {
+                jsid id = gjs_intern_string_to_id(cx, name);
+                if (id == JSID_VOID)
+                    return false;
+                if (!properties.append(id)) {
                     JS_ReportOutOfMemory(cx);
                     return false;
                 }
@@ -1028,7 +1031,10 @@ bool ObjectPrototype::new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
 
             GjsAutoChar js_name = gjs_hyphen_to_underscore(prop_info.name());
 
-            if (!properties.append(gjs_intern_string_to_id(cx, js_name))) {
+            jsid id = gjs_intern_string_to_id(cx, js_name);
+            if (id == JSID_VOID)
+                return false;
+            if (!properties.append(id)) {
                 JS_ReportOutOfMemory(cx);
                 return false;
             }
@@ -1046,7 +1052,10 @@ bool ObjectPrototype::new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
 
             if (flags & GI_FUNCTION_IS_METHOD) {
                 const char* name = meth_info.name();
-                if (!properties.append(gjs_intern_string_to_id(cx, name))) {
+                jsid id = gjs_intern_string_to_id(cx, name);
+                if (id == JSID_VOID)
+                    return false;
+                if (!properties.append(id)) {
                     JS_ReportOutOfMemory(cx);
                     return false;
                 }
@@ -1060,8 +1069,10 @@ bool ObjectPrototype::new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
                 g_object_info_get_property(info(), i);
 
             GjsAutoChar js_name = gjs_hyphen_to_underscore(prop_info.name());
-
-            if (!properties.append(gjs_intern_string_to_id(cx, js_name))) {
+            jsid id = gjs_intern_string_to_id(cx, js_name);
+            if (id == JSID_VOID)
+                return false;
+            if (!properties.append(id)) {
                 JS_ReportOutOfMemory(cx);
                 return false;
             }
diff --git a/gi/param.cpp b/gi/param.cpp
index e4268774..15b14f9c 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -163,14 +163,13 @@ GJS_JSAPI_RETURN_CONVENTION
 static JSObject*
 gjs_lookup_param_prototype(JSContext    *context)
 {
-    JS::RootedId gobject_name(context, gjs_intern_string_to_id(context, "GObject"));
-    JS::RootedObject in_object(context,
-        gjs_lookup_namespace_object_by_name(context, gobject_name));
+    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
+    JS::RootedObject in_object(
+        context, gjs_lookup_namespace_object_by_name(context, atoms.gobject()));
 
     if (G_UNLIKELY (!in_object))
         return NULL;
 
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
     JS::RootedValue value(context);
     if (!JS_GetPropertyById(context, in_object, atoms.param_spec(), &value))
         return NULL;
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 40a45c87..7b0c9fd4 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -547,6 +547,8 @@ gjs_lookup_namespace_object(JSContext  *context,
     }
 
     JS::RootedId ns_name(context, gjs_intern_string_to_id(context, ns));
+    if (ns_name == JSID_VOID)
+        return nullptr;
     return gjs_lookup_namespace_object_by_name(context, ns_name);
 }
 
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index b43b8cf8..4b4963f3 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -716,7 +716,10 @@ static bool importer_new_enumerate(JSContext* context, JS::HandleObject object,
                 continue;
 
             if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY) {
-                if (!properties.append(gjs_intern_string_to_id(context, filename))) {
+                jsid id = gjs_intern_string_to_id(context, filename);
+                if (id == JSID_VOID)
+                    return false;
+                if (!properties.append(id)) {
                     JS_ReportOutOfMemory(context);
                     return false;
                 }
@@ -724,7 +727,10 @@ static bool importer_new_enumerate(JSContext* context, JS::HandleObject object,
                        g_str_has_suffix(filename, ".js")) {
                 GjsAutoChar filename_noext =
                     g_strndup(filename, strlen(filename) - 3);
-                if (!properties.append(gjs_intern_string_to_id(context, filename_noext))) {
+                jsid id = gjs_intern_string_to_id(context, filename_noext);
+                if (id == JSID_VOID)
+                    return false;
+                if (!properties.append(id)) {
                     JS_ReportOutOfMemory(context);
                     return false;
                 }
diff --git a/gjs/jsapi-class.h b/gjs/jsapi-class.h
index 514a10d0..bbd6ea03 100644
--- a/gjs/jsapi-class.h
+++ b/gjs/jsapi-class.h
@@ -244,6 +244,8 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
         JS::RootedObject ctor_obj(cx);                                         \
         JS::RootedId class_name(                                               \
             cx, gjs_intern_string_to_id(cx, gjs_##cname##_class.name));        \
+        if (class_name == JSID_VOID)                                           \
+            return false;                                                      \
         if (!gjs_object_require_property(cx, in_obj, #cname " constructor",    \
                                          class_name, &ctor_obj))               \
             return false;                                                      \
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 97f41768..e1107f95 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -366,6 +366,8 @@ gjs_intern_string_to_id(JSContext  *cx,
 {
     JSAutoRequest ar(cx);
     JS::RootedString str(cx, JS_AtomizeAndPinString(cx, string));
+    if (!str)
+        return JSID_VOID;
     return INTERNED_STRING_TO_JSID(cx, str);
 }
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 664ded20..56c2e5a2 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -285,7 +285,7 @@ bool gjs_string_from_ucs4(JSContext             *cx,
 
 GJS_JSAPI_RETURN_CONVENTION
 bool gjs_get_string_id(JSContext* cx, jsid id, JS::UniqueChars* name_p);
-GJS_USE
+GJS_JSAPI_RETURN_CONVENTION
 jsid        gjs_intern_string_to_id          (JSContext       *context,
                                               const char      *string);
 


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