Skip to content
Open
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 hazelcast/src/hazelcast/client/client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ twos_complement(std::vector<int8_t>& a)
}
// add 1
int8_t carry = 1;
for (auto i = a.size() - 1; i >= 0; i--) {
for (int i = static_cast<int>(a.size()) - 1; i >= 0; i--) {
a[i] = a[i] + carry;
if (a[i] == 0) {
carry = 1;
Expand Down
18 changes: 18 additions & 0 deletions hazelcast/test/src/HazelcastTests2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ TEST_F(DecimalTest, cascading_carry_bit_test)
{ -67, 19, -58, 119, -111, -77, 0, 0, 0 });
}

TEST_F(DecimalTest, twos_complement_single_byte_full_carry)
{
// carry propagates out of the only byte; the signed loop index must exit
// cleanly
std::vector<int8_t> v = { 0 };
pimpl::twos_complement(v);
ASSERT_EQ((std::vector<int8_t>{ 0 }), v);
}

TEST_F(DecimalTest, twos_complement_multi_byte_full_carry)
{
// carry propagates out of every byte; a size_t loop index would wrap to
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what does this comment mean? you changed size_t usage to int int he loop at this PR.

Copy link
Copy Markdown
Member Author

@emreyigit emreyigit Apr 20, 2026

Choose a reason for hiding this comment

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

Yes, only way to test it is calling the function and verifying its result. I just explained the situation what was it before. Also, I couldn't find any test on twos_complement, so I added single and multiple vector size test which verifies both loop and the function.

Copy link
Copy Markdown
Collaborator

@ihsandemir ihsandemir Apr 20, 2026

Choose a reason for hiding this comment

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

Actually, there were existing tests for the complement (see DecimalTest::.... test cases above your added test. e.g. TEST_F(DecimalTest, carry_bit_test)) but not for this exact scenario. you need a specific value to actually see the index goes all the way to 0. Your added tests are OK but they do not catch the infinite loop problem, right? Does you test catch the failure without the fix?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The test should verify the loop as well since it was discarding the carry bit before due to unsigned int casting.

// SIZE_MAX
std::vector<int8_t> v = { 0, 0, 0 };
pimpl::twos_complement(v);
ASSERT_EQ((std::vector<int8_t>{ 0, 0, 0 }), v);
}

class AddressHelperTest : public ClientTest
{};

Expand Down
Loading