From 298415fff3ceac53d3d972700b584c08672f91eb Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 16 May 2021 19:16:08 +0200 Subject: [PATCH] Remove remaining usages of VLAs --- src/zm_comms.cpp | 8 ++++---- src/zm_comms.h | 22 +++++++++++----------- src/zm_logger.cpp | 8 +++++--- src/zm_rtsp_auth.cpp | 6 +++--- src/zm_user.cpp | 4 ++-- tests/zm_comms.cpp | 35 +++++++++++++++++++++++++++-------- 6 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/zm_comms.cpp b/src/zm_comms.cpp index 7f933d16f..bef10164d 100644 --- a/src/zm_comms.cpp +++ b/src/zm_comms.cpp @@ -36,7 +36,7 @@ int ZM::CommsBase::readV(int iovcnt, /* const void *, int, */ ...) { va_list arg_ptr; - iovec iov[iovcnt]; + std::vector 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 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)); } diff --git a/src/zm_comms.h b/src/zm_comms.h index 9126e5373..070ed5395 100644 --- a/src/zm_comms.h +++ b/src/zm_comms.h @@ -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 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 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; } diff --git a/src/zm_logger.cpp b/src/zm_logger.cpp index dd03ec9f2..872247701 100644 --- a/src/zm_logger.cpp +++ b/src/zm_logger.cpp @@ -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 { diff --git a/src/zm_rtsp_auth.cpp b/src/zm_rtsp_auth.cpp index 34fd1921b..1a7ea629b 100644 --- a/src/zm_rtsp_auth.cpp +++ b/src/zm_rtsp_auth.cpp @@ -131,9 +131,9 @@ 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(::)::md5(:)) - size_t md5len = 16; - unsigned char md5buf[md5len]; - char md5HexBuf[md5len*2+1]; + constexpr size_t md5len = 16; + uint8 md5buf[md5len]; + char md5HexBuf[md5len * 2 + 1]; // Step 1: md5(::) std::string ha1Data = username() + ":" + realm() + ":" + password(); diff --git a/src/zm_user.cpp b/src/zm_user.cpp index 254663a04..95f61da4e 100644 --- a/src/zm_user.cpp +++ b/src/zm_user.cpp @@ -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) ) { diff --git a/tests/zm_comms.cpp b/tests/zm_comms.cpp index 1146e69a5..f23e8c987 100644 --- a/tests/zm_comms.cpp +++ b/tests/zm_comms.cpp @@ -224,23 +224,42 @@ TEST_CASE("ZM::UdpUnixSocket send/recv") { ZM::UdpUnixSocket srv_socket; ZM::UdpUnixSocket client_socket; - std::array msg = {'a', 'b', 'c'}; - std::array rcv{}; + SECTION("send/recv byte buffer") { + std::array msg = {'a', 'b', 'c'}; + std::array rcv{}; - SECTION("send/recv on unbound socket") { - REQUIRE(client_socket.send(msg.data(), msg.size()) == -1); - REQUIRE(srv_socket.recv(rcv.data(), rcv.size()) == -1); + SECTION("on unbound socket") { + REQUIRE(client_socket.send(msg.data(), msg.size()) == -1); + REQUIRE(srv_socket.recv(rcv.data(), rcv.size()) == -1); + } + + SECTION("on bound socket") { + 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.data(), msg.size()) == msg.size()); + REQUIRE(srv_socket.recv(rcv.data(), rcv.size()) == msg.size()); + + REQUIRE(rcv == msg); + } } - SECTION("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.data(), msg.size()) == msg.size()); - REQUIRE(srv_socket.recv(rcv.data(), rcv.size()) == msg.size()); + REQUIRE(client_socket.send(msg) == static_cast(msg.size())); + REQUIRE(srv_socket.recv(rcv) == static_cast(msg.size())); REQUIRE(rcv == msg); }