diff options
author | Francis Dupont <fdupont@isc.org> | 2024-03-05 18:44:51 +0100 |
---|---|---|
committer | Francis Dupont <fdupont@isc.org> | 2024-03-20 00:18:24 +0100 |
commit | baa1aa46e56a9eb64c8dbf7b8115bc74b0b9d9ca (patch) | |
tree | f8493dc49ea33769f038b0a3c652ee540f31ff2b /src/lib | |
parent | [#3211] Checkpoint: rewrote InputBuffer (diff) | |
download | kea-baa1aa46e56a9eb64c8dbf7b8115bc74b0b9d9ca.tar.xz kea-baa1aa46e56a9eb64c8dbf7b8115bc74b0b9d9ca.zip |
[#3211] Almost done
Diffstat (limited to 'src/lib')
31 files changed, 164 insertions, 270 deletions
diff --git a/src/lib/asiodns/tests/io_fetch_unittest.cc b/src/lib/asiodns/tests/io_fetch_unittest.cc index 1001807ccd..47bac7ce93 100644 --- a/src/lib/asiodns/tests/io_fetch_unittest.cc +++ b/src/lib/asiodns/tests/io_fetch_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -200,7 +200,7 @@ public: // identical, then check that the data is identical as well. EXPECT_EQ(msgbuf_->getLength(), length); EXPECT_TRUE(equal(receive_buffer_, (receive_buffer_ + length - 1), - static_cast<const uint8_t*>(msgbuf_->getData()))); + msgbuf_->getData())); // Return a message back to the IOFetch object. if (!bad_qid) { @@ -313,7 +313,7 @@ public: receive_buffer_[2] = receive_buffer_[3] = 0; EXPECT_TRUE(equal((receive_buffer_ + 2), (receive_buffer_ + cumulative_ - 2), - static_cast<const uint8_t*>(msgbuf_->getData()))); + msgbuf_->getData())); // ... and return a message back. This has to be preceded by a two-byte // count field. @@ -473,7 +473,7 @@ public: result_buff_->writeUint8At(return_data_[0], 0); result_buff_->writeUint8At(return_data_[1], 1); } - const uint8_t* start = static_cast<const uint8_t*>(result_buff_->getData()); + const uint8_t* start = result_buff_->getData(); EXPECT_TRUE(equal(return_data_.begin(), return_data_.end(), start)); } } diff --git a/src/lib/asiolink/tests/tcp_socket_unittest.cc b/src/lib/asiolink/tests/tcp_socket_unittest.cc index 1f2ff42b4f..9486b582d8 100644 --- a/src/lib/asiolink/tests/tcp_socket_unittest.cc +++ b/src/lib/asiolink/tests/tcp_socket_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -283,7 +283,7 @@ TEST(TCPSocket, processReceivedData) { EXPECT_EQ(PACKET_SIZE, expected); EXPECT_EQ(2, outbuff->getLength()); - const uint8_t* dataptr = static_cast<const uint8_t*>(outbuff->getData()); + const uint8_t* dataptr = outbuff->getData(); EXPECT_TRUE(equal(inbuff + 2, inbuff + cumulative, dataptr)); // And add the remaining data. Remember that "inbuff" is "PACKET_SIZE + 2" @@ -296,7 +296,7 @@ TEST(TCPSocket, processReceivedData) { EXPECT_EQ(0, offset); EXPECT_EQ(PACKET_SIZE, expected); EXPECT_EQ(PACKET_SIZE, outbuff->getLength()); - dataptr = static_cast<const uint8_t*>(outbuff->getData()); + dataptr = outbuff->getData(); EXPECT_TRUE(equal(inbuff + 2, inbuff + cumulative, dataptr)); } @@ -505,7 +505,7 @@ TEST(TCPSocket, sequenceTest) { EXPECT_EQ(sizeof(INBOUND_DATA) + 2, server_cb.length()); // ... and that what was sent is what was received. - const uint8_t* received = static_cast<const uint8_t*>(client_buffer->getData()); + const uint8_t* received = client_buffer->getData(); EXPECT_TRUE(equal(INBOUND_DATA, (INBOUND_DATA + (sizeof(INBOUND_DATA) - 1)), received)); diff --git a/src/lib/asiolink/tests/tls_socket_unittest.cc b/src/lib/asiolink/tests/tls_socket_unittest.cc index 57773f69cb..e5c21d8c14 100644 --- a/src/lib/asiolink/tests/tls_socket_unittest.cc +++ b/src/lib/asiolink/tests/tls_socket_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -293,7 +293,7 @@ TEST(TLSSocket, processReceivedData) { EXPECT_EQ(PACKET_SIZE, expected); EXPECT_EQ(2, outbuff->getLength()); - const uint8_t* dataptr = static_cast<const uint8_t*>(outbuff->getData()); + const uint8_t* dataptr = outbuff->getData(); EXPECT_TRUE(equal(inbuff + 2, inbuff + cumulative, dataptr)); // And add the remaining data. Remember that "inbuff" is "PACKET_SIZE + 2" @@ -306,7 +306,7 @@ TEST(TLSSocket, processReceivedData) { EXPECT_EQ(0, offset); EXPECT_EQ(PACKET_SIZE, expected); EXPECT_EQ(PACKET_SIZE, outbuff->getLength()); - dataptr = static_cast<const uint8_t*>(outbuff->getData()); + dataptr = outbuff->getData(); EXPECT_TRUE(equal(inbuff + 2, inbuff + cumulative, dataptr)); } @@ -550,7 +550,7 @@ TEST(TLSSocket, sequenceTest) { EXPECT_EQ(sizeof(INBOUND_DATA) + 2, server_cb.length()); // ... and that what was sent is what was received. - const uint8_t* received = static_cast<const uint8_t*>(client_buffer->getData()); + const uint8_t* received = client_buffer->getData(); EXPECT_TRUE(equal(INBOUND_DATA, (INBOUND_DATA + (sizeof(INBOUND_DATA) - 1)), received)); diff --git a/src/lib/asiolink/tests/udp_socket_unittest.cc b/src/lib/asiolink/tests/udp_socket_unittest.cc index 5ef43c167d..0668414ced 100644 --- a/src/lib/asiolink/tests/udp_socket_unittest.cc +++ b/src/lib/asiolink/tests/udp_socket_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -194,7 +194,7 @@ TEST(UDPSocket, processReceivedData) { EXPECT_EQ(0, offset); EXPECT_EQ(sizeof(inbuff), expected); - const uint8_t* dataptr = static_cast<const uint8_t*>(outbuff->getData()); + const uint8_t* dataptr = outbuff->getData(); EXPECT_TRUE(equal(inbuff, inbuff + sizeof(inbuff) - 1, dataptr)); } } @@ -310,7 +310,7 @@ TEST(UDPSocket, SequenceTest) { EXPECT_EQ(client_cb.getLength(), client_buffer->getLength()); // ...and check that the data was copied to the output client buffer. - const char* client_char_data = static_cast<const char*>(client_buffer->getData()); + const uint8_t* client_char_data = client_buffer->getData(); EXPECT_TRUE(equal(&data[0], &data[client_cb.getLength() - 1], client_char_data)); // Close client and server. diff --git a/src/lib/cryptolink/tests/hash_unittests.cc b/src/lib/cryptolink/tests/hash_unittests.cc index cd747b4f4d..a8c6ded6ab 100644 --- a/src/lib/cryptolink/tests/hash_unittests.cc +++ b/src/lib/cryptolink/tests/hash_unittests.cc @@ -50,8 +50,7 @@ namespace { size_t len) { ASSERT_EQ(len, buf.getLength()); - checkData(static_cast<const uint8_t*>(buf.getData()), expected, - len); + checkData(buf.getData(), expected, len); } /// @brief Hash with the convenience functions diff --git a/src/lib/cryptolink/tests/hmac_unittests.cc b/src/lib/cryptolink/tests/hmac_unittests.cc index ab133e51d4..d5c6a83ab2 100644 --- a/src/lib/cryptolink/tests/hmac_unittests.cc +++ b/src/lib/cryptolink/tests/hmac_unittests.cc @@ -60,8 +60,7 @@ namespace { size_t len) { ASSERT_EQ(len, buf.getLength()); - checkData(static_cast<const uint8_t*>(buf.getData()), expected, - len); + checkData(buf.getData(), expected, len); } /// @brief Sign and verify with the convenience functions diff --git a/src/lib/dhcp/libdhcp++.cc b/src/lib/dhcp/libdhcp++.cc index 8e5b794be4..ef39c58c5d 100644 --- a/src/lib/dhcp/libdhcp++.cc +++ b/src/lib/dhcp/libdhcp++.cc @@ -1193,7 +1193,7 @@ LibDHCP::splitOptions4(OptionCollection& options, } // Create new option with data starting from offset and // containing truncated length. - const uint8_t* data = static_cast<const uint8_t*>(buf.getData()); + const uint8_t* data = buf.getData(); data += header_len; OptionPtr new_option(new Option(candidate->getUniverse(), candidate->getType(), diff --git a/src/lib/dhcp/opaque_data_tuple.h b/src/lib/dhcp/opaque_data_tuple.h index 66a53dcc17..a8f5528eea 100644 --- a/src/lib/dhcp/opaque_data_tuple.h +++ b/src/lib/dhcp/opaque_data_tuple.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -104,6 +104,9 @@ public: void append(const char* data, const size_t len) { data_.insert(data_.end(), data, data + len); } + void append(const uint8_t* data, const size_t len) { + data_.insert(data_.end(), data, data + len); + } void append(InputIterator data, const size_t len) { data_.insert(data_.end(), data, data + len); } diff --git a/src/lib/dhcp/option4_dnr.cc b/src/lib/dhcp/option4_dnr.cc index 03bcdcd1f1..7f292913ef 100644 --- a/src/lib/dhcp/option4_dnr.cc +++ b/src/lib/dhcp/option4_dnr.cc @@ -808,7 +808,7 @@ DnrInstance::parseAlpnSvcParam(const std::string& svc_param_val) { alpn_id_tuple.pack(out_buf); } - svc_param_val_tuple.append(static_cast<const char*>(out_buf.getData()), out_buf.getLength()); + svc_param_val_tuple.append(out_buf.getData(), out_buf.getLength()); svc_params_map_.insert(std::make_pair(1, svc_param_val_tuple)); out_buf.clear(); } @@ -830,7 +830,7 @@ DnrInstance::parsePortSvcParam(const std::string& svc_param_val) { } out_buf.writeUint16(port); - svc_param_val_tuple.append(static_cast<const char*>(out_buf.getData()), out_buf.getLength()); + svc_param_val_tuple.append(out_buf.getData(), out_buf.getLength()); out_buf.clear(); svc_params_map_.insert(std::make_pair(3, svc_param_val_tuple)); } diff --git a/src/lib/dhcp/pkt4o6.cc b/src/lib/dhcp/pkt4o6.cc index 8374b3463a..3c526aaf12 100644 --- a/src/lib/dhcp/pkt4o6.cc +++ b/src/lib/dhcp/pkt4o6.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -37,7 +37,7 @@ void Pkt4o6::pack() { // Convert wire-format Pkt4 data in the form of OptionBuffer. Pkt4::pack(); OutputBuffer& buf = getBuffer(); - const uint8_t* ptr = static_cast<const uint8_t*>(buf.getData()); + const uint8_t* ptr = buf.getData(); OptionBuffer msg(ptr, ptr + buf.getLength()); // Build the DHCPv4 Message option for the DHCPv6 message, and pack the diff --git a/src/lib/dhcp/pkt_filter_inet.cc b/src/lib/dhcp/pkt_filter_inet.cc index 5972a4de58..3de58912ff 100644 --- a/src/lib/dhcp/pkt_filter_inet.cc +++ b/src/lib/dhcp/pkt_filter_inet.cc @@ -269,7 +269,7 @@ PktFilterInet::send(const Iface&, uint16_t sockfd, const Pkt4Ptr& pkt) { memset(&v, 0, sizeof(v)); // iov_base field is of void * type. We use it for packet // transmission, so this buffer will not be modified. - v.iov_base = const_cast<void *>(pkt->getBuffer().getData()); + v.iov_base = const_cast<void *>(pkt->getBuffer().getDataAsVP()); v.iov_len = pkt->getBuffer().getLength(); m.msg_iov = &v; m.msg_iovlen = 1; diff --git a/src/lib/dhcp/pkt_filter_inet6.cc b/src/lib/dhcp/pkt_filter_inet6.cc index 443e19101d..8328b65320 100644 --- a/src/lib/dhcp/pkt_filter_inet6.cc +++ b/src/lib/dhcp/pkt_filter_inet6.cc @@ -322,7 +322,7 @@ PktFilterInet6::send(const Iface&, uint16_t sockfd, const Pkt6Ptr& pkt) { // to assign const void* to void*. struct iovec v; memset(&v, 0, sizeof(v)); - v.iov_base = const_cast<void *>(pkt->getBuffer().getData()); + v.iov_base = const_cast<void *>(pkt->getBuffer().getDataAsVP()); v.iov_len = pkt->getBuffer().getLength(); m.msg_iov = &v; m.msg_iovlen = 1; diff --git a/src/lib/dhcp/protocol_util.cc b/src/lib/dhcp/protocol_util.cc index 9728cf4ed4..da701045c8 100644 --- a/src/lib/dhcp/protocol_util.cc +++ b/src/lib/dhcp/protocol_util.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -186,11 +186,11 @@ writeIpUdpHeader(const Pkt4Ptr& pkt, util::OutputBuffer& out_buf) { // 4 bytes of source address and 4 bytes of destination address. // The IPPROTO_UDP and udp_len are also added up to the checksum. uint16_t pseudo_hdr_checksum = - calcChecksum(static_cast<const uint8_t*>(out_buf.getData()) + out_buf.getLength() - 8, + calcChecksum(out_buf.getData() + out_buf.getLength() - 8, 8, IPPROTO_UDP + udp_len); // Calculate IP header checksum. - uint16_t ip_checksum = ~calcChecksum(static_cast<const uint8_t*>(out_buf.getData()) + uint16_t ip_checksum = ~calcChecksum(out_buf.getData() + out_buf.getLength() - 20, 20); // Write checksum in the IP header. The offset of the checksum is 10 bytes // back from the tail of the current buffer. @@ -206,8 +206,8 @@ writeIpUdpHeader(const Pkt4Ptr& pkt, util::OutputBuffer& out_buf) { // tail of the current buffer. These 6 bytes contain source and destination port // as well as the length of the header. uint16_t udp_checksum = - ~calcChecksum(static_cast<const uint8_t*>(out_buf.getData()) + out_buf.getLength() - 6, 6, - calcChecksum(static_cast<const uint8_t*>(pkt->getBuffer().getData()), + ~calcChecksum(out_buf.getData() + out_buf.getLength() - 6, 6, + calcChecksum(pkt->getBuffer().getData(), pkt->getBuffer().getLength(), pseudo_hdr_checksum)); // Write UDP checksum. diff --git a/src/lib/dhcp/tests/opaque_data_tuple_unittest.cc b/src/lib/dhcp/tests/opaque_data_tuple_unittest.cc index 974a0bf27b..4b97aae7a8 100644 --- a/src/lib/dhcp/tests/opaque_data_tuple_unittest.cc +++ b/src/lib/dhcp/tests/opaque_data_tuple_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2021 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -275,7 +275,7 @@ TEST(OpaqueDataTuple, pack1Byte) { OutputBuffer out_buf(10); ASSERT_NO_THROW(tuple.pack(out_buf)); ASSERT_EQ(1, out_buf.getLength()); - const uint8_t* zero_len = static_cast<const uint8_t*>(out_buf.getData()); + const uint8_t* zero_len = out_buf.getData(); ASSERT_EQ(0, *zero_len); // Reset the output buffer for another test. out_buf.clear(); @@ -292,8 +292,7 @@ TEST(OpaqueDataTuple, pack1Byte) { ASSERT_EQ(101, out_buf.getLength()); // Get the rendered data into the vector for convenience. std::vector<uint8_t> - render_data(static_cast<const uint8_t*>(out_buf.getData()), - static_cast<const uint8_t*>(out_buf.getData()) + 101); + render_data(out_buf.getData(), out_buf.getData() + 101); // The first byte is a length byte. It should hold the length of 100. EXPECT_EQ(100, render_data[0]); // Verify that the rendered data is correct. @@ -314,8 +313,7 @@ TEST(OpaqueDataTuple, pack1Byte) { // opaque data length, the remaining bytes hold the actual data. ASSERT_EQ(256, out_buf.getLength()); // Check that the data is correct. - render_data.assign(static_cast<const uint8_t*>(out_buf.getData()), - static_cast<const uint8_t*>(out_buf.getData()) + 256); + render_data.assign(out_buf.getData(), out_buf.getData() + 256); EXPECT_EQ(255, render_data[0]); EXPECT_TRUE(std::equal(render_data.begin() + 1, render_data.end(), data.begin())); @@ -339,8 +337,10 @@ TEST(OpaqueDataTuple, pack2Bytes) { OutputBuffer out_buf(10); ASSERT_NO_THROW(tuple.pack(out_buf)); ASSERT_EQ(2, out_buf.getLength()); - const uint16_t* zero_len = static_cast<const uint16_t*>(out_buf.getData()); - ASSERT_EQ(0, *zero_len); + const uint8_t* out_data = out_buf.getData(); + ASSERT_LE(sizeof(uint16_t), out_buf.getLength()); + ASSERT_EQ(0, out_data[0]); + ASSERT_EQ(0, out_data[1]); // Reset the output buffer for another test. out_buf.clear(); // Set the data for tuple. @@ -356,8 +356,7 @@ TEST(OpaqueDataTuple, pack2Bytes) { ASSERT_EQ(514, out_buf.getLength()); // Get the rendered data into the vector for convenience. std::vector<uint8_t> - render_data(static_cast<const uint8_t*>(out_buf.getData()), - static_cast<const uint8_t*>(out_buf.getData()) + 514); + render_data(out_buf.getData(), out_buf.getData() + 514); // The first two bytes hold the length of 512. uint16_t len = (render_data[0] << 8) + render_data[1]; EXPECT_EQ(512, len); diff --git a/src/lib/dhcp/tests/option6_iaprefix_unittest.cc b/src/lib/dhcp/tests/option6_iaprefix_unittest.cc index 2bd8be39b3..bd2eaf83d5 100644 --- a/src/lib/dhcp/tests/option6_iaprefix_unittest.cc +++ b/src/lib/dhcp/tests/option6_iaprefix_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -107,7 +107,7 @@ public: /// @param expected_type expected option type void checkOutputBuffer(uint16_t expected_type) { // Check if pack worked properly: - const uint8_t* out = static_cast<const uint8_t*>(out_buf_.getData()); + const uint8_t* out = out_buf_.getData(); // - if option type is correct EXPECT_EQ(expected_type, out[0]*256 + out[1]); diff --git a/src/lib/dhcp/tests/option6_pdexclude_unittest.cc b/src/lib/dhcp/tests/option6_pdexclude_unittest.cc index 6b3eebca9e..463fa47c7e 100644 --- a/src/lib/dhcp/tests/option6_pdexclude_unittest.cc +++ b/src/lib/dhcp/tests/option6_pdexclude_unittest.cc @@ -76,7 +76,7 @@ TEST(Option6PDExcludeTest, pack) { ASSERT_EQ(expected_vec.size(), buf.getLength()); // Check that the generated wire format is correct. - const uint8_t* data = static_cast<const uint8_t*>(buf.getData()); + const uint8_t* data = buf.getData(); std::vector<uint8_t> vec(data, data + buf.getLength()); ASSERT_TRUE(std::equal(vec.begin(), vec.end(), expected_vec.begin())); } diff --git a/src/lib/dhcp/tests/option_opaque_data_tuples_unittest.cc b/src/lib/dhcp/tests/option_opaque_data_tuples_unittest.cc index 01dcc1877b..e19c8e92a5 100644 --- a/src/lib/dhcp/tests/option_opaque_data_tuples_unittest.cc +++ b/src/lib/dhcp/tests/option_opaque_data_tuples_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -270,9 +270,7 @@ TEST(OptionOpaqueDataTuples, pack4) { 0x66, 0x6F, 0x6F // foo }; // Compare the buffer with reference data. - EXPECT_EQ(0, memcmp(static_cast<const void*>(ref), - static_cast<const void*>(buf.getData()), - buf.getLength())); + EXPECT_EQ(0, memcmp(ref, buf.getData(), buf.getLength())); } // Check that the DHCPv4 option is rendered to the buffer in wire format, @@ -304,9 +302,7 @@ TEST(OptionOpaqueDataTuples, pack4_with_ltf) { 0x66, 0x6F, 0x6F // foo }; // Compare the buffer with reference data. - EXPECT_EQ(0, memcmp(static_cast<const void*>(ref), - static_cast<const void*>(buf.getData()), - buf.getLength())); + EXPECT_EQ(0, memcmp(ref, buf.getData(), buf.getLength())); } // Check that the DHCPv6 option is rendered to the buffer in wire format. @@ -336,9 +332,7 @@ TEST(OptionOpaqueDataTuples, pack6) { 0x66, 0x6F, 0x6F // foo }; // Compare the buffer with reference data. - EXPECT_EQ(0, memcmp(static_cast<const void*>(ref), - static_cast<const void*>(buf.getData()), - buf.getLength())); + EXPECT_EQ(0, memcmp(ref, buf.getData(), buf.getLength())); } // This function checks that the DHCPv4 option with two opaque data tuples diff --git a/src/lib/dhcp/tests/option_unittest.cc b/src/lib/dhcp/tests/option_unittest.cc index b2c36d32de..1415d271ad 100644 --- a/src/lib/dhcp/tests/option_unittest.cc +++ b/src/lib/dhcp/tests/option_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2023 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -244,7 +244,7 @@ TEST_F(OptionTest, v6_data1) { opt->pack(outBuf_); EXPECT_EQ(11, outBuf_.getLength()); - const uint8_t* out = static_cast<const uint8_t*>(outBuf_.getData()); + const uint8_t* out = outBuf_.getData(); EXPECT_EQ(out[0], 333 / 256); // Type EXPECT_EQ(out[1], 333 % 256); @@ -280,7 +280,7 @@ TEST_F(OptionTest, v6_data2) { // Check if pack worked properly: // If option type is correct - const uint8_t* out = static_cast<const uint8_t*>(outBuf_.getData()); + const uint8_t* out = outBuf_.getData(); EXPECT_EQ(D6O_CLIENTID, out[0] * 256 + out[1]); @@ -521,7 +521,7 @@ TEST_F(OptionTest, setData) { opt1->setData(buf_.begin(), buf_.end()); opt1->pack(outBuf_); ASSERT_EQ(outBuf_.getLength() - opt1->getHeaderLen(), buf_.size()); - const uint8_t* test_data = static_cast<const uint8_t*>(outBuf_.getData()); + const uint8_t* test_data = outBuf_.getData(); EXPECT_TRUE(0 == memcmp(&buf_[0], test_data + opt1->getHeaderLen(), buf_.size())); @@ -534,7 +534,7 @@ TEST_F(OptionTest, setData) { opt2->setData(buf_.begin(), buf_.end()); opt2->pack(outBuf_); ASSERT_EQ(outBuf_.getLength() - opt1->getHeaderLen(), buf_.size()); - test_data = static_cast<const uint8_t*>(outBuf_.getData()); + test_data = outBuf_.getData(); EXPECT_TRUE(0 == memcmp(&buf_[0], test_data + opt1->getHeaderLen(), buf_.size())); } diff --git a/src/lib/dhcp/tests/option_vendor_class_unittest.cc b/src/lib/dhcp/tests/option_vendor_class_unittest.cc index 40a36b8275..39826441ea 100644 --- a/src/lib/dhcp/tests/option_vendor_class_unittest.cc +++ b/src/lib/dhcp/tests/option_vendor_class_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -201,8 +201,7 @@ TEST(OptionVendorClass, pack4) { 0x66, 0x6F, 0x6F // foo }; // Compare the buffer with reference data. - EXPECT_EQ(0, memcmp(static_cast<const void*>(ref), - static_cast<const void*>(buf.getData()), 26)); + EXPECT_EQ(0, memcmp(ref, buf.getData(), 26)); } // Check that the DHCPv6 option is rendered to the buffer in wire format. @@ -233,9 +232,7 @@ TEST(OptionVendorClass, pack6) { 0x66, 0x6F, 0x6F // foo }; // Compare the buffer with reference data. - EXPECT_EQ(0, memcmp(static_cast<const void*>(ref), - static_cast<const void*>(buf.getData()), - buf.getLength())); + EXPECT_EQ(0, memcmp(ref, buf.getData(), buf.getLength())); } // This function checks that the DHCPv4 option with two opaque data tuples diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc index 47ca71162f..a450ac363b 100644 --- a/src/lib/dhcp/tests/pkt4_unittest.cc +++ b/src/lib/dhcp/tests/pkt4_unittest.cc @@ -317,7 +317,7 @@ TEST_F(Pkt4Test, fixedFieldsPack) { // Redundant but MUCH easier for debug in gdb const uint8_t* exp = &expectedFormat[0]; - const uint8_t* got = static_cast<const uint8_t*>(pkt->getBuffer().getData()); + const uint8_t* got = pkt->getBuffer().getData(); EXPECT_EQ(0, memcmp(exp, got, Pkt4::DHCPV4_PKT_HDR_LEN)); } @@ -405,8 +405,7 @@ TEST_F(Pkt4Test, hwAddr) { ); // CHADDR starts at offset 28 in DHCP packet - const uint8_t* ptr = - static_cast<const uint8_t*>(pkt->getBuffer().getData()) + 28; + const uint8_t* ptr = pkt->getBuffer().getData() + 28; EXPECT_EQ(0, memcmp(ptr, expectedChaddr, Pkt4::MAX_CHADDR_LEN)); @@ -481,8 +480,7 @@ TEST_F(Pkt4Test, sname) { ); // SNAME starts at offset 44 in DHCP packet - const uint8_t* ptr = - static_cast<const uint8_t*>(pkt->getBuffer().getData()) + 44; + const uint8_t* ptr = pkt->getBuffer().getData() + 44; EXPECT_EQ(0, memcmp(ptr, sname, Pkt4::MAX_SNAME_LEN)); pkt.reset(); @@ -526,8 +524,7 @@ TEST_F(Pkt4Test, file) { ); // FILE starts at offset 108 in DHCP packet. - const uint8_t* ptr = - static_cast<const uint8_t*>(pkt->getBuffer().getData()) + 108; + const uint8_t* ptr = pkt->getBuffer().getData() + 108; EXPECT_EQ(0, memcmp(ptr, file, Pkt4::MAX_FILE_LEN)); pkt.reset(); @@ -592,7 +589,7 @@ TEST_F(Pkt4Test, options) { buf.getLength()); // That that this extra data actually contain our options - const uint8_t* ptr = static_cast<const uint8_t*>(buf.getData()); + const uint8_t* ptr = buf.getData(); // Rewind to end of fixed part. ptr += Pkt4::DHCPV4_PKT_HDR_LEN + sizeof(DHCP_OPTIONS_COOKIE); @@ -1440,7 +1437,7 @@ TEST_F(Pkt4Test, nullTerminatedOptions) { DHO_ROOT_PATH, 4, 'n', 'o', 'n', 'e', }; - const uint8_t* packed = static_cast<const uint8_t*>(pkt->getBuffer().getData()); + const uint8_t* packed = pkt->getBuffer().getData(); int packed_len = pkt->getBuffer().getLength(); // Packed message options should be 3 bytes smaller than original onwire data. diff --git a/src/lib/dhcp/tests/pkt_filter6_test_utils.cc b/src/lib/dhcp/tests/pkt_filter6_test_utils.cc index 9302b8b0d4..aa5d85c7da 100644 --- a/src/lib/dhcp/tests/pkt_filter6_test_utils.cc +++ b/src/lib/dhcp/tests/pkt_filter6_test_utils.cc @@ -119,7 +119,7 @@ PktFilter6Test::sendMessage() { // The iovec structure holds the packet data. struct iovec v; memset(&v, 0, sizeof(v)); - v.iov_base = const_cast<void *>(test_message_->getBuffer().getData()); + v.iov_base = const_cast<void *>(test_message_->getBuffer().getDataAsVP()); v.iov_len = test_message_->getBuffer().getLength(); // Assign the iovec to msghdr structure. m.msg_iov = &v; diff --git a/src/lib/dhcp_ddns/ncr_udp.cc b/src/lib/dhcp_ddns/ncr_udp.cc index c6ca025db8..80cf4edd85 100644 --- a/src/lib/dhcp_ddns/ncr_udp.cc +++ b/src/lib/dhcp_ddns/ncr_udp.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -310,8 +310,7 @@ NameChangeUDPSender::doSend(NameChangeRequestPtr& ncr) { // Copy the wire-ized request to callback. This way we know after // send completes what we sent (or attempted to send). - send_callback_->putData(static_cast<const uint8_t*>(ncr_buffer.getData()), - ncr_buffer.getLength()); + send_callback_->putData(ncr_buffer.getData(), ncr_buffer.getLength()); // Call the socket's asynchronous send, passing our callback socket_->asyncSend(send_callback_->getData(), send_callback_->getPutLen(), diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index a10c0c5437..a74eca9415 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -5066,7 +5066,7 @@ AllocEngine::updateLease6ExtendedInfo(const Lease6Ptr& lease, LibDHCP::packOptions6(buf, relay.options_); if (buf.getLength() > 0) { - const uint8_t* cp = static_cast<const uint8_t*>(buf.getData()); + const uint8_t* cp = buf.getData(); std::vector<uint8_t> bytes; std::stringstream ss; diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index 9208b9b361..49c7709e1c 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -1859,7 +1859,7 @@ public: // option and store it in the database as a blob. OutputBuffer buf(opt_desc.option_->len()); opt_desc.option_->pack(buf); - const char* buf_ptr = static_cast<const char*>(buf.getData()); + const uint8_t* buf_ptr = buf.getData(); value_.assign(buf_ptr + opt_desc.option_->getHeaderLen(), buf_ptr + buf.getLength()); value_len_ = value_.size(); diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index 7659b508d9..8db86ffa49 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -1240,7 +1240,7 @@ public: // option and store it in the database as a BYTEA. OutputBuffer buf(opt_desc.option_->len()); opt_desc.option_->pack(buf); - const char* buf_ptr = static_cast<const char*>(buf.getData()); + const uint8_t* buf_ptr = buf.getData(); value_.assign(buf_ptr + opt_desc.option_->getHeaderLen(), buf_ptr + buf.getLength()); value_len_ = value_.size(); diff --git a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc index 4a327cde13..69aae2bd14 100644 --- a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -227,7 +227,7 @@ Dhcp4o6IpcBaseTest::createDHCPv4MsgOption(TestIpc::EndpointType src) { // Make a wire representation of the DHCPv4 message. pkt->pack(); OutputBuffer& output_buffer = pkt->getBuffer(); - const uint8_t* data = static_cast<const uint8_t*>(output_buffer.getData()); + const uint8_t* data = output_buffer.getData(); OptionBuffer option_buffer(data, data + output_buffer.getLength()); // Create the DHCPv4 Message option holding the created message. @@ -338,7 +338,7 @@ Dhcp4o6IpcBaseTest::testReceiveError(const Pkt6Ptr& pkt) { buf.clear(); ASSERT_NO_THROW(pkt->pack()); - ASSERT_NE(-1, ::send(ipc_src.getSocketFd(), buf.getData(), + ASSERT_NE(-1, ::send(ipc_src.getSocketFd(), buf.getDataAsVP(), buf.getLength(), 0)); // Call receive with a timeout. The data should appear on the socket diff --git a/src/lib/dns/tests/labelsequence_unittest.cc b/src/lib/dns/tests/labelsequence_unittest.cc index 3fa0cc61a5..ccc5c6e153 100644 --- a/src/lib/dns/tests/labelsequence_unittest.cc +++ b/src/lib/dns/tests/labelsequence_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -816,10 +816,8 @@ TEST_F(LabelSequenceTest, serialize) { // Copy wire data of the name isc::util::OutputBuffer ob(0); n_maxlabel.toWire(ob); - expected_data7.insert(expected_data7.end(), - static_cast<const uint8_t*>(ob.getData()), - static_cast<const uint8_t*>(ob.getData()) + - ob.getLength()); + expected_data7.insert(expected_data7.end(), ob.getData(), + ob.getData() + ob.getLength()); expected.push_back(DataPair(expected_data7.size(), &expected_data7[0])); // For each data set, serialize the labels and compare the data to the diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h index aad629740f..434448a618 100644 --- a/src/lib/util/buffer.h +++ b/src/lib/util/buffer.h @@ -315,7 +315,7 @@ private: /// // pass the buffer to a DNS message object to construct a wire-format /// // DNS message. /// struct sockaddr to; -/// sendto(s, buffer.getData(), buffer.getLength(), 0, &to, sizeof(to)); +/// sendto(s, buffer.getDataAsVP(), buffer.getLength(), 0, &to, sizeof(to)); /// \endcode /// /// where the \c getData() method gives a reference to the internal memory @@ -367,19 +367,11 @@ public: //@{ /// \brief Constructor from the initial size of the buffer. /// - /// \param len The initial length of the buffer in bytes. - OutputBuffer(size_t len) : - buffer_(NULL), - size_(0), - allocated_(len) + /// \param len The initial allocated length of the buffer in bytes. + OutputBuffer(size_t len) : buffer_() { - // We use malloc and free instead of C++ new[] and delete[]. - // This way we can use realloc, which may in fact do it without a copy. - if (allocated_ != 0) { - buffer_ = static_cast<uint8_t*>(malloc(allocated_)); - if (buffer_ == NULL) { - throw std::bad_alloc(); - } + if (len != 0) { + buffer_.reserve(len); } } @@ -391,22 +383,17 @@ public: /// size_ <= allocated_, and that if allocated_ is greater than zero, /// buffer_ points to valid memory. OutputBuffer(const OutputBuffer& other) : - buffer_(NULL), - size_(other.size_), - allocated_(other.allocated_) + buffer_(other.buffer_) { - if (allocated_ != 0) { - buffer_ = static_cast<uint8_t*>(malloc(allocated_)); - if (buffer_ == NULL) { - throw std::bad_alloc(); - } - static_cast<void>(std::memmove(buffer_, other.buffer_, other.size_)); + size_t len = other.buffer_.capacity(); + if (len != 0) { + buffer_.reserve(len); } } /// \brief Destructor ~OutputBuffer() { - free(buffer_); + buffer_.clear(); } //@} @@ -420,35 +407,11 @@ public: OutputBuffer& operator =(const OutputBuffer& other) { if (this != &other) { // Not self-assignment. - if (other.allocated_ != 0) { - - // There is something in the source object, so allocate memory - // and copy it. The pointer to the allocated memory is placed - // in a temporary variable so that if the allocation fails and - // an exception is thrown, the destination object ("this") is - // unchanged. - uint8_t* newbuff = static_cast<uint8_t*>(malloc(other.allocated_)); - if (newbuff == NULL) { - throw std::bad_alloc(); - } - - // Memory allocated, update the source object and copy data - // across. - free(buffer_); - buffer_ = newbuff; - static_cast<void>(std::memmove(buffer_, other.buffer_, other.size_)); - - } else { - - // Nothing allocated in the source object, so zero the buffer - // in the destination. - free(buffer_); - buffer_ = NULL; + buffer_ = other.buffer_; + size_t len = other.buffer_.capacity(); + if (len != 0) { + buffer_.reserve(len); } - - // Update the other member variables. - size_ = other.size_; - allocated_ = other.allocated_; } return (*this); } @@ -458,7 +421,10 @@ public: /// //@{ /// \brief Return the current capacity of the buffer. - size_t getCapacity() const { return (allocated_); } + size_t getCapacity() const { + return (buffer_.capacity()); + } + /// \brief Return a pointer to the head of the data stored in the buffer. /// /// The caller can assume that the subsequent \c getLength() bytes are @@ -466,9 +432,24 @@ public: /// /// Note: The pointer returned by this method may be invalidated after a /// subsequent write operation. - const void* getData() const { return (buffer_); } + const uint8_t* getData() const { + if (!buffer_.empty()) { + return (&buffer_[0]); + } else { + return (0); + } + } + + /// \brief Return data as a pointer to void. + const void* getDataAsVP() const { + return (static_cast<const void*>(getData())); + } + /// \brief Return the length of data written in the buffer. - size_t getLength() const { return (size_); } + size_t getLength() const { + return (buffer_.size()); + } + /// \brief Return the value of the buffer at the specified position. /// /// \c pos must specify the valid position of the buffer; otherwise an @@ -476,12 +457,17 @@ public: /// /// \param pos The position in the buffer to be returned. uint8_t operator[](size_t pos) const { - if (pos >= size_) { + if (pos >= buffer_.size()) { isc_throw(InvalidBufferPosition, - "[]: pos (" << pos << ") >= size (" << size_ << ")"); + "[]: pos (" << pos << ") >= size (" << buffer_.size() << ")"); } return (buffer_[pos]); } + + /// \brief Return the buffer. + const std::vector<uint8_t>& getVector() const { + return (buffer_); + } //@} /// @@ -495,8 +481,7 @@ public: /// that is to be filled in later, e.g, by \ref writeUint16At(). /// \param len The length of the gap to be inserted in bytes. void skip(size_t len) { - ensureAllocated(size_ + len); - size_ += len; + buffer_.resize(buffer_.size() + len); } /// \brief Trim the specified length of data from the end of the buffer. @@ -507,33 +492,22 @@ public: /// /// \param len The length of data that should be trimmed. void trim(size_t len) { - if (len > size_) { + if (len > buffer_.size()) { isc_throw(OutOfRange, "trimming too large from output buffer"); } - size_ -= len; + buffer_.resize(buffer_.size() - len); } - /// \brief Clear buffer content. - /// - /// This method can be used to re-initialize and reuse the buffer without - /// constructing a new one. Note it must keep current content. - void clear() { size_ = 0; } - /// \brief Wipe buffer content. - /// - /// This method is the destructive alternative to clear(). - void wipe() { - if (buffer_ != NULL) { - static_cast<void>(std::memset(buffer_, 0, allocated_)); - } - size_ = 0; + /// \brief Clear buffer content. + void clear() { + buffer_.clear(); } /// \brief Write an unsigned 8-bit integer into the buffer. /// /// \param data The 8-bit integer to be written into the buffer. void writeUint8(uint8_t data) { - ensureAllocated(size_ + 1); - buffer_[size_ ++] = data; + buffer_.push_back(data); } /// \brief Write an unsigned 8-bit integer into the buffer. @@ -545,7 +519,7 @@ public: /// \param data The 8-bit integer to be written into the buffer. /// \param pos The position in the buffer to write the data. void writeUint8At(uint8_t data, size_t pos) { - if (pos + sizeof(data) > size_) { + if (pos + sizeof(data) > buffer_.size()) { isc_throw(InvalidBufferPosition, "write at invalid position"); } buffer_[pos] = data; @@ -556,9 +530,8 @@ public: /// /// \param data The 16-bit integer to be written into the buffer. void writeUint16(uint16_t data) { - ensureAllocated(size_ + sizeof(data)); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0xff00U) >> 8); - buffer_[size_ ++] = static_cast<uint8_t>(data & 0x00ffU); + buffer_.push_back(static_cast<uint8_t>((data & 0xff00U) >> 8)); + buffer_.push_back(static_cast<uint8_t>(data & 0x00ffU)); } /// \brief Write an unsigned 16-bit integer in host byte order at the @@ -573,7 +546,7 @@ public: /// \param data The 16-bit integer to be written into the buffer. /// \param pos The beginning position in the buffer to write the data. void writeUint16At(uint16_t data, size_t pos) { - if (pos + sizeof(data) > size_) { + if (pos + sizeof(data) > buffer_.size()) { isc_throw(InvalidBufferPosition, "write at invalid position"); } @@ -586,11 +559,10 @@ public: /// /// \param data The 32-bit integer to be written into the buffer. void writeUint32(uint32_t data) { - ensureAllocated(size_ + sizeof(data)); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0xff000000) >> 24); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x00ff0000) >> 16); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x0000ff00) >> 8); - buffer_[size_ ++] = static_cast<uint8_t>(data & 0x000000ff); + buffer_.push_back(static_cast<uint8_t>((data & 0xff000000) >> 24)); + buffer_.push_back(static_cast<uint8_t>((data & 0x00ff0000) >> 16)); + buffer_.push_back(static_cast<uint8_t>((data & 0x0000ff00) >> 8)); + buffer_.push_back(static_cast<uint8_t>(data & 0x000000ff)); } /// \brief Write an unsigned 64-bit integer in host byte order @@ -598,15 +570,14 @@ public: /// /// \param data The 64-bit integer to be written into the buffer. void writeUint64(uint64_t data) { - ensureAllocated(size_ + sizeof(data)); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0xff00000000000000) >> 56); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x00ff000000000000) >> 48); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x0000ff0000000000) >> 40); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x000000ff00000000) >> 32); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x00000000ff000000) >> 24); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x0000000000ff0000) >> 16); - buffer_[size_ ++] = static_cast<uint8_t>((data & 0x000000000000ff00) >> 8); - buffer_[size_ ++] = static_cast<uint8_t>(data & 0x00000000000000ff); + buffer_.push_back(static_cast<uint8_t>((data & 0xff00000000000000) >> 56)); + buffer_.push_back(static_cast<uint8_t>((data & 0x00ff000000000000) >> 48)); + buffer_.push_back(static_cast<uint8_t>((data & 0x0000ff0000000000) >> 40)); + buffer_.push_back(static_cast<uint8_t>((data & 0x000000ff00000000) >> 32)); + buffer_.push_back(static_cast<uint8_t>((data & 0x00000000ff000000) >> 24)); + buffer_.push_back(static_cast<uint8_t>((data & 0x0000000000ff0000) >> 16)); + buffer_.push_back(static_cast<uint8_t>((data & 0x000000000000ff00) >> 8)); + buffer_.push_back(static_cast<uint8_t>(data & 0x00000000000000ff)); } /// \brief Copy an arbitrary length of data into the buffer. @@ -620,45 +591,14 @@ public: return; } - ensureAllocated(size_ + len); - static_cast<void>(std::memmove(buffer_ + size_, data, len)); - size_ += len; + const uint8_t* ptr = static_cast<const uint8_t*>(data); + buffer_.insert(buffer_.end(), ptr, ptr + len); } //@} private: /// The actual data - uint8_t* buffer_; - /// How many bytes are used - size_t size_; - /// How many bytes do we have preallocated (eg. the capacity) - size_t allocated_; - - /// \brief Ensure buffer is appropriate size - /// - /// Checks that the buffer equal to or larger than the size given as - /// argument and extends it to at least that size if not. - /// - /// \param needed_size The number of bytes required in the buffer - void ensureAllocated(size_t needed_size) { - if (allocated_ < needed_size) { - // Guess some bigger size - size_t new_size = (allocated_ == 0) ? 1024 : allocated_; - while (new_size < needed_size) { - new_size *= 2; - } - // Allocate bigger space. Note that buffer_ may be NULL, - // in which case realloc acts as malloc. - uint8_t* new_buffer_(static_cast<uint8_t*>(realloc(buffer_, - new_size))); - if (new_buffer_ == NULL) { - // If it fails, the original block is left intact by it - throw std::bad_alloc(); - } - buffer_ = new_buffer_; - allocated_ = new_size; - } - } + std::vector<uint8_t> buffer_; }; /// \brief Pointer-like types pointing to \c InputBuffer or \c OutputBuffer diff --git a/src/lib/util/io/socketsession.cc b/src/lib/util/io/socketsession.cc index 88f2b1e36d..59e2983996 100644 --- a/src/lib/util/io/socketsession.cc +++ b/src/lib/util/io/socketsession.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -241,7 +241,7 @@ SocketSessionForwarder::push(int sock, int family, int type, int protocol, impl_->buf_.writeUint16At(impl_->buf_.getLength() - sizeof(uint16_t), 0); const struct iovec iov[2] = { - { const_cast<void*>(impl_->buf_.getData()), impl_->buf_.getLength() }, + { const_cast<void*>(impl_->buf_.getDataAsVP()), impl_->buf_.getLength() }, { const_cast<void*>(data), data_len } }; const int cc = writev(impl_->fd_, iov, 2); diff --git a/src/lib/util/tests/buffer_unittest.cc b/src/lib/util/tests/buffer_unittest.cc index 0f45569701..46706b179b 100644 --- a/src/lib/util/tests/buffer_unittest.cc +++ b/src/lib/util/tests/buffer_unittest.cc @@ -125,19 +125,19 @@ TEST_F(BufferTest, outputBufferWrite) { obuffer.writeUint8(1); expected_size += sizeof(uint8_t); EXPECT_EQ(expected_size, obuffer.getLength()); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(1, *cp); obuffer.writeUint16(data16); expected_size += sizeof(data16); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(expected_size, obuffer.getLength()); EXPECT_EQ(2, *(cp + 1)); EXPECT_EQ(3, *(cp + 2)); obuffer.writeUint32(data32); expected_size += sizeof(data32); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(expected_size, obuffer.getLength()); EXPECT_EQ(4, *(cp + 3)); EXPECT_EQ(5, *(cp + 4)); @@ -147,31 +147,31 @@ TEST_F(BufferTest, outputBufferWrite) { obuffer.writeData(testdata, sizeof(testdata)); expected_size += sizeof(testdata); EXPECT_EQ(expected_size, obuffer.getLength()); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(0, memcmp(cp + 7, testdata, sizeof(testdata))); } -TEST_F(BufferTest, outputBufferWriteat) { +TEST_F(BufferTest, outputBufferWriteAt) { obuffer.writeUint32(data32); expected_size += sizeof(data32); // overwrite 2nd byte obuffer.writeUint8At(4, 1); EXPECT_EQ(expected_size, obuffer.getLength()); // length shouldn't change - const uint8_t* cp = static_cast<const uint8_t*>(obuffer.getData()); + const uint8_t* cp = obuffer.getData(); EXPECT_EQ(4, *(cp + 1)); // overwrite 2nd and 3rd bytes obuffer.writeUint16At(data16, 1); EXPECT_EQ(expected_size, obuffer.getLength()); // length shouldn't change - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(2, *(cp + 1)); EXPECT_EQ(3, *(cp + 2)); // overwrite 3rd and 4th bytes obuffer.writeUint16At(data16, 2); EXPECT_EQ(expected_size, obuffer.getLength()); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_EQ(2, *(cp + 2)); EXPECT_EQ(3, *(cp + 3)); @@ -210,35 +210,18 @@ TEST_F(BufferTest, outputBufferTrim) { TEST_F(BufferTest, outputBufferReadAt) { obuffer.writeData(testdata, sizeof(testdata)); - for (int i = 0; i < sizeof(testdata); i ++) { + for (size_t i = 0; i < sizeof(testdata); ++i) { EXPECT_EQ(testdata[i], obuffer[i]); } EXPECT_THROW(obuffer[sizeof(testdata)], isc::util::InvalidBufferPosition); } TEST_F(BufferTest, outputBufferClear) { - const uint8_t* cp; - obuffer.writeData(testdata, sizeof(testdata)); - cp = static_cast<const uint8_t*>(obuffer.getData()); + const uint8_t* cp = obuffer.getData(); obuffer.clear(); EXPECT_EQ(0, obuffer.getLength()); - EXPECT_EQ(*cp, 1); -} - -TEST_F(BufferTest, outputBufferWipe) { - const uint8_t* cp; - - obuffer.writeData(testdata, sizeof(testdata)); - cp = static_cast<const uint8_t*>(obuffer.getData()); - obuffer.wipe(); - EXPECT_EQ(0, obuffer.getLength()); - EXPECT_EQ(*cp, 0); -} - -TEST_F(BufferTest, emptyOutputBufferWipe) { - ASSERT_NO_THROW(obuffer.wipe()); - EXPECT_EQ(0, obuffer.getLength()); + EXPECT_FALSE(obuffer.getData()); } TEST_F(BufferTest, outputBufferCopy) { @@ -264,6 +247,7 @@ TEST_F(BufferTest, outputEmptyBufferCopy) { EXPECT_NO_THROW({ OutputBuffer copy(obuffer); ASSERT_EQ(0, copy.getLength()); + EXPECT_FALSE(copy.getData()); }); } @@ -294,7 +278,7 @@ TEST_F(BufferTest, outputEmptyBufferAssign) { copy = obuffer; }); ASSERT_EQ(0, copy.getLength()); - EXPECT_EQ(NULL, copy.getData()); + EXPECT_EQ(0, copy.getData()); } // Check assign to self doesn't break stuff @@ -312,22 +296,6 @@ TEST_F(BufferTest, outputBufferZeroSize) { }); } -TEST_F(BufferTest, inputBufferReadVectorAll) { - std::vector<uint8_t> vec; - - // check that vector can read the whole buffer - ibuffer.readVector(vec, 5); - - ASSERT_EQ(5, vec.size()); - EXPECT_EQ(0, memcmp(&vec[0], testdata, 5)); - - // ibuffer is 5 bytes long. Can't read past it. - EXPECT_THROW( - ibuffer.readVector(vec, 1), - isc::util::InvalidBufferPosition - ); -} - TEST_F(BufferTest, inputBufferReadVectorChunks) { std::vector<uint8_t> vec; @@ -341,7 +309,8 @@ TEST_F(BufferTest, inputBufferReadVectorChunks) { ibuffer.readVector(vec, 2) ); - EXPECT_EQ(0, memcmp(&vec[0], testdata+3, 2)); + ASSERT_EQ(2, vec.size()); + EXPECT_EQ(0, memcmp(&vec[0], &testdata[3], 2)); } // Tests whether uint64 can be written properly. @@ -357,7 +326,7 @@ TEST_F(BufferTest, writeUint64) { obuffer.writeUint64(val1); ASSERT_EQ(sizeof(uint64_t), obuffer.getLength()); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_TRUE(cp); EXPECT_FALSE(memcmp(exp_val1, obuffer.getData(), sizeof(uint64_t))); @@ -365,7 +334,7 @@ TEST_F(BufferTest, writeUint64) { obuffer.writeUint64(val2); ASSERT_EQ(sizeof(uint64_t), obuffer.getLength()); - cp = static_cast<const uint8_t*>(obuffer.getData()); + cp = obuffer.getData(); EXPECT_TRUE(cp); EXPECT_FALSE(memcmp(exp_val2, obuffer.getData(), sizeof(uint64_t))); } diff --git a/src/lib/util/tests/io_utilities_unittest.cc b/src/lib/util/tests/io_utilities_unittest.cc index acaaf13f02..003f14fd9f 100644 --- a/src/lib/util/tests/io_utilities_unittest.cc +++ b/src/lib/util/tests/io_utilities_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -65,7 +65,7 @@ TEST(asioutil, writeUint16) { writeUint16(i16, test, sizeof(test)); // ... and compare - const uint8_t* ref = static_cast<const uint8_t*>(buffer.getData()); + const uint8_t* ref = buffer.getData(); EXPECT_EQ(ref[0], test[0]); EXPECT_EQ(ref[1], test[1]); } |