gparamspecs: Fix loss of precision when validating a double-typed pspec

As spotted by `-Wfloat-conversion`. Doubles which could not be
accurately represented as floats may have erroneously failed bounds
validation.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
This commit is contained in:
Philip Withnall 2024-06-28 14:07:48 +01:00
parent 616749c1e2
commit ad5948bbf5
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73
2 changed files with 15 additions and 1 deletions

View File

@ -691,7 +691,7 @@ param_double_is_valid (GParamSpec *pspec,
const GValue *value) const GValue *value)
{ {
GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (pspec); GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (pspec);
gfloat oval = value->data[0].v_double; gdouble oval = value->data[0].v_double;
return dspec->minimum <= oval && oval <= dspec->maximum; return dspec->minimum <= oval && oval <= dspec->maximum;
} }

View File

@ -292,6 +292,20 @@ test_param_spec_double (void)
g_assert_cmpint (g_value_get_double (&value), ==, 20.0); g_assert_cmpint (g_value_get_double (&value), ==, 20.0);
g_param_spec_unref (pspec); g_param_spec_unref (pspec);
/* Test validation with some larger values, which fit in a double but not in a float.
* In particular, check there are no double float conversions in the pipeline,
* by using a valid range which is entirely outside the range of numbers
* representable in a float. */
pspec = g_param_spec_double ("double", NULL, NULL,
1.2e308, 1.3e308, 1.21e308, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpfloat (g_value_get_double (&value), ==, 1.21e308);
g_param_spec_unref (pspec);
} }
static void static void