Fix dna_genfile error converting signed int types to floating point

Regression in 51befa4108
caused negative values to overflow into a uint64_t.

Resolve by casting to a signed int from originally signed types.

This caused D14033 not to work properly.
This commit is contained in:
Campbell Barton 2022-02-08 11:50:15 +11:00 committed by Philipp Oeser
parent 05f631b7ad
commit 87b77a97b9
Notes: blender-bot 2023-02-13 12:29:31 +01:00
Referenced by issue #88449: Blender LTS: Maintenance Task 2.93
Referenced by issue #88449, Blender LTS: Maintenance Task 2.93
1 changed files with 7 additions and 4 deletions

View File

@ -752,6 +752,9 @@ static void cast_primitive_type(const eSDNA_Type old_type,
const int curlen = DNA_elem_type_size(new_type);
double old_value_f = 0.0;
/* Intentionally overflow signed values into an unsigned type.
* Casting back to a signed value preserves the sign (when the new value is signed).
* It's also important to cast to `int64_t` when setting `old_value_f` from a signed value. */
uint64_t old_value_i = 0;
for (int a = 0; a < array_len; a++) {
@ -771,7 +774,7 @@ static void cast_primitive_type(const eSDNA_Type old_type,
case SDNA_TYPE_SHORT: {
const short value = *((short *)old_data);
old_value_i = value;
old_value_f = (double)old_value_i;
old_value_f = (double)(int64_t)old_value_i;
break;
}
case SDNA_TYPE_USHORT: {
@ -783,7 +786,7 @@ static void cast_primitive_type(const eSDNA_Type old_type,
case SDNA_TYPE_INT: {
const int value = *((int *)old_data);
old_value_i = value;
old_value_f = (double)old_value_i;
old_value_f = (double)(int64_t)old_value_i;
break;
}
case SDNA_TYPE_FLOAT: {
@ -801,7 +804,7 @@ static void cast_primitive_type(const eSDNA_Type old_type,
case SDNA_TYPE_INT64: {
const int64_t value = *((int64_t *)old_data);
old_value_i = (uint64_t)value;
old_value_f = (double)old_value_i;
old_value_f = (double)(int64_t)old_value_i;
break;
}
case SDNA_TYPE_UINT64: {
@ -813,7 +816,7 @@ static void cast_primitive_type(const eSDNA_Type old_type,
case SDNA_TYPE_INT8: {
const int8_t value = *((int8_t *)old_data);
old_value_i = (uint64_t)value;
old_value_f = (double)old_value_i;
old_value_f = (double)(int64_t)old_value_i;
}
}