From 64a593f383a08224c00ec2253bfc8186019e0945 Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Wed, 26 Aug 2015 14:42:19 +0300 Subject: [PATCH] PR fixes --- search/query_saver.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/search/query_saver.cpp b/search/query_saver.cpp index 97ba6cd5fc..659bb96c30 100644 --- a/search/query_saver.cpp +++ b/search/query_saver.cpp @@ -9,23 +9,22 @@ #include "base/logging.hpp" +#include "std/limits.hpp" + namespace { char constexpr kSettingsKey[] = "UserQueries"; using TLength = uint16_t; -TLength constexpr kMaxSuggestCount = 10; +TLength constexpr kMaxSuggestionsCount = 10; size_t constexpr kLengthTypeSize = sizeof(TLength); // Reader from memory that throws exceptions. class SecureMemReader : public Reader { - bool CheckPosAndSize(uint64_t pos, uint64_t size) const + void CheckPosAndSize(uint64_t pos, uint64_t size) const { - bool const ret1 = (pos + size <= m_size); - bool const ret2 = (size <= static_cast(-1)); - if (!ret1 || !ret2) + if (pos + size > m_size || size > numeric_limits::max()) MYTHROW(SizeException, (pos, size, m_size) ); - return (ret1 && ret2); } public: @@ -46,16 +45,16 @@ public: memcpy(p, m_pData + pos, size); } - inline MemReader SubReader(uint64_t pos, uint64_t size) const + inline SecureMemReader SubReader(uint64_t pos, uint64_t size) const { CheckPosAndSize(pos, size); - return MemReader(m_pData + pos, static_cast(size)); + return SecureMemReader(m_pData + pos, static_cast(size)); } - inline MemReader * CreateSubReader(uint64_t pos, uint64_t size) const + inline SecureMemReader * CreateSubReader(uint64_t pos, uint64_t size) const { CheckPosAndSize(pos, size); - return new MemReader(m_pData + pos, static_cast(size)); + return new SecureMemReader(m_pData + pos, static_cast(size)); } private: @@ -81,7 +80,7 @@ void QuerySaver::Add(string const & query) auto const it = find(m_topQueries.begin(), m_topQueries.end(), query); if (it != m_topQueries.end()) m_topQueries.erase(it); - else if (m_topQueries.size() >= kMaxSuggestCount) + else if (m_topQueries.size() >= kMaxSuggestionsCount) m_topQueries.pop_back(); // Add new query and save it to drive. @@ -117,7 +116,7 @@ void QuerySaver::Deserialize(string const & data) ReaderSource reader(rawReader); TLength queriesCount = ReadPrimitiveFromSource(reader); - queriesCount = min(queriesCount, kMaxSuggestCount); + queriesCount = min(queriesCount, kMaxSuggestionsCount); for (TLength i = 0; i < queriesCount; ++i) {