1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix unaligned access to double in RFloat

This commit is contained in:
Nobuyoshi Nakada 2021-10-26 20:14:19 +09:00
parent e1ecda297e
commit afdca0e780
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
2 changed files with 16 additions and 0 deletions

View file

@ -206,10 +206,22 @@ rb_float_flonum_value(VALUE v)
return 0.0; return 0.0;
} }
#if SIZEOF_VALUE >= SIZEOF_DOUBLE || defined(UNALIGNED_WORD_ACCESS)
# define UNALIGNED_DOUBLE_ACCESS 1
#else
# define UNALIGNED_DOUBLE_ACCESS 0
#endif
static inline double static inline double
rb_float_noflonum_value(VALUE v) rb_float_noflonum_value(VALUE v)
{ {
#if UNALIGNED_DOUBLE_ACCESS
return RFLOAT(v)->float_value; return RFLOAT(v)->float_value;
#else
double d;
memcpy(&d, &RFLOAT(v)->float_value, sizeof(double));
return d;
#endif
} }
static inline double static inline double

View file

@ -951,7 +951,11 @@ rb_float_new_in_heap(double d)
{ {
NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT | (RGENGC_WB_PROTECTED_FLOAT ? FL_WB_PROTECTED : 0)); NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT | (RGENGC_WB_PROTECTED_FLOAT ? FL_WB_PROTECTED : 0));
#if UNALIGNED_DOUBLE_ACCESS
flt->float_value = d; flt->float_value = d;
#else
memcpy(&flt->float_value, &d, sizeof(double));
#endif
OBJ_FREEZE((VALUE)flt); OBJ_FREEZE((VALUE)flt);
return (VALUE)flt; return (VALUE)flt;
} }