Skip to content

Fix #54391: Invalid pointer aliasing in SAS7BDAT parser #54401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions pandas/_libs/byteswap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def read_float_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
assert offset + 4 < len(data)
cdef:
const char *data_ptr = data
float res = (<float*>(data_ptr + offset))[0]
float res = (<float *>(data_ptr + offset))[0]
if byteswap:
res = _byteswap_float(res)
return res
Expand All @@ -26,7 +26,7 @@ def read_double_with_byteswap(bytes data, Py_ssize_t offset, bint byteswap):
assert offset + 8 < len(data)
cdef:
const char *data_ptr = data
double res = (<double*>(data_ptr + offset))[0]
double res = (<double *>(data_ptr + offset))[0]
if byteswap:
res = _byteswap_double(res)
return res
Expand Down Expand Up @@ -81,13 +81,25 @@ cdef extern from *:
uint64_t _byteswap8(uint64_t)


cdef union Cast64:
uint64_t u
double d


cdef union Cast32:
uint32_t u
float f


cdef float _byteswap_float(float num):
cdef uint32_t *intptr = <uint32_t *>&num
intptr[0] = _byteswap4(intptr[0])
return num
cdef Cast32 cast
cast.f = num
cast.u = _byteswap4(cast.u)
return cast.f


cdef double _byteswap_double(double num):
cdef uint64_t *intptr = <uint64_t *>&num
intptr[0] = _byteswap8(intptr[0])
return num
cdef Cast64 cast
cast.d = num
cast.u = _byteswap8(cast.u)
return cast.d