[ 1346144 ] Segfaults from unaligned loads in floatobject.c

by using memcpy and not just blinding casting char* to double*.

Thanks to Rune Holm for the report.
This commit is contained in:
Michael W. Hudson 2005-12-05 00:27:49 +00:00
parent d54a0aed8e
commit b78a5fc004
1 changed files with 13 additions and 5 deletions

View File

@ -1631,20 +1631,24 @@ _PyFloat_Unpack4(const unsigned char *p, int le)
return x; return x;
} }
else { else {
float x;
if ((float_format == ieee_little_endian_format && !le) if ((float_format == ieee_little_endian_format && !le)
|| (float_format == ieee_big_endian_format && le)) { || (float_format == ieee_big_endian_format && le)) {
char buf[8]; char buf[4];
char *d = &buf[3]; char *d = &buf[3];
int i; int i;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
*d-- = *p++; *d-- = *p++;
} }
return *(float*)&buf[0]; memcpy(&x, buf, 4);
} }
else { else {
return *(float*)p; memcpy(&x, p, 4);
} }
return x;
} }
} }
@ -1722,6 +1726,8 @@ _PyFloat_Unpack8(const unsigned char *p, int le)
return x; return x;
} }
else { else {
double x;
if ((double_format == ieee_little_endian_format && !le) if ((double_format == ieee_little_endian_format && !le)
|| (double_format == ieee_big_endian_format && le)) { || (double_format == ieee_big_endian_format && le)) {
char buf[8]; char buf[8];
@ -1731,10 +1737,12 @@ _PyFloat_Unpack8(const unsigned char *p, int le)
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
*d-- = *p++; *d-- = *p++;
} }
return *(double*)&buf[0]; memcpy(&x, buf, 8);
} }
else { else {
return *(double*)p; memcpy(&x, p, 8);
} }
return x;
} }
} }