From f8dd0dbfb43f81de4e084b8c9b810e6e1582ec7b Mon Sep 17 00:00:00 2001 From: Nik Pavlov Date: Thu, 5 Oct 2023 15:34:40 +0000 Subject: [PATCH] Avoid signed integer overflow UB in IdGenerator --- libcef_dll/wrapper/cef_message_router.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libcef_dll/wrapper/cef_message_router.cc b/libcef_dll/wrapper/cef_message_router.cc index 8d750e6a5..17e4b252e 100644 --- a/libcef_dll/wrapper/cef_message_router.cc +++ b/libcef_dll/wrapper/cef_message_router.cc @@ -4,6 +4,7 @@ #include "include/wrapper/cef_message_router.h" +#include #include #include @@ -164,7 +165,14 @@ ParsedMessage ParseMessage(const CefRefPtr& message) { 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::max()]. + * + * @tparam T The data type for the ID values. + */ template class IdGenerator { public: @@ -174,11 +182,10 @@ class IdGenerator { IdGenerator& operator=(const IdGenerator&) = delete; T GetNextId() { - T id = ++next_id_; - if (id == kReservedId) { // In case the integer value wraps. - id = ++next_id_; + if (next_id_ == std::numeric_limits::max()) { + next_id_ = kReservedId; } - return id; + return ++next_id_; } private: