From a64964a2219bdfe5906b9de3838d0b97e1b1edb5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 24 Feb 2021 21:16:48 +0300 Subject: use SafeInt to make integers a bit more safe --- include/winapi/buffer.hpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/winapi/buffer.hpp b/include/winapi/buffer.hpp index 7c8f928..11d029f 100644 --- a/include/winapi/buffer.hpp +++ b/include/winapi/buffer.hpp @@ -5,6 +5,8 @@ #pragma once +#include + #include #include #include @@ -35,7 +37,11 @@ public: template void set(const std::basic_string& src) { - set(src.c_str(), src.length() * sizeof(std::basic_string::char_type)); + std::size_t new_size = 0; + if (!SafeMultiply(src.length(), sizeof(std::basic_string::char_type), new_size)) { + throw std::runtime_error{"Destination buffer size is too large"}; + } + set(src.c_str(), new_size); } void set(const void* src, std::size_t nb) { @@ -64,7 +70,13 @@ public: void add(const Buffer& src) { const auto nb = size(); - resize(size() + src.size()); + { + std::size_t new_size = 0; + if (!SafeAdd(size(), src.size(), new_size)) { + throw std::runtime_error{"Destination buffer size is too large"}; + } + resize(new_size); + } std::memcpy(data() + nb, src.data(), src.size()); } }; -- cgit v1.2.3