-
Notifications
You must be signed in to change notification settings - Fork 480
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
Question: Violation of Strict Aliasing Rule ? #130
Comments
@Ursescu thanks for reaching out. |
One pointer is said to alias another pointer when both refer to the same location or object. In the code the sh is a char* pointer. And Further digging: C99 - 6.5 Expressions:
So in other words it is allowed to alias a struct pointer with char * pointer, but this won't work the other way: there's no assumption that your struct aliases a buffer of chars. |
Ohh, So that's about compiler optimizations re-ordering statements or caching memory in registers. I'll need to look deeper when i get a chance. If you happen to have a solution, please make a PR to https://github.com/redis/redis |
When a new sbs is created (as a char array) it is casted to struct type pointer, ex. SDS_HDR(8,s);. Isn't this violating the strict aliasing rule?
(Dereferencing a pointer that aliases an object that is not of a compatible type or one of the other types allowed by C 2011 6.5 paragraph 71 is undefined behavior)
Example:
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
The text was updated successfully, but these errors were encountered: