Avoid signed integer overflow UB in IdGenerator
This commit is contained in:
parent
bdec92caf8
commit
f8dd0dbfb4
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "include/wrapper/cef_message_router.h"
|
#include "include/wrapper/cef_message_router.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
|
@ -164,7 +165,14 @@ ParsedMessage ParseMessage(const CefRefPtr<CefProcessMessage>& message) {
|
||||||
return ParsedMessage{};
|
return ParsedMessage{};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper template for generated ID values.
|
/**
|
||||||
|
* @brief A helper template for generating ID values.
|
||||||
|
*
|
||||||
|
* This class generates monotonically increasing ID values within the interval
|
||||||
|
* [kReservedId + 1, numeric_limits<T>::max()].
|
||||||
|
*
|
||||||
|
* @tparam T The data type for the ID values.
|
||||||
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class IdGenerator {
|
class IdGenerator {
|
||||||
public:
|
public:
|
||||||
|
@ -174,11 +182,10 @@ class IdGenerator {
|
||||||
IdGenerator& operator=(const IdGenerator&) = delete;
|
IdGenerator& operator=(const IdGenerator&) = delete;
|
||||||
|
|
||||||
T GetNextId() {
|
T GetNextId() {
|
||||||
T id = ++next_id_;
|
if (next_id_ == std::numeric_limits<T>::max()) {
|
||||||
if (id == kReservedId) { // In case the integer value wraps.
|
next_id_ = kReservedId;
|
||||||
id = ++next_id_;
|
|
||||||
}
|
}
|
||||||
return id;
|
return ++next_id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue