From: Remi Gacogne Date: Mon, 19 Mar 2018 13:00:26 +0000 (+0100) Subject: dnsdist: Get rid of VLAs in the console X-Git-Tag: dnsdist-1.3.0~31^2~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=13d895c918f3ad216bfefe2bdc2c064a448e1f4d;p=thirdparty%2Fpdns.git dnsdist: Get rid of VLAs in the console --- diff --git a/pdns/sodcrypto.cc b/pdns/sodcrypto.cc index cbf672ad22..daff010ff6 100644 --- a/pdns/sodcrypto.cc +++ b/pdns/sodcrypto.cc @@ -30,30 +30,48 @@ string newKey() { - unsigned char key[crypto_secretbox_KEYBYTES]; - randombytes_buf(key, sizeof key); - return "\""+Base64Encode(string((char*)key, sizeof key))+"\""; + std::string key; + key.resize(crypto_secretbox_KEYBYTES); + + randombytes_buf(reinterpret_cast(&key.at(0)), key.size()); + + return "\""+Base64Encode(key)+"\""; } std::string sodEncryptSym(const std::string& msg, const std::string& key, SodiumNonce& nonce) { - unsigned char ciphertext[msg.length() + crypto_secretbox_MACBYTES]; - crypto_secretbox_easy(ciphertext, (unsigned char*)msg.c_str(), msg.length(), nonce.value, (unsigned char*)key.c_str()); + std::string ciphertext; + ciphertext.resize(msg.length() + crypto_secretbox_MACBYTES); + crypto_secretbox_easy(reinterpret_cast(&ciphertext.at(0)), + reinterpret_cast(msg.c_str()), + msg.length(), + nonce.value, + reinterpret_cast(key.c_str())); nonce.increment(); - return string((char*)ciphertext, sizeof(ciphertext)); + return ciphertext; } std::string sodDecryptSym(const std::string& msg, const std::string& key, SodiumNonce& nonce) { - unsigned char decrypted[msg.length() - crypto_secretbox_MACBYTES]; + std::string decrypted; + + if (msg.length() < crypto_secretbox_MACBYTES) { + throw std::runtime_error("Could not decrypt message of size " + msg.length()); + } + + decrypted.resize(msg.length() - crypto_secretbox_MACBYTES); - if (crypto_secretbox_open_easy(decrypted, (const unsigned char*)msg.c_str(), - msg.length(), nonce.value, (const unsigned char*)key.c_str()) != 0) { + if (crypto_secretbox_open_easy(reinterpret_cast(&decrypted.at(0)), + reinterpret_cast(msg.c_str()), + msg.length(), + nonce.value, + reinterpret_cast(key.c_str())) != 0) { throw std::runtime_error("Could not decrypt message"); } + nonce.increment(); - return string((char*)decrypted, sizeof(decrypted)); + return decrypted; } #else std::string sodEncryptSym(const std::string& msg, const std::string& key, SodiumNonce& nonce)