Skip to content

Commit 85e41f8

Browse files
committed
Fix float and double casting for property access. Access functions were working as is, but dump functionality output erroneous values and there was potential for some memory overwrite issues in the setter.
1 parent 5b0f733 commit 85e41f8

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

engine/source/openborscript/common_property.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
#include "scriptcommon.h"
22

3+
/*
4+
* Caskey, Damon V.
5+
* 2025-05-04
6+
*
7+
* Handle float/double get and set casting.
8+
*
9+
* This is used because script-side uses double,
10+
* but property fields are floats, so we need to
11+
* downcast carefully to avoid memory overwrites.
12+
*/
13+
14+
static inline double property_access_get_property_float(const void* field)
15+
{
16+
float value = *(float*)field;
17+
return (double)value;
18+
}
19+
20+
static inline void property_access_set_property_float(const void* field, const double value)
21+
{
22+
*(float*)field = (float)value;
23+
24+
//printf("DEBUG: Writing %.6f to float field at %p\n", value, field);
25+
}
26+
327
/*
428
* Caskey, Damon V.
529
* 2023-04-17
@@ -114,7 +138,7 @@ void property_access_dump_members(property_access_map get_property_map, const in
114138

115139
case VT_DECIMAL:
116140

117-
printf(" | %f", *(DOUBLE*)property_map.field);
141+
printf(" | %f", property_access_get_property_float(property_map.field));
118142
break;
119143
case VT_STR:
120144

@@ -188,7 +212,9 @@ HRESULT property_access_get_member(const s_property_access_map* property_map, Sc
188212
case VT_DECIMAL:
189213

190214
//printf("\n\t VT_DECIMAL");
191-
pretvar->dblVal = *(DOUBLE*)property_map->field;
215+
//pretvar->dblVal = *(DOUBLE*)property_map->field;
216+
pretvar->dblVal = property_access_get_property_float(property_map->field);
217+
192218
break;
193219
case VT_STR:
194220

@@ -256,8 +282,11 @@ HRESULT property_access_set_member(const void* const acting_object, const s_prop
256282
case VT_DECIMAL:
257283

258284
if (SUCCEEDED(ScriptVariant_DecimalValue(acting_value, &temp_float)))
259-
{
260-
*(DOUBLE*)property_map->field = temp_float;
285+
{
286+
property_access_set_property_float(property_map->field, temp_float);
287+
288+
// Legacy.
289+
//*(float*)property_map->field = (float)temp_float;
261290
}
262291
break;
263292

0 commit comments

Comments
 (0)