auto payload = makeProxyHeader(tcp, source, destination, values);
auto previousSize = buffer.size();
+ if (payload.size() > (std::numeric_limits<size_t>::max() - previousSize)) {
+ return false;
+ }
+
buffer.resize(previousSize + payload.size());
std::copy_backward(buffer.begin(), buffer.begin() + previousSize, buffer.end());
std::copy(payload.begin(), payload.end(), buffer.begin());
size_t valuesSize = 0;
for (const auto& value : values) {
- valuesSize += sizeof(uint8_t) + sizeof(uint8_t) * 2 + value.content.size();
- if (valuesSize > std::numeric_limits<uint16_t>::max()) {
+ if (value.content.size() > std::numeric_limits<uint16_t>::max()) {
throw std::runtime_error("The size of proxy protocol values is limited to " + std::to_string(std::numeric_limits<uint16_t>::max()) + ", trying to add a value of size " + std::to_string(value.content.size()));
}
+ valuesSize += sizeof(uint8_t) + sizeof(uint8_t) * 2 + value.content.size();
+ if (valuesSize > std::numeric_limits<uint16_t>::max()) {
+ throw std::runtime_error("The total size of proxy protocol values is limited to " + std::to_string(std::numeric_limits<uint16_t>::max()));
+ }
}
size_t total = (addrSize * 2) + sizeof(sourcePort) + sizeof(destinationPort) + valuesSize;