Skip to content

Commit

Permalink
replace &foo[0] with foo.data()
Browse files Browse the repository at this point in the history
available since: https://cplusplus.github.io/LWG/issue464
and -D_GLIBCXX_ASSERTIONS aborts on an attempt to deref
a non-existing entry in the case of an empty vector

Signed-off-by: Caolán McNamara <[email protected]>
Change-Id: I5d398aff293ec982d283640b454a1af874862c3f
  • Loading branch information
caolanm committed Dec 6, 2024
1 parent a4d480a commit d8a291c
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion common/CharacterConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CharacterConverter
std::size_t in_left = source.size();

std::vector<char> buffer(8 * source.size());
char* out = &buffer[0];
char* out = buffer.data();
std::size_t out_left = buffer.size();

// Convert.
Expand Down
2 changes: 1 addition & 1 deletion common/Clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct ClipboardData
{
uint64_t len = strtoll( hexLen.c_str(), nullptr, 16 );
std::string content(len, ' ');
inStream.read(&content[0], len);
inStream.read(content.data(), len);
if (inStream.fail())
throw ParseError("error during reading the stream");
std::getline(inStream, newline, '\n');
Expand Down
2 changes: 1 addition & 1 deletion common/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Message
/// Find a subarray in the raw message.
int find(const char* sub, const std::size_t subLen) const
{
return Util::findSubArray(&_data[0], _data.size(), sub, subLen);
return Util::findSubArray(_data.data(), _data.size(), sub, subLen);
}

/// Returns true iff the subarray exists in the raw message.
Expand Down
4 changes: 2 additions & 2 deletions common/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ void Session::handleMessage(const std::vector<char> &data)
try
{
std::unique_ptr< std::vector<char> > replace;
if (UnitBase::isUnitTesting() && !Util::isFuzzing() && UnitBase::get().filterSessionInput(this, &data[0], data.size(), replace))
if (UnitBase::isUnitTesting() && !Util::isFuzzing() && UnitBase::get().filterSessionInput(this, data.data(), data.size(), replace))
{
if (replace && !replace->empty())
_handleInput(replace->data(), replace->size());
return;
}

if (!data.empty())
_handleInput(&data[0], data.size());
_handleInput(data.data(), data.size());
}
catch (const Exception& exc)
{
Expand Down
2 changes: 1 addition & 1 deletion net/DelaySocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class DelaySocket : public Socket {
{
ssize_t len;
do {
len = ::write(getFD(), &chunk->getData()[0], chunk->getData().size());
len = ::write(getFD(), chunk->getData().data(), chunk->getData().size());
} while (len < 0 && errno == EINTR);

if (len < 0)
Expand Down
2 changes: 1 addition & 1 deletion net/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ bool StreamSocket::compactChunks(MessageMap& map)
LOG_CHUNK("Pre-compact " << map._spans.size() << " chunks: \n" <<
Util::dumpHex("", "", _inBuffer.begin(), _inBuffer.end(), false));

char *first = &_inBuffer[0];
char *first = _inBuffer.data();
char *dest = first;
for (const auto &span : map._spans)
{
Expand Down
2 changes: 1 addition & 1 deletion net/Ssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ SslContext::SslContext(const std::string& certFilePath, const std::string& keyFi
, _verification(verification)
{
const std::vector<char> rand = Util::rng::getBytes(512);
RAND_seed(&rand[0], rand.size());
RAND_seed(rand.data(), rand.size());

#if OPENSSL_VERSION_NUMBER >= 0x0907000L && OPENSSL_VERSION_NUMBER < 0x10100003L
OPENSSL_config(nullptr);
Expand Down
4 changes: 2 additions & 2 deletions net/WebSocketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class WebSocketHandler : public ProtocolHandlerInterface
return false;
}

unsigned char *p = reinterpret_cast<unsigned char*>(&socket->getInBuffer()[0]);
unsigned char *p = reinterpret_cast<unsigned char*>(socket->getInBuffer().data());
_lastFlags = p[0];
const bool fin = _lastFlags & 0x80;
const WSOpCode code = static_cast<WSOpCode>(_lastFlags & 0x0f);
Expand Down Expand Up @@ -483,7 +483,7 @@ class WebSocketHandler : public ProtocolHandlerInterface
//Process data frame
readPayload(data, payloadLen, mask, _wsPayload);
#else
unsigned char * const p = reinterpret_cast<unsigned char*>(&socket->getInBuffer()[0]);
unsigned char * const p = reinterpret_cast<unsigned char*>(socket->getInBuffer().data());
_wsPayload.insert(_wsPayload.end(), p, p + len);
const size_t headerLen = 0;
const size_t payloadLen = len;
Expand Down
2 changes: 1 addition & 1 deletion net/clientnb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct Client : public Poco::Util::Application
std::shared_ptr<WebSocket> ws = session.getWebSocket();

std::string send = "hello there";
ws->sendFrame(&send[0], send.length(),
ws->sendFrame(send.data(), send.length(),
WebSocket::SendFlags::FRAME_TEXT);

for (size_t i = 0; i < 10; i++)
Expand Down
10 changes: 5 additions & 5 deletions test/DeltaTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,14 @@ void DeltaTests::testDeltaSequence()

// Stash it in the cache
LOK_ASSERT(gen.createDelta(
reinterpret_cast<unsigned char *>(&text[0]),
reinterpret_cast<unsigned char *>(text.data()),
0, 0, width, height, width, height,
TileLocation(1, 2, 3, 0, 1, 0), delta, textWid, false, LOK_TILEMODE_RGBA, rleData) == false);
LOK_ASSERT(delta.empty());

// Build a delta between text2 & textWid
LOK_ASSERT(gen.createDelta(
reinterpret_cast<unsigned char *>(&text2[0]),
reinterpret_cast<unsigned char *>(text2.data()),
0, 0, width, height, width, height,
TileLocation(1, 2, 3, 0, 1, 0), delta, text2Wid, false, LOK_TILEMODE_RGBA, rleData) == true);
LOK_ASSERT(delta.size() > 0);
Expand All @@ -445,7 +445,7 @@ void DeltaTests::testDeltaSequence()
// Build a delta between text & text2Wid
std::vector<char> two2one;
LOK_ASSERT(gen.createDelta(
reinterpret_cast<unsigned char *>(&text[0]),
reinterpret_cast<unsigned char *>(text.data()),
0, 0, width, height, width, height,
TileLocation(1, 2, 3, 0, 1, 0), two2one, textWid, false, LOK_TILEMODE_RGBA, rleData) == true);
LOK_ASSERT(two2one.size() > 0);
Expand Down Expand Up @@ -484,14 +484,14 @@ void DeltaTests::testDeltaCopyOutOfBounds()

// Stash it in the cache
LOK_ASSERT(gen.createDelta(
reinterpret_cast<unsigned char *>(&text[0]),
reinterpret_cast<unsigned char *>(text.data()),
0, 0, width, height, width, height,
TileLocation(1, 2, 3, 0, 1, 0), delta, textWid, false, LOK_TILEMODE_RGBA, rleData) == false);
LOK_ASSERT(delta.empty());

// Build a delta between the two frames
LOK_ASSERT(gen.createDelta(
reinterpret_cast<unsigned char *>(&text2[0]),
reinterpret_cast<unsigned char *>(text2.data()),
0, 0, width, height, width, height,
TileLocation(1, 2, 3, 0, 1, 0), delta, text2Wid, false, LOK_TILEMODE_RGBA, rleData) == true);
LOK_ASSERT(delta.size() > 0);
Expand Down
6 changes: 3 additions & 3 deletions test/WopiProofTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WopiProofTests : public CPPUNIT_NS::TestFixture
BIGNUM *Base64ToNum(const std::string &str)
{
std::vector<unsigned char> vec = Proof::Base64ToBytes(str);
return BN_bin2bn(&vec[0], vec.size(), nullptr);
return BN_bin2bn(vec.data(), vec.size(), nullptr);
}

void verifySignature(const std::string &access,
Expand Down Expand Up @@ -97,8 +97,8 @@ void WopiProofTests::verifySignature(const std::string &access,
std::vector<unsigned char> digest = digestEngine.digest();

LOK_ASSERT_EQUAL(1, RSA_verify(digestEngine.nid(),
&digest[0], digest.size(),
&msgProof[0], msgProof.size(),
digest.data(), digest.size(),
msgProof.data(), msgProof.size(),
rsa));

RSA_free(rsa);
Expand Down
2 changes: 1 addition & 1 deletion tools/WebSocketDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ClientRequestDispatcher final : public SimpleSocketHandler
// Skip the marker.
itBody += marker.size();

Poco::MemoryInputStream message(&in[0], in.size());
Poco::MemoryInputStream message(in.data(), in.size());
Poco::Net::HTTPRequest request;
try
{
Expand Down
6 changes: 3 additions & 3 deletions tools/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@ static void dumpPages(unsigned proc_id, unsigned parent_id, const char *type, co

if (lseek(mem_fd, page, SEEK_SET) < 0)
error(EXIT_FAILURE, errno, "Failed to seek in /proc/<pid>/mem to %lld", page);
if (read(mem_fd, &pageData[0], 0x1000) != 0x1000)
if (read(mem_fd, pageData.data(), 0x1000) != 0x1000)
error(EXIT_FAILURE, errno, "Failed to read page %lld from /proc/<pid>/mem", page);

if (lseek(parent_fd, page, SEEK_SET) < 0)
parentData.resize(0);
else if (read(parent_fd, &parentData[0], 0x1000) != 0x1000)
else if (read(parent_fd, parentData.data(), 0x1000) != 0x1000)
parentData.resize(0); // missing equivalent page.

int touched = 0;
Expand Down Expand Up @@ -567,7 +567,7 @@ static void dump_unshared(unsigned proc_id, unsigned parent_id,
printf ("\tunshared %5lld (%lldkB)\n", numOwn, numOwn * 4);

std::vector<char> compressed = compressBitmap(bitmap);
printf ("\tRLE sharing bitmap:\n%s\n", &compressed[0]);
printf ("\tRLE sharing bitmap:\n%s\n", compressed.data());

dumpPages(proc_id, parent_id, type, vunshared, space);

Expand Down
4 changes: 2 additions & 2 deletions wsd/COOLWSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,8 @@ static std::unique_ptr<COOLWSDServer> Server;

void ForKitProcWSHandler::handleMessage(const std::vector<char> &data)
{
LOG_TRC("ForKitProcWSHandler: handling incoming [" << COOLProtocol::getAbbreviatedMessage(&data[0], data.size()) << "].");
const std::string firstLine = COOLProtocol::getFirstLine(&data[0], data.size());
LOG_TRC("ForKitProcWSHandler: handling incoming [" << COOLProtocol::getAbbreviatedMessage(data.data(), data.size()) << "].");
const std::string firstLine = COOLProtocol::getFirstLine(data.data(), data.size());
const StringVector tokens = StringVector::tokenize(firstLine.data(), firstLine.size());

if (tokens.startsWith(0, "segfaultcount"))
Expand Down
4 changes: 2 additions & 2 deletions wsd/ClientRequestDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ void ClientRequestDispatcher::handleIncomingMessage(SocketDisposition& dispositi
return;
}

Poco::MemoryInputStream startmessage(&socket->getInBuffer()[0], socket->getInBuffer().size());
Poco::MemoryInputStream startmessage(socket->getInBuffer().data(), socket->getInBuffer().size());

#if 0 // debug a specific command's payload
if (Util::findInVector(socket->getInBuffer(), "insertfile") != std::string::npos)
Expand Down Expand Up @@ -693,7 +693,7 @@ void ClientRequestDispatcher::handleIncomingMessage(SocketDisposition& dispositi
{
// We may need to re-write the chunks moving the inBuffer.
socket->compactChunks(map);
Poco::MemoryInputStream message(&socket->getInBuffer()[0], socket->getInBuffer().size());
Poco::MemoryInputStream message(socket->getInBuffer().data(), socket->getInBuffer().size());
// update the read cursor - headers are not altered by chunks.
message.seekg(startmessage.tellg(), std::ios::beg);

Expand Down
4 changes: 2 additions & 2 deletions wsd/FileServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,10 @@ void FileServerRequestHandler::readDirToHash(const std::string &basePath, const
std::string compressedFile;
const long unsigned int compSize = compressBound(size);
compressedFile.resize(compSize);
strm.next_in = (unsigned char*)&uncompressedFile[0];
strm.next_in = (unsigned char*)uncompressedFile.data();
strm.avail_in = size;
strm.avail_out = compSize;
strm.next_out = (unsigned char*)&compressedFile[0];
strm.next_out = (unsigned char*)compressedFile.data();
strm.total_out = strm.total_in = 0;

const int deflateResult = deflate(&strm, Z_FINISH);
Expand Down

0 comments on commit d8a291c

Please sign in to comment.