From: Remi Gacogne Date: Fri, 23 Mar 2018 16:11:43 +0000 (+0100) Subject: Use obj.data() instead of &obj.at(0) to prevent exception X-Git-Tag: dnsdist-1.3.0~31^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c2a16005ce822b3d704d2e717d734ee94e4ed189;p=thirdparty%2Fpdns.git Use obj.data() instead of &obj.at(0) to prevent exception In case of a 0-sized container, &obj.at(0) would throw an exception even if we don't intend to use the pointer afterward. obj.data is required to provide a non-nullptr pointer that we should not dereference for most containers (except strings since C++11), but that's fine for the way we intend (not to) use it. --- diff --git a/modules/pipebackend/coprocess.cc b/modules/pipebackend/coprocess.cc index 029a8b8dac..f3967301d0 100644 --- a/modules/pipebackend/coprocess.cc +++ b/modules/pipebackend/coprocess.cc @@ -51,7 +51,7 @@ CoProcess::CoProcess(const string &command,int timeout, int infd, int outfd) for (size_t n = 0; n < v.size(); n++) argv[n]=v[n].c_str(); // we get away with not copying since nobody resizes v - launch(&argv.at(0), timeout, infd, outfd); + launch(argv.data(), timeout, infd, outfd); } void CoProcess::launch(const char **argv, int timeout, int infd, int outfd) diff --git a/modules/remotebackend/pipeconnector.cc b/modules/remotebackend/pipeconnector.cc index ba3aaba9a4..b7fd605ee5 100644 --- a/modules/remotebackend/pipeconnector.cc +++ b/modules/remotebackend/pipeconnector.cc @@ -107,7 +107,7 @@ void PipeConnector::launch() { // stdin & stdout are now connected, fire up our coprocess! - if(execv(argv[0], const_cast(&argv[0]))<0) // now what + if(execv(argv[0], const_cast(argv.data()))<0) // now what exit(123); /* not a lot we can do here. We shouldn't return because that will leave a forked process around. diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 124745f54c..91ee527c40 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -561,7 +561,7 @@ void* tcpClientThread(int pipefd) #endif responseSize += addRoom; answerBuffer.resize(responseSize); - char* response = &answerBuffer.at(0); + char* response = answerBuffer.data(); readn2WithTimeout(dsock, response, rlen, ds->tcpRecvTimeout); uint16_t responseLen = rlen; if (outstanding) { diff --git a/pdns/misc.cc b/pdns/misc.cc index 11d31f4e85..78ec742b9b 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -363,7 +363,7 @@ int waitForMultiData(const set& fds, const int seconds, const int useconds, } std::vector pfds(realFDs.size()); - memset(&pfds.at(0), 0, realFDs.size()*sizeof(struct pollfd)); + memset(pfds.data(), 0, realFDs.size()*sizeof(struct pollfd)); int ctr = 0; for (const auto& fd : realFDs) { pfds[ctr].fd = fd; @@ -373,9 +373,9 @@ int waitForMultiData(const set& fds, const int seconds, const int useconds, int ret; if(seconds >= 0) - ret = poll(&pfds.at(0), realFDs.size(), seconds * 1000 + useconds/1000); + ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000); else - ret = poll(&pfds.at(0), realFDs.size(), -1); + ret = poll(pfds.data(), realFDs.size(), -1); if(ret <= 0) return ret; diff --git a/pdns/opensslsigners.cc b/pdns/opensslsigners.cc index 3176744d45..daff5a5384 100644 --- a/pdns/opensslsigners.cc +++ b/pdns/opensslsigners.cc @@ -388,14 +388,14 @@ std::string OpenSSLRSADNSCryptoKeyEngine::getPubKeyHash() const throw runtime_error(getName()+" failed to init hash context for generating the public key hash"); } - int len = BN_bn2bin(e, &tmp.at(0)); - res = SHA1_Update(&ctx, &tmp.at(0), len); + int len = BN_bn2bin(e, tmp.data()); + res = SHA1_Update(&ctx, tmp.data(), len); if (res != 1) { throw runtime_error(getName()+" failed to update hash context for generating the public key hash"); } - len = BN_bn2bin(n, &tmp.at(0)); - res = SHA1_Update(&ctx, &tmp.at(0), len); + len = BN_bn2bin(n, tmp.data()); + res = SHA1_Update(&ctx, tmp.data(), len); if (res != 1) { throw runtime_error(getName()+" failed to update hash context for generating the public key hash"); } diff --git a/pdns/pkcs11signers.cc b/pdns/pkcs11signers.cc index 4ad764c488..041cb8c177 100644 --- a/pdns/pkcs11signers.cc +++ b/pdns/pkcs11signers.cc @@ -673,7 +673,7 @@ CK_RV Pkcs11Slot::HuntSlot(const string& tokenId, CK_SLOT_ID &slotId, _CK_SLOT_I // get the actual slot ids std::vector slotIds(slots); - err = functions->C_GetSlotList(CK_FALSE, &slotIds.at(0), &slots); + err = functions->C_GetSlotList(CK_FALSE, slotIds.data(), &slots); if (err) { L<