From ad5948bbf5b511221689b80d5ecfdd1a8afa4163 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 28 Jun 2024 14:07:48 +0100 Subject: [PATCH] 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 Helps: #3405 --- gobject/gparamspecs.c | 2 +- gobject/tests/param.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gobject/gparamspecs.c b/gobject/gparamspecs.c index 12a81245a..9fa1b88c2 100644 --- a/gobject/gparamspecs.c +++ b/gobject/gparamspecs.c @@ -691,7 +691,7 @@ param_double_is_valid (GParamSpec *pspec, const GValue *value) { 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; } diff --git a/gobject/tests/param.c b/gobject/tests/param.c index 9d22135cc..47ec695e8 100644 --- a/gobject/tests/param.c +++ b/gobject/tests/param.c @@ -292,6 +292,20 @@ test_param_spec_double (void) g_assert_cmpint (g_value_get_double (&value), ==, 20.0); 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