Skip to content
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

This code seems to violate strict aliasing rule #2381

Open
lifengxiang1025 opened this issue Feb 13, 2025 · 0 comments
Open

This code seems to violate strict aliasing rule #2381

lifengxiang1025 opened this issue Feb 13, 2025 · 0 comments

Comments

@lifengxiang1025
Copy link

folly/folly/FBString.h

Lines 691 to 708 in 27f765a

#ifndef FOLLY_SANITIZE_ADDRESS
if ((reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) == 0) {
const size_t byteSize = size * sizeof(Char);
constexpr size_t wordWidth = sizeof(size_t);
switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
case 3:
ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
[[fallthrough]];
case 2:
ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
[[fallthrough]];
case 1:
ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
[[fallthrough]];
case 0:
break;
}
} else

This code seems to violate strict aliasing rule ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));. I think it cause compiler's undefined behavior. After I add compiler flag "-fno-strict-aliasing" there is no odd output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant