Skip to content

Commit

Permalink
Merged Pull Request '#37 profile-id-underflow->main : Fix underflow w…
Browse files Browse the repository at this point in the history
…hen looking for missing profile ID.'

Fix underflow when looking for missing profile ID.
  • Loading branch information
Automation51D authored Dec 19, 2023
2 parents 0a5f01a + 018e27e commit b51e400
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,12 @@ long fiftyoneDegreesCollectionBinarySearch(
return middle;
}
else if (comparisonResult > 0) {
upper = middle - 1;
if (middle) { // guard against underflow of unsigned type
upper = middle - 1;
}
else {
lower += 1; // break once iteration finishes
}
}
else {
lower = middle + 1;
Expand Down
32 changes: 31 additions & 1 deletion tests/CollectionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ class CollectionTest : public Base {
}
}

void binarySearch_notFound() {
if (this->data->isCount == false) {
cout << "Skipping binary search test for as the collection "
<< "contains variable size elements.\n";
}
else {
FIFTYONE_DEGREES_EXCEPTION_CREATE;
fiftyoneDegreesCollectionItem resultItem, targetItem;
fiftyoneDegreesDataReset(&resultItem.data);
fiftyoneDegreesDataReset(&targetItem.data);

int DummyIDs[] = {
-5,
(int)data->count + 3,
};
for (uint32_t i = 0; i < sizeof(DummyIDs) / sizeof(DummyIDs[0]); i++) {
EXPECT_EQ(-1, fiftyoneDegreesCollectionBinarySearch(
collection,
&resultItem,
0,
data->count - 1,
&(DummyIDs[i]),
data->itemComparer,
exception));
EXPECT_FALSE(FIFTYONE_DEGREES_EXCEPTION_FAILED);
}
}
}

void outOfRange() {
FIFTYONE_DEGREES_EXCEPTION_CREATE
fiftyoneDegreesCollectionItem item;
Expand Down Expand Up @@ -680,7 +709,8 @@ TEST_F(CollectionTest##s##w##e##o, Random) { random(); } \
TEST_F(CollectionTest##s##w##e##o, RandomOutOfRange) { random(); outOfRange(); } \
TEST_F(CollectionTest##s##w##e##o, RandomMultiThreaded) { randomMultiThreaded(); } \
TEST_F(CollectionTest##s##w##e##o, List) { list(0.1); } \
TEST_F(CollectionTest##s##w##e##o, BinarySearch) { binarySearch(); }
TEST_F(CollectionTest##s##w##e##o, BinarySearch) { binarySearch(); } \
TEST_F(CollectionTest##s##w##e##o, BinarySearchNotFound) { binarySearch_notFound(); }

/* Configs to test. */
#define COLLECTION_TEST_THREADS 4
Expand Down

0 comments on commit b51e400

Please sign in to comment.