Skip to content

Commit 66e0f5a

Browse files
committed
CBD-6348: [BP] [ubsan] Let random value be uint32_t
The comment states it may be any value, but ubsan complains with: hash_table.cc:629:22: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself Just flip the value to uint32_t as that's what it really is Change-Id: I89c6e027360c12e3d2498bbb3bc54bd9b73b3275 Reviewed-on: https://review.couchbase.org/c/kv_engine/+/235073 Tested-by: Trond Norbye <[email protected]> Well-Formed: Restriction Checker Reviewed-by: Jim Walker <[email protected]>
1 parent 87cf079 commit 66e0f5a

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

engines/ep/src/hash_table.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ HashTable::UnlockedFindResult HashTable::unlocked_find(
365365
return {foundCmt, foundPend};
366366
}
367367

368-
HashTable::RandomKeyVisitor::RandomKeyVisitor(size_t size, int random)
369-
: random(std::abs(random)) {
368+
HashTable::RandomKeyVisitor::RandomKeyVisitor(size_t size, uint32_t random)
369+
: random(random) {
370370
setup(size);
371371
}
372372

@@ -397,11 +397,11 @@ void HashTable::RandomKeyVisitor::setup(size_t size) {
397397
}
398398

399399
currentSize = size;
400-
currentBucket = random % (currentSize);
400+
currentBucket = random % static_cast<uint32_t>(currentSize);
401401
bucketsVisited = 0;
402402
}
403403

404-
std::unique_ptr<Item> HashTable::getRandomKey(CollectionID cid, int rnd) {
404+
std::unique_ptr<Item> HashTable::getRandomKey(CollectionID cid, uint32_t rnd) {
405405
return getRandomKey(cid, RandomKeyVisitor{getSize(), rnd});
406406
}
407407

engines/ep/src/hash_table.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ class HashTable {
937937
*
938938
* @return an item -- NULL if not fount
939939
*/
940-
std::unique_ptr<Item> getRandomKey(CollectionID cid, int random);
940+
std::unique_ptr<Item> getRandomKey(CollectionID cid, uint32_t random);
941941

942942
/**
943943
* Set an Item into the this hashtable
@@ -1490,7 +1490,7 @@ class HashTable {
14901490
* should be the result of a random generator. There is no limit
14911491
* on this value.
14921492
*/
1493-
RandomKeyVisitor(size_t size, int random);
1493+
RandomKeyVisitor(size_t size, uint32_t random);
14941494

14951495
/**
14961496
* Return the bucket to use, this internally increments (and wraps) for
@@ -1517,7 +1517,7 @@ class HashTable {
15171517
size_t currentSize{0};
15181518
size_t currentBucket{0};
15191519
size_t bucketsVisited{0};
1520-
int random{0};
1520+
uint32_t random{0};
15211521
};
15221522

15231523
/**

engines/ep/tests/module_tests/hash_table_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,12 +1244,12 @@ class GetRandomHashTable : public HashTable {
12441244
: HashTable(st, std::move(svFactory), initialSize, locks) {
12451245
}
12461246

1247-
void testIntialiseVisitor(size_t size, int random) {
1247+
void testIntialiseVisitor(size_t size, uint32_t random) {
12481248
HashTable::RandomKeyVisitor visitor{size, random};
12491249
EXPECT_LT(visitor.getNextBucket(), size);
12501250
}
12511251

1252-
void testVisitor(size_t size, int random) {
1252+
void testVisitor(size_t size, uint32_t random) {
12531253
HashTable::RandomKeyVisitor visitor{size, random};
12541254
EXPECT_FALSE(visitor.visitComplete());
12551255
EXPECT_FALSE(visitor.maybeReset(size));
@@ -1294,7 +1294,7 @@ class GetRandomHashTable : public HashTable {
12941294

12951295
// Initialise the visitor so it computes a start point very close to the
12961296
// end a place which definitely does not exist after the resize
1297-
HashTable::RandomKeyVisitor visitor{getSize(), int(size - 10)};
1297+
HashTable::RandomKeyVisitor visitor{getSize(), uint32_t(size - 10)};
12981298

12991299
// Ensure if resize is called after the visitor is constructed
13001300
// getRandomKey does not generate any faults
@@ -1318,7 +1318,7 @@ class GetRandomHashTable : public HashTable {
13181318

13191319
// Initialise the visitor so it computes a start point very close to the
13201320
// end a place which definitely does not exist after the resize
1321-
HashTable::RandomKeyVisitor visitor{getSize(), int(size - 10)};
1321+
HashTable::RandomKeyVisitor visitor{getSize(), uint32_t(size - 10)};
13221322

13231323
// Increase the keys in the HT
13241324
keys = generateKeys(500, 100 /*start key*/);

0 commit comments

Comments
 (0)