From f6374c8308ba9749e3784ca7469b11d297beb718 Mon Sep 17 00:00:00 2001 From: Sundeep Mediratta Date: Mon, 6 Mar 2023 19:58:46 -0500 Subject: Use the mask for the flagsclass to compute valid values Fix Errors --- gi/arg.cpp | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) (limited to 'gi') diff --git a/gi/arg.cpp b/gi/arg.cpp index ef94cea7..1367e7ec 100644 --- a/gi/arg.cpp +++ b/gi/arg.cpp @@ -65,34 +65,18 @@ static void throw_invalid_argument(JSContext* cx, JS::HandleValue value, GjsArgumentType arg_type); bool _gjs_flags_value_is_valid(JSContext* context, GType gtype, int64_t value) { - GFlagsValue *v; - guint32 tmpval; + /* Do proper value check for flags with GType's */ + if (gtype != G_TYPE_NONE) { + GjsAutoTypeClass gflags_class(gtype); + uint32_t tmpval = static_cast(value); - /* FIXME: Do proper value check for flags with GType's */ - if (gtype == G_TYPE_NONE) - return true; - - GjsAutoTypeClass klass(gtype); - - /* check all bits are defined for flags.. not necessarily desired */ - tmpval = (guint32)value; - if (tmpval != value) { /* Not a guint32 */ - gjs_throw(context, - "0x%" G_GINT64_MODIFIER "x is not a valid value for flags %s", - value, g_type_name(G_TYPE_FROM_CLASS(klass))); - return false; - } - - while (tmpval) { - v = g_flags_get_first_value(klass.as(), tmpval); - if (!v) { + /* check all bits are valid bits for the flag and is a 32 bit flag*/ + if ((tmpval &= gflags_class->mask) != value) { /* Not a guint32 with invalid mask values*/ gjs_throw(context, - "0x%x is not a valid value for flags %s", - (guint32)value, g_type_name(G_TYPE_FROM_CLASS(klass))); + "0x%" G_GINT64_MODIFIER "x is not a valid value for flags %s", + value, g_type_name(gtype)); return false; } - - tmpval &= ~v->value; } return true; @@ -2584,8 +2568,20 @@ gjs_value_from_g_argument (JSContext *context, gjs_arg_get(arg)); gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)interface_info); - if (!_gjs_flags_value_is_valid(context, gtype, value_int64)) - return false; + + if (gtype != G_TYPE_NONE) { + /* check make sure 32 bit flag */ + if (static_cast(value_int64) != value_int64) { /* Not a guint32 */ + gjs_throw(context, + "0x%" G_GINT64_MODIFIER "x is not a valid value for flags %s", + value_int64, g_type_name(gtype)); + return false; + } + + /* Pass only valid values*/ + GjsAutoTypeClass gflags_class(gtype); + value_int64 &= gflags_class->mask; + } value_p.setNumber(static_cast(value_int64)); return true; -- cgit v1.2.1