Remove remaining usages of VLAs

This commit is contained in:
Peter Keresztes Schmidt 2021-05-16 19:16:08 +02:00
parent c0017a5263
commit 298415fff3
6 changed files with 52 additions and 31 deletions

View File

@ -36,7 +36,7 @@
int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
va_list arg_ptr;
iovec iov[iovcnt];
std::vector<iovec> iov(iovcnt);
va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) {
@ -45,7 +45,7 @@ int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
}
va_end(arg_ptr);
int nBytes = ::readv(mRd, iov, iovcnt);
int nBytes = ::readv(mRd, iov.data(), iovcnt);
if (nBytes < 0) {
Debug(1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno));
}
@ -54,7 +54,7 @@ int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) {
int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
va_list arg_ptr;
iovec iov[iovcnt];
std::vector<iovec> iov(iovcnt);
va_start(arg_ptr, iovcnt);
for (int i = 0; i < iovcnt; i++) {
@ -63,7 +63,7 @@ int ZM::CommsBase::writeV(int iovcnt, /* const void *, int, */ ...) {
}
va_end(arg_ptr);
ssize_t nBytes = ::writev(mWd, iov, iovcnt);
ssize_t nBytes = ::writev(mWd, iov.data(), iovcnt);
if (nBytes < 0) {
Debug(1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno));
}

View File

@ -243,27 +243,27 @@ class Socket : public CommsBase {
return nBytes;
}
virtual int recv(std::string &msg) const {
char buffer[msg.capacity()];
int nBytes = 0;
if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) {
Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", sizeof(buffer), mSd, strerror(errno));
virtual ssize_t recv(std::string &msg) const {
std::vector<char> buffer(msg.capacity());
ssize_t nBytes;
if ((nBytes = ::recv(mSd, buffer.data(), buffer.size(), 0)) < 0) {
Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", msg.size(), mSd, strerror(errno));
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
msg = {buffer.begin(), buffer.begin() + nBytes};
return nBytes;
}
virtual int recv(std::string &msg, size_t maxLen) const {
char buffer[maxLen];
int nBytes = 0;
if ((nBytes = ::recv(mSd, buffer, sizeof(buffer), 0)) < 0) {
virtual ssize_t recv(std::string &msg, size_t maxLen) const {
std::vector<char> buffer(maxLen);
ssize_t nBytes;
if ((nBytes = ::recv(mSd, buffer.data(), buffer.size(), 0)) < 0) {
Debug(1, "Recv of %zd bytes max to string on sd %d failed: %s", maxLen, mSd, strerror(errno));
return nBytes;
}
buffer[nBytes] = '\0';
msg = buffer;
msg = {buffer.begin(), buffer.begin() + nBytes};
return nBytes;
}

View File

@ -529,15 +529,17 @@ void Logger::logPrint(bool hex, const char *filepath, int line, int level, const
if (level <= mDatabaseLevel) {
if (zmDbConnected) {
int syslogSize = syslogEnd-syslogStart;
char escapedString[(syslogSize*2)+1];
mysql_real_escape_string(&dbconn, escapedString, syslogStart, syslogSize);
std::string escapedString;
escapedString.reserve((syslogSize * 2) + 1);
mysql_real_escape_string(&dbconn, &escapedString[0], syslogStart, syslogSize);
escapedString.resize(std::strlen(escapedString.c_str()));
std::string sql_string = stringtf(
"INSERT INTO `Logs` "
"( `TimeKey`, `Component`, `ServerId`, `Pid`, `Level`, `Code`, `Message`, `File`, `Line` )"
" VALUES "
"( %ld.%06ld, '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
timeVal.tv_sec, timeVal.tv_usec, mId.c_str(), staticConfig.SERVER_ID, tid, level, classString, escapedString, file, line
timeVal.tv_sec, timeVal.tv_usec, mId.c_str(), staticConfig.SERVER_ID, tid, level, classString, escapedString.c_str(), file, line
);
dbQueue.push(std::move(sql_string));
} else {

View File

@ -131,8 +131,8 @@ std::string Authenticator::computeDigestResponse(const std::string &method, cons
#if HAVE_DECL_MD5 || HAVE_DECL_GNUTLS_FINGERPRINT
// The "response" field is computed as:
// md5(md5(<username>:<realm>:<password>):<nonce>:md5(<cmd>:<url>))
size_t md5len = 16;
unsigned char md5buf[md5len];
constexpr size_t md5len = 16;
uint8 md5buf[md5len];
char md5HexBuf[md5len * 2 + 1];
// Step 1: md5(<username>:<realm>:<password>)

View File

@ -236,8 +236,8 @@ User *zmLoadAuthUser(const char *auth, bool use_remote_addr) {
}
char auth_key[512] = "";
char auth_md5[32+1] = "";
size_t md5len = 16;
unsigned char md5sum[md5len];
constexpr size_t md5len = 16;
uint8 md5sum[md5len];
const char * hex = "0123456789abcdef";
while ( MYSQL_ROW dbrow = mysql_fetch_row(result) ) {

View File

@ -224,15 +224,16 @@ TEST_CASE("ZM::UdpUnixSocket send/recv") {
ZM::UdpUnixSocket srv_socket;
ZM::UdpUnixSocket client_socket;
SECTION("send/recv byte buffer") {
std::array<char, 3> msg = {'a', 'b', 'c'};
std::array<char, msg.size()> rcv{};
SECTION("send/recv on unbound socket") {
SECTION("on unbound socket") {
REQUIRE(client_socket.send(msg.data(), msg.size()) == -1);
REQUIRE(srv_socket.recv(rcv.data(), rcv.size()) == -1);
}
SECTION("send/recv") {
SECTION("on bound socket") {
REQUIRE(srv_socket.bind(sock_path.c_str()) == true);
REQUIRE(srv_socket.isOpen() == true);
@ -246,6 +247,24 @@ TEST_CASE("ZM::UdpUnixSocket send/recv") {
}
}
SECTION("send/recv string") {
std::string msg = "abc";
std::string rcv;
rcv.reserve(msg.length());
REQUIRE(srv_socket.bind(sock_path.c_str()) == true);
REQUIRE(srv_socket.isOpen() == true);
REQUIRE(client_socket.connect(sock_path.c_str()) == true);
REQUIRE(client_socket.isConnected() == true);
REQUIRE(client_socket.send(msg) == static_cast<ssize_t>(msg.size()));
REQUIRE(srv_socket.recv(rcv) == static_cast<ssize_t>(msg.size()));
REQUIRE(rcv == msg);
}
}
TEST_CASE("ZM::TcpInetClient basics") {
ZM::TcpInetClient client;
REQUIRE(client.isClosed() == true);