-
Notifications
You must be signed in to change notification settings - Fork 92
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
Support build cython codes with -Werror=strict-aliasing
#247
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ import sys | |||||||||||
|
||||||||||||
from libc.stdlib cimport free, malloc | ||||||||||||
from libc.stdint cimport int16_t, int32_t, int64_t | ||||||||||||
from libc.string cimport memcpy | ||||||||||||
from cpython cimport bool | ||||||||||||
|
||||||||||||
import six | ||||||||||||
|
@@ -99,7 +100,9 @@ cdef inline int write_i64(CyTransportBase buf, int64_t val) except -1: | |||||||||||
|
||||||||||||
|
||||||||||||
cdef inline int write_double(CyTransportBase buf, double val) except -1: | ||||||||||||
cdef int64_t v = htobe64((<int64_t*>(&val))[0]) | ||||||||||||
cdef int64_t v | ||||||||||||
memcpy(&v, &val, 8) | ||||||||||||
v = htobe64(v) | ||||||||||||
buf.c_write(<char*>(&v), 8) | ||||||||||||
return 0 | ||||||||||||
|
||||||||||||
|
@@ -269,6 +272,7 @@ cdef c_read_val(CyTransportBase buf, TType ttype, spec=None, | |||||||||||
cdef int size | ||||||||||||
cdef int64_t n | ||||||||||||
cdef TType v_type, k_type, orig_type, orig_key_type | ||||||||||||
cdef double double_value | ||||||||||||
|
||||||||||||
if ttype == T_BOOL: | ||||||||||||
return <bint>read_i08(buf) | ||||||||||||
|
@@ -287,7 +291,8 @@ cdef c_read_val(CyTransportBase buf, TType ttype, spec=None, | |||||||||||
|
||||||||||||
elif ttype == T_DOUBLE: | ||||||||||||
n = read_i64(buf) | ||||||||||||
return (<double*>(&n))[0] | ||||||||||||
memcpy(&double_value, &n, 8) | ||||||||||||
return double_value | ||||||||||||
Comment on lines
+294
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of + # Using memcpy to safely convert int64_t to double, adhering to strict-aliasing rules
memcpy(&double_value, &n, 8) Committable suggestion
Suggested change
|
||||||||||||
|
||||||||||||
elif ttype == T_BINARY: | ||||||||||||
size = read_i32(buf) | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
memcpy
inwrite_double
correctly addresses the strict-aliasing violation by safely copying the double value. It's a good practice to add a brief comment explaining the reason behind usingmemcpy
here, for the benefit of future maintainers.+ # Using memcpy to comply with strict-aliasing rules memcpy(&v, &val, 8)
Committable suggestion