]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#749] Converted assert to throw outside dns lib
authorFrancis Dupont <fdupont@isc.org>
Fri, 31 Jul 2020 22:55:21 +0000 (00:55 +0200)
committerThomas Markwalder <tmark@isc.org>
Thu, 13 Aug 2020 13:59:21 +0000 (09:59 -0400)
18 files changed:
src/bin/d2/d2_update_message.cc
src/lib/asiodns/io_fetch.cc
src/lib/asiolink/tcp_socket.h
src/lib/asiolink/udp_socket.h
src/lib/dhcp/libdhcp++.cc
src/lib/dhcp/option_custom.cc
src/lib/dhcp/option_custom.h
src/lib/dhcp/pkt_filter_inet6.cc
src/lib/log/logger_manager_impl.cc
src/lib/log/message_reader.cc
src/lib/util/buffer.h
src/lib/util/encode/base16_from_binary.h
src/lib/util/encode/base32hex_from_binary.h
src/lib/util/encode/base_n.cc
src/lib/util/io/sockaddr_util.h
src/lib/util/io/socketsession.cc
src/lib/util/random/random_number_generator.h
src/lib/util/tests/buffer_unittest.cc

index 7eb5ab813add7c7aae54e1b3b32dbdca5f4833bf..115663fa80d3efbe79e9f43e32f93a751365a05e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2015,2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2020 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
@@ -152,7 +152,9 @@ D2UpdateMessage::fromWire(const void* received_data, size_t bytes_received,
         // If the Zone counter is greater than 0 (which we have checked)
         // there must be a valid Question pointer stored in the message_
         // object. If there isn't, it is a programming error.
-        assert(question);
+        if (!question) {
+            isc_throw(isc::Unexpected, "question is null?!");
+        }
         zone_.reset(new D2Zone(question->getName(), question->getClass()));
 
     } else {
@@ -226,4 +228,3 @@ D2UpdateMessage::validateResponse() const {
 
 } // namespace d2
 } // namespace isc
-
index d5405c518bcf5c0a28254c2700fcee703ec57e53..5ad10dfdb736e1e38284437d85593d2cd8db521c 100644 (file)
@@ -394,10 +394,12 @@ IOFetch::stop(Result result) {
 void IOFetch::logIOFailure(boost::system::error_code ec) {
 
     // Should only get here with a known error code.
-    assert((data_->origin == ASIODNS_OPEN_SOCKET) ||
-           (data_->origin == ASIODNS_SEND_DATA) ||
-           (data_->origin == ASIODNS_READ_DATA) ||
-           (data_->origin == ASIODNS_UNKNOWN_ORIGIN));
+    if ((data_->origin != ASIODNS_OPEN_SOCKET) &&
+        (data_->origin != ASIODNS_SEND_DATA) &&
+        (data_->origin != ASIODNS_READ_DATA) &&
+        (data_->origin != ASIODNS_UNKNOWN_ORIGIN)) {
+        isc_throw(isc::Unexpected, "impossible error code " << data_->origin);
+    }
 
     LOG_ERROR(logger, data_->origin).arg(ec.value()).
         arg((data_->remote_snd->getProtocol() == IPPROTO_TCP) ?
index b98907a17a49f1b2b52eb7efe3cbcf528991f0dd..137b828385e5672229531c58bd7e4e0c01133843 100644 (file)
@@ -16,7 +16,6 @@
 #include <unistd.h>             // for some IPC/network system calls
 
 #include <algorithm>
-#include <cassert>
 #include <cstddef>
 
 #include <boost/numeric/conversion/cast.hpp>
@@ -29,6 +28,8 @@
 #include <asiolink/io_service.h>
 #include <asiolink/tcp_endpoint.h>
 
+#include <exceptions/isc_assert.h>
+
 namespace isc {
 namespace asiolink {
 
@@ -299,7 +300,7 @@ TCPSocket<C>::open(const IOEndpoint* endpoint, C& callback) {
     // IOEndpoint is the base class of UDPEndpoint and TCPEndpoint, it does not
     // contain a method for getting at the underlying endpoint type - that is in
     /// the derived class and the two classes differ on return type.
-    assert(endpoint->getProtocol() == IPPROTO_TCP);
+    isc_throw_assert(endpoint->getProtocol() == IPPROTO_TCP);
     const TCPEndpoint* tcp_endpoint =
         static_cast<const TCPEndpoint*>(endpoint);
 
@@ -381,7 +382,7 @@ TCPSocket<C>::asyncReceive(void* data, size_t length, size_t offset,
         // does not contain a method for getting at the underlying endpoint
         // type - that is in the derived class and the two classes differ on
         // return type.
-        assert(endpoint->getProtocol() == IPPROTO_TCP);
+        isc_throw_assert(endpoint->getProtocol() == IPPROTO_TCP);
         TCPEndpoint* tcp_endpoint = static_cast<TCPEndpoint*>(endpoint);
 
         // Write the endpoint details from the communications link.  Ideally
index 796abaf6c21901f40c50448c87d456a848d94fc1..afb742608bae82de44c0dd19b5788eb572eae005 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -22,6 +22,8 @@
 #include <asiolink/io_service.h>
 #include <asiolink/udp_endpoint.h>
 
+#include <exceptions/isc_assert.h>
+
 namespace isc {
 namespace asiolink {
 
@@ -228,7 +230,7 @@ UDPSocket<C>::asyncSend(const void* data, size_t length,
         // does not contain a method for getting at the underlying endpoint
         // type - that is in the derived class and the two classes differ on
         // return type.
-        assert(endpoint->getProtocol() == IPPROTO_UDP);
+        isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
         const UDPEndpoint* udp_endpoint =
             static_cast<const UDPEndpoint*>(endpoint);
 
@@ -251,7 +253,7 @@ UDPSocket<C>::asyncReceive(void* data, size_t length, size_t offset,
     if (isopen_) {
 
         // Upconvert the endpoint again.
-        assert(endpoint->getProtocol() == IPPROTO_UDP);
+        isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
         UDPEndpoint* udp_endpoint = static_cast<UDPEndpoint*>(endpoint);
 
         // Ensure we can write into the buffer
index 6a6ba1344bc2cd308e6ac38f6a120d35b05f3f28..6c417d24ce4a6a07a6e354204c3022932c7c0b08 100644 (file)
@@ -18,6 +18,7 @@
 #include <dhcp/std_option_defs.h>
 #include <dhcp/docsis3_option_defs.h>
 #include <exceptions/exceptions.h>
+#include <exceptions/isc_assert.h>
 #include <util/buffer.h>
 
 #include <boost/lexical_cast.hpp>
@@ -433,7 +434,7 @@ LibDHCP::unpackOptions6(const OptionBuffer& buf,
                 // The option definition has been found. Use it to create
                 // the option instance from the provided buffer chunk.
                 const OptionDefinitionPtr& def = *(range.first);
-                assert(def);
+                isc_throw_assert(def);
                 opt = def->optionFactory(Option::V6, opt_type,
                                          buf.begin() + offset,
                                          buf.begin() + offset + opt_len);
@@ -586,7 +587,7 @@ LibDHCP::unpackOptions4(const OptionBuffer& buf,
                 // The option definition has been found. Use it to create
                 // the option instance from the provided buffer chunk.
                 const OptionDefinitionPtr& def = *(range.first);
-                assert(def);
+                isc_throw_assert(def);
                 opt = def->optionFactory(Option::V4, opt_type,
                                          buf.begin() + offset,
                                          buf.begin() + offset + opt_len);
@@ -675,7 +676,7 @@ LibDHCP::unpackVendorOptions6(const uint32_t vendor_id,
                 // The option definition has been found. Use it to create
                 // the option instance from the provided buffer chunk.
                 const OptionDefinitionPtr& def = *(range.first);
-                assert(def);
+                isc_throw_assert(def);
                 opt = def->optionFactory(Option::V6, opt_type,
                                          buf.begin() + offset,
                                          buf.begin() + offset + opt_len);
@@ -786,7 +787,7 @@ LibDHCP::unpackVendorOptions4(const uint32_t vendor_id, const OptionBuffer& buf,
                     // The option definition has been found. Use it to create
                     // the option instance from the provided buffer chunk.
                     const OptionDefinitionPtr& def = *(range.first);
-                    assert(def);
+                    isc_throw_assert(def);
                     opt = def->optionFactory(Option::V4, opt_type,
                                              buf.begin() + offset,
                                              buf.begin() + offset + opt_len);
index ac487f085df9a60c36b99ff2611005c1bbb174d8..dfa22dfec15869ec75721fa8799fe2c584a9e882 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2020 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
@@ -8,6 +8,7 @@
 #include <dhcp/libdhcp++.h>
 #include <dhcp/option_data_types.h>
 #include <dhcp/option_custom.h>
+#include <exceptions/isc_assert.h>
 #include <util/encode/hex.h>
 
 using namespace isc::asiolink;
@@ -316,7 +317,7 @@ OptionCustom::createBuffers(const OptionBuffer& data_buf) {
                 // Code copied from the standard array case
                 size_t data_size = bufferLength(fields.back(), true,
                                                 data, data_buf.end());
-                assert(data_size > 0);
+                isc_throw_assert(data_size > 0);
                 if (std::distance(data, data_buf.end()) < data_size) {
                     break;
                 }
@@ -355,7 +356,7 @@ OptionCustom::createBuffers(const OptionBuffer& data_buf) {
                 // etc. This is because OptionDefinition::validate function should
                 // have checked this already. Thus data_size must be greater than
                 // zero.
-                assert(data_size > 0);
+                isc_throw_assert(data_size > 0);
                 // Get chunks of data and store as a collection of buffers.
                 // Truncate any remaining part which length is not divisible by
                 // data_size. Note that it is ok to truncate the data if and only
index 1dcdc00264b1704ab53c73f3172685e275e3e4e2..73be64efe6ad5100ade2b190cd3eb5d6389033ff 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2020 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
@@ -10,6 +10,7 @@
 #include <asiolink/io_address.h>
 #include <dhcp/option.h>
 #include <dhcp/option_definition.h>
+#include <exceptions/isc_assert.h>
 #include <util/io_utilities.h>
 
 namespace isc {
@@ -256,7 +257,7 @@ public:
         checkDataType<T>(index);
         // When we created the buffer we have checked that it has a
         // valid size so this condition here should be always fulfilled.
-        assert(buffers_[index].size() == OptionDataTypeTraits<T>::len);
+        isc_throw_assert(buffers_[index].size() == OptionDataTypeTraits<T>::len);
         // Read an integer value.
         return (OptionDataTypeUtil::readInt<T>(buffers_[index]));
     }
@@ -490,7 +491,7 @@ OptionCustom::checkDataType(const uint32_t index) const {
             // When we initialized buffers we have already checked that
             // the number of these buffers is equal to number of option
             // fields in the record so the condition below should be met.
-            assert(index < record_fields.size());
+            isc_throw_assert(index < record_fields.size());
             // Get the data type to be returned.
             data_type = record_fields[index];
         }
index 9b92bb9b7564d6ea87e9cbf7a6494dca2c4ffe1e..20d682cfbefbe0f26edb41354c539ebb70a114c3 100644 (file)
@@ -9,6 +9,7 @@
 #include <dhcp/iface_mgr.h>
 #include <dhcp/pkt6.h>
 #include <dhcp/pkt_filter_inet6.h>
+#include <exceptions/isc_assert.h>
 #include <util/io/pktinfo_utilities.h>
 
 #include <fcntl.h>
@@ -305,7 +306,7 @@ PktFilterInet6::send(const Iface&, uint16_t sockfd, const Pkt6Ptr& pkt) {
     // CMSG_FIRSTHDR() is coded to return NULL as a possibility.  The
     // following assertion should never fail, but if it did and you came
     // here, fix the code. :)
-    assert(cmsg != NULL);
+    isc_throw_assert(cmsg != NULL);
 
     cmsg->cmsg_level = IPPROTO_IPV6;
     cmsg->cmsg_type = IPV6_PKTINFO;
index e7cc9fe3397cb0ad71f4a125994c081de9088451..e8e4bfd0762ef0e2dcae8119579810990a69cc7f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2019 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -28,6 +28,8 @@
 #include <log/logger_specification.h>
 #include <log/buffer_appender_impl.h>
 
+#include <exceptions/isc_assert.h>
+
 #include <boost/lexical_cast.hpp>
 
 using namespace std;
@@ -275,7 +277,7 @@ void LoggerManagerImpl::flushBufferAppenders() {
     for (it = copy.begin(); it != copy.end(); ++it) {
         internal::BufferAppender* app =
             dynamic_cast<internal::BufferAppender*>(it->get());
-        assert(app != NULL);
+        isc_throw_assert(app);
         app->flush();
     }
 }
index 91593045820c84e1d5a775969ec2cb40310e0b27..f5b0d3907c9707bfce79bdb593bc4c370a254faf 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -6,7 +6,6 @@
 
 #include <config.h>
 
-#include <cassert>
 #include <errno.h>
 #include <string.h>
 #include <iostream>
@@ -14,6 +13,7 @@
 #include <iostream>
 #include <fstream>
 
+#include <exceptions/isc_assert.h>
 #include <log/log_messages.h>
 #include <log/message_exception.h>
 #include <log/message_reader.h>
@@ -121,7 +121,7 @@ void
 MessageReader::parsePrefix(const vector<string>& tokens) {
 
     // Should not get here unless there is something in the tokens array.
-    assert(!tokens.empty());
+    isc_throw_assert(!tokens.empty());
 
     // Process $PREFIX.  With no arguments, the prefix is set to the empty
     // string.  One argument sets the prefix to the to its value and more than
@@ -219,7 +219,7 @@ MessageReader::parseMessage(const std::string& text, MessageReader::Mode mode) {
 
     // The line passed should be at least one character long and start with the
     // message introducer (else we should not have got here).
-    assert((text.size() >= 1) && (text[0] == MESSAGE_FLAG));
+    isc_throw_assert((text.size() >= 1) && (text[0] == MESSAGE_FLAG));
 
     // A line comprising just the message introducer is not valid.
     if (text.size() == 1) {
index 5640595053810d7ef454a168931a9c682d7786a4..128db9998097a5da34ea28641b8484cbad6afa2f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2020 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
@@ -408,7 +408,10 @@ public:
     ///
     /// \param pos The position in the buffer to be returned.
     uint8_t operator[](size_t pos) const {
-        assert (pos < size_);
+        if (pos >= size_) {
+            isc_throw(InvalidBufferPosition,
+                      "[]: pos (" << pos << ") >= size (" << size_ << ")");
+        }
         return (buffer_[pos]);
     }
     //@}
index f5674fdac4a9de3750364ae0b8e14e601d8b664d..3eb697dcc3405f362a7e59c1f88f075f8a24422a 100644 (file)
@@ -11,7 +11,7 @@
 
 //  See http://www.boost.org for updates, documentation, and revision history.
 
-#include <cassert>
+#include <exceptions/isc_assert.h>
 
 #include <cstddef> // size_t
 #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
@@ -40,7 +40,7 @@ struct from_4_bit {
         const char * lookup_table = 
             "0123456789"
             "ABCDEF";
-        assert(t < 16);
+        isc_throw_assert(t < 16);
         return (lookup_table[static_cast<size_t>(t)]);
     }
 };
index 39216c2471a0e0e9941db79a3aaf70fc279e5e1b..84f2b69b3f3da66d3b51a75326ad17e95a70e2eb 100644 (file)
@@ -11,7 +11,7 @@
 
 //  See http://www.boost.org for updates, documentation, and revision history.
 
-#include <cassert>
+#include <exceptions/isc_assert.h>
 
 #include <cstddef> // size_t
 #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
@@ -42,7 +42,7 @@ struct from_5_bit {
         const char * lookup_table = 
             "0123456789"
             "ABCDEFGHIJKLMNOPQRSTUV";
-        assert(t < 32);
+        isc_throw_assert(t < 32);
         return (lookup_table[static_cast<size_t>(t)]);
     }
 };
index 82e54824922460a8d39b5a458888a2f284a6d7b0..eb0eb2358f7a4c0c3b7372cdb385f8f989a9bfe3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2019 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2020 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
@@ -14,6 +14,7 @@
 #include <util/encode/base64.h>
 
 #include <exceptions/exceptions.h>
+#include <exceptions/isc_assert.h>
 
 #include <boost/archive/iterators/base64_from_binary.hpp>
 #include <boost/archive/iterators/binary_from_base64.hpp>
@@ -26,7 +27,6 @@
 
 #include <stdint.h>
 #include <stdexcept>
-#include <cassert>
 #include <iterator>
 #include <string>
 #include <vector>
@@ -308,7 +308,7 @@ BaseNTransformer<BitsPerChunk, BaseZeroCode, Encoder, Decoder>::encode(
     result.reserve(len);
     result.assign(Encoder(EncodeNormalizer(binary.begin(), binary.end())),
                   Encoder(EncodeNormalizer(binary.end(), binary.end())));
-    assert(len >= result.length());
+    isc_throw_assert(len >= result.length());
     result.append(len - result.length(), BASE_PADDING_CHAR);
     return (result);
 }
@@ -402,7 +402,7 @@ BaseNTransformer<BitsPerChunk, BaseZeroCode, Encoder, Decoder>::decode(
     // data, that is, that the first byte of padding is indeed 0.
     // (DecodeNormalizer and binary_from_baseXX ensure that the rest of the
     // padding is all zero).
-    assert(result.size() >= padbytes);
+    isc_throw_assert(result.size() >= padbytes);
     if (padbytes > 0 && *(result.end() - padbytes) != 0) {
             isc_throw(BadValue, "Non 0 bits included in " << algorithm
                       << " padding: " << input);
index 554602fdd40ede34124590a5144c8ec27e930511..1cb31f2028a4e0365948ff6c2a6d440732204b50 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -11,7 +11,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#include <cassert>
+#include <exceptions/isc_assert.h>
 
 // These definitions in this file are for the convenience of internal
 // implementation and test code, and are not intended to be used publicly.
@@ -27,7 +27,7 @@ getSALength(const struct sockaddr& sa) {
     if (sa.sa_family == AF_INET) {
         return (sizeof(struct sockaddr_in));
     } else {
-        assert(sa.sa_family == AF_INET6);
+        isc_throw_assert(sa.sa_family == AF_INET6);
         return (sizeof(struct sockaddr_in6));
     }
 }
index ff51debc8d99ba238a7dc4fe4c2421b0c9ac5ed4..573931a30e3315fb94fb300204fb72cc92017710 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2019 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -22,7 +22,6 @@
 #include <csignal>
 #include <cstddef>
 #include <cstring>
-#include <cassert>
 
 #include <string>
 #include <vector>
@@ -30,6 +29,7 @@
 #include <boost/noncopyable.hpp>
 
 #include <exceptions/exceptions.h>
+#include <exceptions/isc_assert.h>
 
 #include <util/buffer.h>
 
@@ -105,7 +105,7 @@ SocketSessionForwarder::SocketSessionForwarder(const std::string& unix_file) :
     memset(&impl.sock_un_.sun_path, 0, sizeof(impl.sock_un_.sun_path));
     strncpy(impl.sock_un_.sun_path, unix_file.c_str(),
             sizeof(impl.sock_un_.sun_path) - 1);
-    assert(impl.sock_un_.sun_path[sizeof(impl.sock_un_.sun_path) - 1] == '\0');
+    isc_throw_assert(impl.sock_un_.sun_path[sizeof(impl.sock_un_.sun_path) - 1] == '\0');
     impl.sock_un_len_ = offsetof(struct sockaddr_un, sun_path) +
         unix_file.length();
 #ifdef HAVE_SA_LEN
@@ -232,7 +232,7 @@ SocketSessionForwarder::push(int sock, int family, int type, int protocol,
     impl_->buf_.writeData(&remote_end, getSALength(remote_end));
     // Data length.  Must be fit uint32 due to the range check above.
     const uint32_t data_len32 = static_cast<uint32_t>(data_len);
-    assert(data_len == data_len32); // shouldn't cause overflow.
+    isc_throw_assert(data_len == data_len32); // shouldn't cause overflow.
     impl_->buf_.writeUint32(data_len32);
     // Write the resulting header length at the beginning of the buffer
     impl_->buf_.writeUint16At(impl_->buf_.getLength() - sizeof(uint16_t), 0);
index da3f0be407c444b2768ac32af955ea2d2bf0bdbc..5f50ee7ccffb28ebe8fb0def19709310c64158f7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2020 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
@@ -92,12 +92,12 @@ class WeightedRandomIntegerGenerator {
 public:
     /// \brief Constructor
     ///
-    /// \param probabilities The probabilities for all the integers, the probability must be 
+    /// \param probabilities The probabilities for all the integers, the probability must be
     /// between 0 and 1.0, the sum of probabilities must be equal to 1.
     /// For example, if the probabilities contains the following values:
     /// 0.5 0.3 0.2, the 1st integer will be generated more frequently than the
     /// other integers and the probability is proportional to its value.
-    /// \param min The minimum integer that generated, other integers will be 
+    /// \param min The minimum integer that generated, other integers will be
     /// min, min + 1, ..., min + probabilities.size() - 1
     WeightedRandomIntegerGenerator(const std::vector<double>& probabilities,
         size_t min = 0):
@@ -105,7 +105,7 @@ public:
     {
         // The probabilities must be valid.  Checking is quite an expensive
         // operation, so is only done in a debug build.
-        assert(areProbabilitiesValid(probabilities));
+        areProbabilitiesValid(probabilities);
 
         // Calculate the partial sum of probabilities
         std::partial_sum(probabilities.begin(), probabilities.end(),
@@ -129,7 +129,7 @@ public:
     void reset(const std::vector<double>& probabilities, size_t min = 0)
     {
         // The probabilities must be valid.
-        assert(areProbabilitiesValid(probabilities));
+        areProbabilitiesValid(probabilities);
 
         // Reset the cumulative sum
         cumulative_.clear();
@@ -145,7 +145,7 @@ public:
     /// \brief Generate weighted random integer
     size_t operator()()
     {
-        return std::lower_bound(cumulative_.begin(), cumulative_.end(), uniform_real_gen_()) 
+        return std::lower_bound(cumulative_.begin(), cumulative_.end(), uniform_real_gen_())
             - cumulative_.begin() + min_;
     }
 
@@ -161,31 +161,31 @@ private:
     /// error, an exception is thrown.  This makes unit testing somewhat easier.
     ///
     /// \param probabilities Vector of probabilities.
-    bool areProbabilitiesValid(const std::vector<double>& probabilities) const
+    /// \throw InvalidProbValue or SumNotOne when not valid.
+    void areProbabilitiesValid(const std::vector<double>& probabilities) const
     {
-        typedef std::vector<double>::const_iterator Iterator;
         double sum = probabilities.empty() ? 1.0 : 0.0;
-        for(Iterator it = probabilities.begin(); it != probabilities.end(); ++it){
+        for (const double it : probabilities) {
             //The probability must be in [0, 1.0]
-            if(*it < 0.0 || *it > 1.0) {
+            if (it < 0.0 || it > 1.0) {
                 isc_throw(InvalidProbValue,
                           "probability must be in the range 0..1");
             }
 
-            sum += *it;
+            sum += it;
         }
 
         double epsilon = 0.0001;
         // The sum must be equal to 1
-       if (std::fabs(sum - 1.0) >= epsilon) {
-           isc_throw(SumNotOne, "Sum of probabilities is not equal to 1");
-       }
+        if (std::fabs(sum - 1.0) >= epsilon) {
+            isc_throw(SumNotOne, "Sum of probabilities is not equal to 1");
+        }
 
-       return true;
+        return;
     }
 
     std::vector<double> cumulative_;    ///< Partial sum of the probabilities
-    boost::mt19937 rng_;                ///< Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator 
+    boost::mt19937 rng_;                ///< Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator
     boost::uniform_real<> dist_;        ///< Uniformly distributed real numbers
 
     // Shortcut typedef
index 9e4294f951a38011b21f7b5330fce044541afc36..1f631673daba86e817e5798fe1b5b900b875e10d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2020 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
@@ -181,21 +181,7 @@ TEST_F(BufferTest, outputBufferReadAt) {
     for (int i = 0; i < sizeof(testdata); i ++) {
         EXPECT_EQ(testdata[i], obuffer[i]);
     }
-#ifdef EXPECT_DEATH
-    // We use assert now, so we check it dies
-    if (!isc::util::unittests::runningOnValgrind()) {
-        EXPECT_DEATH({
-            isc::util::unittests::dontCreateCoreDumps();
-
-            try {
-                obuffer[sizeof(testdata)];
-            } catch (...) {
-                // Prevent exceptions killing the application, we need
-                // to make sure it dies the real hard way
-            }
-            }, "");
-    }
-#endif
+    EXPECT_THROW(obuffer[sizeof(testdata)], isc::util::InvalidBufferPosition);
 }
 
 TEST_F(BufferTest, outputBufferClear) {