Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc Improvements (2) #10855

Merged
merged 9 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/JailUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ bool enterUserNS(uid_t uid, gid_t gid)

bool coolmount(const std::string& arg, std::string source, std::string target, bool silent = false)
{
source = Util::trim(source, '/');
target = Util::trim(target, '/');
source = Util::rtrim(source, '/');
target = Util::rtrim(target, '/');

if (isMountNamespacesEnabled())
{
Expand Down
31 changes: 12 additions & 19 deletions common/SpookyV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

#define ALLOW_UNALIGNED_READS true

#if defined __clang__
#define FALLTHROUGH [[clang::fallthrough]]
#elif defined __GNUC__ && __GNUC__ >= 7
#define FALLTHROUGH [[fallthrough]]
#else
#define FALLTHROUGH
#endif
//
// short hash ... it could be used on any message,
// but it's used by Spooky just for short messages.
Expand Down Expand Up @@ -86,48 +79,48 @@ void SpookyHash::Short(
switch (remainder)
{
case 15:
d += ((uint64)u.p8[14]) << 48;
FALLTHROUGH;
d += ((uint64)u.p8[14]) << 48;
[[fallthrough]];
case 14:
d += ((uint64)u.p8[13]) << 40;
FALLTHROUGH;
[[fallthrough]];
case 13:
d += ((uint64)u.p8[12]) << 32;
FALLTHROUGH;
[[fallthrough]];
case 12:
d += u.p32[2];
c += u.p64[0];
break;
case 11:
d += ((uint64)u.p8[10]) << 16;
FALLTHROUGH;
[[fallthrough]];
case 10:
d += ((uint64)u.p8[9]) << 8;
FALLTHROUGH;
[[fallthrough]];
case 9:
d += (uint64)u.p8[8];
FALLTHROUGH;
[[fallthrough]];
case 8:
c += u.p64[0];
break;
case 7:
c += ((uint64)u.p8[6]) << 48;
FALLTHROUGH;
[[fallthrough]];
case 6:
c += ((uint64)u.p8[5]) << 40;
FALLTHROUGH;
[[fallthrough]];
case 5:
c += ((uint64)u.p8[4]) << 32;
FALLTHROUGH;
[[fallthrough]];
case 4:
c += u.p32[0];
break;
case 3:
c += ((uint64)u.p8[2]) << 16;
FALLTHROUGH;
[[fallthrough]];
case 2:
c += ((uint64)u.p8[1]) << 8;
FALLTHROUGH;
[[fallthrough]];
case 1:
c += (uint64)u.p8[0];
break;
Expand Down
6 changes: 3 additions & 3 deletions common/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ namespace Util
return result;
}

std::string replaceAllOf(const std::string &str, const std::string& match, const std::string& repl)
std::string replaceAllOf(std::string_view str, std::string_view match, std::string_view repl)
{
std::ostringstream os;

Expand All @@ -240,8 +240,8 @@ namespace Util

std::string cleanupFilename(const std::string &filename)
{
static const std::string mtch(",/?:@&=+$#'\"");
static const std::string repl("------------");
constexpr std::string_view mtch(",/?:@&=+$#'\"");
constexpr std::string_view repl("------------");
return replaceAllOf(filename, mtch, repl);
}

Expand Down
4 changes: 2 additions & 2 deletions common/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ namespace Util
std::string replace(std::string s, const std::string& a, const std::string& b);

/// Replace any characters in @a matching characters in @b with replacement chars in @c and return
std::string replaceAllOf(const std::string &str, const std::string& match, const std::string& repl);
std::string replaceAllOf(std::string_view str, std::string_view match, std::string_view repl);

std::string formatLinesForLog(const std::string& s);

Expand Down Expand Up @@ -608,7 +608,7 @@ namespace Util
size_t findInVector(const std::vector<char>& tokens, const char *cstring, std::size_t offset = 0);

/// Trim trailing characters (on the right).
inline std::string_view trim(const std::string_view s, const char ch)
inline std::string_view rtrim(const std::string_view s, const char ch)
{
const size_t last = s.find_last_not_of(ch);
if (last != std::string::npos)
Expand Down
9 changes: 5 additions & 4 deletions kit/ChildSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <fstream>
#include <memory>
#include <sstream>
#include <string_view>

#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
Expand Down Expand Up @@ -2143,8 +2144,8 @@ bool ChildSession::renderSearchResult(const char* buffer, int length, const Stri

if (Png::encodeBufferToPNG(bitmapBuffer, width, height, output, eTileMode))
{
static const std::string header = "rendersearchresult:\n";
size_t responseSize = header.size() + output.size();
constexpr std::string_view header = "rendersearchresult:\n";
const size_t responseSize = header.size() + output.size();
std::vector<char> response(responseSize);
std::copy(header.begin(), header.end(), response.begin());
std::copy(output.begin(), output.end(), response.begin() + header.size());
Expand Down Expand Up @@ -3111,8 +3112,8 @@ bool ChildSession::renderShapeSelection(const StringVector& tokens)
const std::size_t outputSize = getLOKitDocument()->renderShapeSelection(&output);
if (output != nullptr && outputSize > 0)
{
static const std::string header = "shapeselectioncontent:\n";
size_t responseSize = header.size() + outputSize;
constexpr std::string_view header = "shapeselectioncontent:\n";
const size_t responseSize = header.size() + outputSize;
std::unique_ptr<char[]> response(new char[responseSize]);
std::memcpy(response.get(), header.data(), header.size());
std::memcpy(response.get() + header.size(), output, outputSize);
Expand Down
2 changes: 1 addition & 1 deletion kit/Kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ bool Document::forwardToChild(const std::string& prefix, const std::vector<char>
{
std::shared_ptr<ChildSession> session = it->second;

static const std::string disconnect("disconnect");
constexpr std::string_view disconnect("disconnect");
if (size == disconnect.size() &&
strncmp(data, disconnect.data(), disconnect.size()) == 0)
{
Expand Down
7 changes: 4 additions & 3 deletions net/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ bool SslStreamSocket::verifyCertificate()
}

LOG_TRC("Verifying certificate of [" << hostname() << ']');
X509* x509 = SSL_get_peer_certificate(_ssl);
X509* x509 = SSL_get1_peer_certificate(_ssl);
if (x509)
{
// Dump cert info, for debugging only.
Expand Down Expand Up @@ -272,7 +272,7 @@ bool SslStreamSocket::verifyCertificate()
std::string SslStreamSocket::getSslCert(std::string& subjectHash)
{
std::ostringstream strstream;
if (X509* x509 = SSL_get_peer_certificate(_ssl))
if (X509* x509 = SSL_get1_peer_certificate(_ssl))
{
Poco::Net::X509Certificate cert(x509);
cert.save(strstream);
Expand Down Expand Up @@ -1234,6 +1234,7 @@ std::shared_ptr<Socket> ServerSocket::accept()
::close(rc);
return nullptr;
}

try
{
// Create a socket object using the factory.
Expand Down Expand Up @@ -1518,7 +1519,7 @@ bool StreamSocket::parseHeader(const char *clientName,
std::chrono::duration<float, std::milli> delayMs = now - lastHTTPHeader;

// Find the end of the header, if any.
static const std::string marker("\r\n\r\n");
constexpr std::string_view marker("\r\n\r\n");
auto itBody = std::search(_inBuffer.begin(), _inBuffer.end(), marker.begin(), marker.end());
if (itBody == _inBuffer.end())
{
Expand Down
3 changes: 2 additions & 1 deletion net/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ class Socket
_noShutdown = false;
_sendBufferSize = DefaultSendBufferSize;
_owner = std::this_thread::get_id();
LOG_TRC("Created socket. Thread affinity set to " << Log::to_string(_owner) << ", " << toStringImpl());
LOG_DBG("Created socket. Thread affinity set to " << Log::to_string(_owner) << ", "
<< toStringImpl());

if constexpr (!Util::isMobileApp())
{
Expand Down
13 changes: 8 additions & 5 deletions net/Ssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

#include <config.h>

#include <unistd.h>
#include "Ssl.hpp"

#include <common/Log.hpp>
#include <Util.hpp>

#include <sys/syscall.h>
#include <unistd.h>

#ifdef __FreeBSD__
#include <pthread_np.h>
#endif

#include <sys/syscall.h>
#include <common/Log.hpp>
#include <Util.hpp>

extern "C"
{
// Multithreading support for OpenSSL.
Expand Down Expand Up @@ -108,6 +109,8 @@ SslContext::SslContext(const std::string& certFilePath, const std::string& keyFi
: _ctx(nullptr)
, _verification(verification)
{
LOG_INF("Initializing " << OPENSSL_VERSION_TEXT);

const std::vector<char> rand = Util::rng::getBytes(512);
RAND_seed(rand.data(), rand.size());

Expand Down
9 changes: 4 additions & 5 deletions net/SslSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

#pragma once

#include "Ssl.hpp"
#include "Socket.hpp"
#include <net/Ssl.hpp>
#include <net/Socket.hpp>

#include <cerrno>
#include <memory>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -368,7 +367,7 @@ class SslStreamSocket final : public StreamSocket
return rc;
}

// Fallthrough...
[[fallthrough]];
default:
{
// Effectively an EAGAIN error at the BIO layer
Expand Down Expand Up @@ -473,7 +472,7 @@ class SslStreamSocket final : public StreamSocket
auto cb = [](const char* str, size_t len, void* u) -> int
{
std::ostringstream& os = *reinterpret_cast<std::ostringstream*>(u);
os << '\n' << std::string(str, len);
os << '\n' << std::string_view(str, len);
return 1; // Apparently 0 means failure here.
};

Expand Down
2 changes: 1 addition & 1 deletion tools/WebSocketDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ClientRequestDispatcher final : public SimpleSocketHandler
LOG_TRC('#' << socket->getFD() << " handling incoming " << in.size() << " bytes.");

// Find the end of the header, if any.
static const std::string marker("\r\n\r\n");
constexpr std::string_view marker("\r\n\r\n");
auto itBody = std::search(in.begin(), in.end(),
marker.begin(), marker.end());
if (itBody == in.end())
Expand Down
2 changes: 1 addition & 1 deletion wsd/ClientSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3151,7 +3151,7 @@ std::string ClientSession::processSVGContent(const std::string& svg)
std::string::size_type pos = 0;
for (;;)
{
static const std::string prefix = "src=\"file:///tmp/";
constexpr std::string_view prefix = "src=\"file:///tmp/";
const auto start = svg.find(prefix, pos);
if (start == std::string::npos)
{
Expand Down
4 changes: 2 additions & 2 deletions wsd/DocumentBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4677,8 +4677,8 @@ void DocumentBroker::onUrpMessage(const char* data, size_t len)
const auto session = getWriteableSession();
if (session)
{
static const std::string header = "urp: ";
size_t responseSize = header.size() + len;
constexpr std::string_view header = "urp: ";
const size_t responseSize = header.size() + len;
std::vector<char> response(responseSize);
std::memcpy(response.data(), header.data(), header.size());
std::memcpy(response.data() + header.size(), data, len);
Expand Down
3 changes: 2 additions & 1 deletion wsd/Storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <memory>
#include <string>
#include <string_view>
#include <chrono>

class LockContext;
Expand Down Expand Up @@ -454,7 +455,7 @@ class StorageBase
/// Sanitize a URI by removing authorization tokens.
void sanitizeUri(Poco::URI& uri)
{
static const std::string access_token("access_token");
constexpr std::string_view access_token("access_token");

Poco::URI::QueryParameters queryParams = uri.getQueryParameters();
for (auto& param : queryParams)
Expand Down
2 changes: 1 addition & 1 deletion wsd/wopi/StorageConnectionManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class StorageConnectionManager final
/// Sanitize a URI by removing authorization tokens.
static Poco::URI sanitizeUri(Poco::URI uri)
{
static const std::string access_token("access_token");
constexpr std::string_view access_token("access_token");

Poco::URI::QueryParameters queryParams = uri.getQueryParameters();
for (auto& param : queryParams)
Expand Down
Loading