mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
AK: Use get_random() in IDAllocator
Also generate a new random ID on collision, instead of using the old ID + 1. :^) SonarCloud: https://sonarcloud.io/project/security_hotspots?id=SerenityOS_serenity&hotspots=AXuVPBMNk92xXUF3qWZd
This commit is contained in:
parent
eaf904000f
commit
9fd58fd6d8
Notes:
sideshowbarker
2024-07-18 05:05:11 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9fd58fd6d86
@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
|
#include <AK/Random.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
// This class manages a pool of random ID's in the range 1..INT32_MAX
|
||||||
class IDAllocator {
|
class IDAllocator {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -19,18 +21,16 @@ public:
|
|||||||
|
|
||||||
int allocate()
|
int allocate()
|
||||||
{
|
{
|
||||||
int r = rand();
|
VERIFY(m_allocated_ids.size() < (INT32_MAX - 2));
|
||||||
for (int i = 0; i < 100000; ++i) {
|
int id = 0;
|
||||||
int allocated_id = r + i;
|
for (;;) {
|
||||||
// Make sure we never vend ID 0, as some code may interpret that as "no ID"
|
id = static_cast<int>(get_random_uniform(NumericLimits<int>::max()));
|
||||||
if (allocated_id == 0)
|
if (id == 0)
|
||||||
++allocated_id;
|
continue;
|
||||||
if (!m_allocated_ids.contains(allocated_id)) {
|
if (m_allocated_ids.set(id) == AK::HashSetResult::InsertedNewEntry)
|
||||||
m_allocated_ids.set(allocated_id);
|
break;
|
||||||
return allocated_id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
VERIFY_NOT_REACHED();
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(int id)
|
void deallocate(int id)
|
||||||
|
Loading…
Reference in New Issue
Block a user