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

Fix type conversion error in BitFlags Clear #30680

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/lib/support/BitFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BitFlags
*/
BitFlags & Clear(const BitFlags & other)
{
mValue &= ~other.mValue;
mValue &= static_cast<IntegerType>(~static_cast<IntegerType>(other.mValue));
fessehaeve marked this conversation as resolved.
Show resolved Hide resolved
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not right. The right thing is:

mValue = static_cast<IntegerType>(mValue & ~other.mValue);

or so. Because using &= causes the result of the & (which may have been promoted to a different type) to be assigned to mValue...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you provide a test showing this behaviour?

We relied a lot on unit tests to prove that bitflags works for us as expected.

Copy link
Contributor

@bzbarsky-apple bzbarsky-apple Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works correctly. When it compiles. It fails to compile on some compilers, I expect. Depending on optimization settings and the like. Because it triggers -Wconversion warnings due to assigning the int result of & into whatever type mValue is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a compile variant test for this? When this was fixed, we added a unit test that validated we compile.

return *this;
}

Expand Down
16 changes: 16 additions & 0 deletions src/lib/support/tests/TestBitMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,26 @@ void TestBitMaskInvalid(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, mask.Raw() == 0x1234);
}

void TestClear(nlTestSuite * inSuite, void * inContext)
{
BitMask<TestEnum> mask1;
BitMask<TestEnum> mask2;

mask1.Set(TestEnum::kBit_0);
mask1.Set(TestEnum::kBits_1_2);
mask1.Set(TestEnum::kBits_High8);

mask2.Set(TestEnum::kBits_1_2);
mask1.Clear(mask2);

NL_TEST_ASSERT(inSuite, mask1.Raw() == 0xFF01);
}

const nlTest sTests[] = {
NL_TEST_DEF("BitMask operations", TestBitMaskOperations), //
NL_TEST_DEF("BitFields logic", TestBitFieldLogic), //
NL_TEST_DEF("Invalid operations", TestBitMaskInvalid), //
NL_TEST_DEF("Clear operations", TestClear), //
NL_TEST_SENTINEL() //
};

Expand Down
Loading