girnode: Explicitly lose precision on parsed float values

When building a typelib, the values of constants need to be converted
from a string format (from the GIR) to a binary format. This is
currently done, for all numeric types, using `g_ascii_strto*()`
functions, but with minimal validation. String values which are not
representable as binary numbers are either silently truncated or
clamped.

`-Wfloat-conversion` has flagged that this happens for floats – a
double-precision return from `g_ascii_strtod()` is implicitly cast down
to a float.

While we should ideally have some better error handling so that
conversion to a typelib fails if a constant is not representable in the
typelib, this is a problem for *all* numeric types and not just `float`,
so add an explicit cast to ignore the error for now.

In practice there probably isn’t a problem for any numeric types here,
as there should be validation of the string value when the GIR is
generated anyway.

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

Helps: #3405
This commit is contained in:
Philip Withnall 2024-06-28 14:27:23 +01:00
parent ebe609eeef
commit d0cba9e6ec
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -2371,7 +2371,7 @@ gi_ir_node_build_typelib (GIIrNode *node,
break; break;
case GI_TYPE_TAG_FLOAT: case GI_TYPE_TAG_FLOAT:
blob->size = sizeof (float); blob->size = sizeof (float);
DO_ALIGNED_COPY (&data[blob->offset], parse_float_value (constant->value), float); DO_ALIGNED_COPY (&data[blob->offset], (float) parse_float_value (constant->value), float);
break; break;
case GI_TYPE_TAG_DOUBLE: case GI_TYPE_TAG_DOUBLE:
blob->size = sizeof (double); blob->size = sizeof (double);