diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2021-02-24 21:16:48 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-02-24 21:48:54 +0300 |
commit | a64964a2219bdfe5906b9de3838d0b97e1b1edb5 (patch) | |
tree | c2ddeeeecfda89eb6883dcef494a141c65a095f3 /include/winapi | |
parent | add README.md (diff) | |
download | winapi-common-a64964a2219bdfe5906b9de3838d0b97e1b1edb5.tar.gz winapi-common-a64964a2219bdfe5906b9de3838d0b97e1b1edb5.zip |
use SafeInt to make integers a bit more safe
Diffstat (limited to '')
-rw-r--r-- | include/winapi/buffer.hpp | 16 |
1 files changed, 14 insertions, 2 deletions
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 <SafeInt.hpp> + #include <cstddef> #include <cstring> #include <initializer_list> @@ -35,7 +37,11 @@ public: template <typename CharT> void set(const std::basic_string<CharT>& src) { - set(src.c_str(), src.length() * sizeof(std::basic_string<CharT>::char_type)); + std::size_t new_size = 0; + if (!SafeMultiply(src.length(), sizeof(std::basic_string<CharT>::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()); } }; |