diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2023-04-05 04:46:34 +0000 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2023-04-05 04:46:34 +0000 |
commit | 1b78dec51c12379c332246854760b038355984da (patch) | |
tree | fa6a21657bb495264e609b97249d1dfd3b1b1f0c | |
parent | c8f4b63cdd236ebfb23094a54050ebe156cd44cb (diff) | |
parent | c484bd393e7609933b2729f2bcb4276cb0742364 (diff) | |
download | gjs-1b78dec51c12379c332246854760b038355984da.tar.gz |
Merge branch 'sanitize-mask-return' into 'master'
Use the mask for the GFlagsClass to compute valid values for flags passed from C
Closes #507
See merge request GNOME/gjs!829
-rw-r--r-- | gi/arg.cpp | 48 | ||||
-rwxr-xr-x | test/test-ci.sh | 2 |
2 files changed, 23 insertions, 27 deletions
@@ -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<GFlagsClass> gflags_class(gtype); + uint32_t tmpval = static_cast<uint32_t>(value); - /* FIXME: Do proper value check for flags with GType's */ - if (gtype == G_TYPE_NONE) - return true; - - GjsAutoTypeClass<GTypeClass> 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<GFlagsClass>(), 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<int, GI_TYPE_TAG_INTERFACE>(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<uint32_t>(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<GFlagsClass> gflags_class(gtype); + value_int64 &= gflags_class->mask; + } value_p.setNumber(static_cast<double>(value_int64)); return true; diff --git a/test/test-ci.sh b/test/test-ci.sh index 0905340f..757dc02e 100755 --- a/test/test-ci.sh +++ b/test/test-ci.sh @@ -48,7 +48,7 @@ do_Get_Upstream_Base () { git fetch --depth=30 --no-tags upstream "$base_branch" fi - git branch ci-upstream-base-branch FETCH_HEAD + git branch -f ci-upstream-base-branch FETCH_HEAD # Work out the newest common ancestor between the detached HEAD that this CI # job has checked out, and the upstream target branch (which will typically |