Skip to content

Commit 000e93a

Browse files
committed
Fix pandas-dev#54391: Invalid pointer aliasing in SAS7BDAT parser
1 parent 92d1d6a commit 000e93a

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

pandas/_libs/byteswap.pyx

+20-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def read_float_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
1616
assert offset + 4 < len(data)
1717
cdef:
1818
const char *data_ptr = data
19-
float res = (<float*>(data_ptr + offset))[0]
19+
float res = (<float *>(data_ptr + offset))[0]
2020
if byteswap:
2121
res = _byteswap_float(res)
2222
return res
@@ -26,7 +26,7 @@ def read_double_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
2626
assert offset + 8 < len(data)
2727
cdef:
2828
const char *data_ptr = data
29-
double res = (<double*>(data_ptr + offset))[0]
29+
double res = (<double *>(data_ptr + offset))[0]
3030
if byteswap:
3131
res = _byteswap_double(res)
3232
return res
@@ -81,13 +81,25 @@ cdef extern from *:
8181
uint64_t _byteswap8(uint64_t)
8282

8383

84+
cdef union Cast64:
85+
uint64_t u
86+
double d
87+
88+
89+
cdef union Cast32:
90+
uint32_t u
91+
float f
92+
93+
8494
cdef float _byteswap_float(float num):
85-
cdef uint32_t *intptr = <uint32_t *>&num
86-
intptr[0] = _byteswap4(intptr[0])
87-
return num
95+
cdef Cast32 cast
96+
cast.f = num
97+
cast.u = _byteswap4(cast.u)
98+
return cast.f
8899

89100

90101
cdef double _byteswap_double(double num):
91-
cdef uint64_t *intptr = <uint64_t *>&num
92-
intptr[0] = _byteswap8(intptr[0])
93-
return num
102+
cdef Cast64 cast
103+
cast.d = num
104+
cast.u = _byteswap8(cast.u)
105+
return cast.d

0 commit comments

Comments
 (0)