Skip to content

Commit 1694d69

Browse files
Merge branch 'main' into concatenate-namespace-declarations
2 parents e5b887d + 009a71f commit 1694d69

43 files changed

Lines changed: 1515 additions & 162 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Crypto/src/PKCS12Container.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ std::string PKCS12Container::extractFriendlyName(X509* pCert)
127127
if(pCert)
128128
{
129129
int length = 0;
130-
char* pBuffer = reinterpret_cast<char*>(X509_alias_get0(pCert, &length));
130+
auto pBuffer = reinterpret_cast<const char*>(X509_alias_get0(pCert, &length));
131131
if (pBuffer)
132132
{
133133
friendlyName.append(pBuffer, length);

Crypto/src/X509Certificate.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void X509Certificate::save(const std::string& path) const
208208
}
209209

210210

211-
std::string _X509_NAME_oneline_utf8(X509_NAME *name)
211+
std::string _X509_NAME_oneline_utf8(const X509_NAME *name)
212212
{
213213
BIO * bio_out = BIO_new(BIO_s_mem());
214214
X509_NAME_print_ex(bio_out, name, 0, (ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_FN_SN | XN_FLAG_DUMP_UNKNOWN_FIELDS) & ~ASN1_STRFLGS_ESC_MSB);
@@ -246,7 +246,7 @@ std::string X509Certificate::commonName() const
246246

247247
std::string X509Certificate::issuerName(NID nid) const
248248
{
249-
if (X509_NAME* issuer = X509_get_issuer_name(_pCert))
249+
if (auto issuer = X509_get_issuer_name(_pCert))
250250
{
251251
char buffer[NAME_BUFFER_SIZE];
252252
if (X509_NAME_get_text_by_NID(issuer, nid, buffer, sizeof(buffer)) >= 0)
@@ -258,7 +258,7 @@ std::string X509Certificate::issuerName(NID nid) const
258258

259259
std::string X509Certificate::subjectName(NID nid) const
260260
{
261-
if (X509_NAME* subj = X509_get_subject_name(_pCert))
261+
if (auto subj = X509_get_subject_name(_pCert))
262262
{
263263
char buffer[NAME_BUFFER_SIZE];
264264
if (X509_NAME_get_text_by_NID(subj, nid, buffer, sizeof(buffer)) >= 0)
@@ -296,13 +296,14 @@ void X509Certificate::extractNames(std::string& cmnName, std::set<std::string>&
296296
Poco::DateTime X509Certificate::validFrom() const
297297
{
298298
const ASN1_TIME* certTime = X509_get0_notBefore(_pCert);
299-
std::string dateTime(reinterpret_cast<char*>(certTime->data));
299+
auto certTimeType = ASN1_STRING_type(certTime);
300+
std::string dateTime(reinterpret_cast<const char*>(ASN1_STRING_get0_data(certTime)), ASN1_STRING_length(certTime));
300301
int tzd;
301-
if (certTime->type == V_ASN1_UTCTIME)
302+
if (certTimeType == V_ASN1_UTCTIME)
302303
{
303304
return DateTimeParser::parse("%y%m%d%H%M%S", dateTime, tzd);
304305
}
305-
else if (certTime->type == V_ASN1_GENERALIZEDTIME)
306+
else if (certTimeType == V_ASN1_GENERALIZEDTIME)
306307
{
307308
return DateTimeParser::parse("%Y%m%d%H%M%S", dateTime, tzd);
308309
}
@@ -316,13 +317,14 @@ Poco::DateTime X509Certificate::validFrom() const
316317
Poco::DateTime X509Certificate::expiresOn() const
317318
{
318319
const ASN1_TIME* certTime = X509_get0_notAfter(_pCert);
319-
std::string dateTime(reinterpret_cast<char*>(certTime->data));
320+
auto certTimeType = ASN1_STRING_type(certTime);
321+
std::string dateTime(reinterpret_cast<const char*>(ASN1_STRING_get0_data(certTime)), ASN1_STRING_length(certTime));
320322
int tzd;
321-
if (certTime->type == V_ASN1_UTCTIME)
323+
if (certTimeType == V_ASN1_UTCTIME)
322324
{
323325
return DateTimeParser::parse("%y%m%d%H%M%S", dateTime, tzd);
324326
}
325-
else if (certTime->type == V_ASN1_GENERALIZEDTIME)
327+
else if (certTimeType == V_ASN1_GENERALIZEDTIME)
326328
{
327329
return DateTimeParser::parse("%Y%m%d%H%M%S", dateTime, tzd);
328330
}

MongoDB/include/Poco/MongoDB/BSONReader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "Poco/MongoDB/MongoDB.h"
2222
#include "Poco/BinaryReader.h"
23+
#include "Poco/Exception.h"
2324

2425

2526
namespace Poco::MongoDB {
@@ -71,6 +72,8 @@ inline std::string BSONReader::readCString()
7172
{
7273
if (c == 0x00) return val;
7374
else val += c;
75+
if (val.size() > static_cast<std::size_t>(BSON_MAX_DOCUMENT_SIZE))
76+
throw Poco::DataFormatException("BSON cstring exceeds maximum size");
7477
}
7578
}
7679
return val;

MongoDB/include/Poco/MongoDB/Binary.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,19 @@ inline void BSONReader::read<Binary::Ptr>(Binary::Ptr& to)
153153
Poco::Int32 size;
154154
_reader >> size;
155155

156+
if (size < 0)
157+
throw Poco::DataFormatException("Invalid BSON binary size: " + std::to_string(size));
158+
if (size > BSON_MAX_DOCUMENT_SIZE)
159+
throw Poco::DataFormatException("BSON binary size exceeds maximum: " + std::to_string(size));
160+
156161
to->buffer().resize(size);
157162

158163
unsigned char subtype;
159164
_reader >> subtype;
160165
to->subtype(subtype);
161166

162-
_reader.readRaw(reinterpret_cast<char*>(to->buffer().begin()), size);
167+
if (size > 0)
168+
_reader.readRaw(reinterpret_cast<char*>(to->buffer().begin()), size);
163169
}
164170

165171

MongoDB/include/Poco/MongoDB/Connection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ class MongoDB_API Connection
135135
void connect(const Poco::Net::SocketAddress& addrs);
136136
/// Connects to the given MongoDB server.
137137

138+
void connect(const Poco::Net::SocketAddress& addrs, const Poco::Timespan& connectTimeout, const Poco::Timespan& socketTimeout = 0);
139+
/// Connects to the given MongoDB server with the specified connect timeout.
140+
/// If connectTimeout is non-zero, the connection attempt will be aborted
141+
/// after the specified time.
142+
/// If socketTimeout is non-zero, the send and receive timeouts on the socket
143+
/// will be set after a successful connection.
144+
138145
void connect(const Poco::Net::StreamSocket& socket);
139146
/// Connects using an already connected socket.
140147

MongoDB/include/Poco/MongoDB/Element.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Poco/BinaryReader.h"
2222
#include "Poco/BinaryWriter.h"
2323
#include "Poco/DateTimeFormatter.h"
24+
#include "Poco/Exception.h"
2425
#include "Poco/MongoDB/BSONReader.h"
2526
#include "Poco/MongoDB/BSONWriter.h"
2627
#include "Poco/MongoDB/MongoDB.h"
@@ -187,6 +188,10 @@ inline void BSONReader::read<std::string>(std::string& to)
187188
{
188189
Poco::Int32 size;
189190
_reader >> size;
191+
if (size < BSON_MIN_STRING_SIZE)
192+
throw Poco::DataFormatException("Invalid BSON string size: " + std::to_string(size));
193+
if (size > BSON_MAX_DOCUMENT_SIZE)
194+
throw Poco::DataFormatException("BSON string size exceeds maximum: " + std::to_string(size));
190195
_reader.readRaw(size, to);
191196
to.erase(to.end() - 1); // remove terminating 0
192197
}

MongoDB/include/Poco/MongoDB/MessageHeader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ inline Int32 MessageHeader::getMessageLength() const
110110

111111
inline void MessageHeader::setMessageLength(Int32 length)
112112
{
113-
poco_assert (_messageLength >= 0);
113+
poco_assert (length >= 0);
114114
_messageLength = MSG_HEADER_SIZE + length;
115115
}
116116

MongoDB/include/Poco/MongoDB/MongoDB.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@
5151
#endif
5252

5353

54+
// MongoDB wire protocol and BSON specification limits.
55+
// These constants are defined centrally so that all validation
56+
// code in the library uses the same values.
57+
58+
namespace Poco {
59+
namespace MongoDB {
60+
61+
/// Maximum BSON document size (16 MB) per MongoDB specification.
62+
/// Applies to documents, strings, binary data, and cstrings.
63+
static constexpr Poco::Int32 BSON_MAX_DOCUMENT_SIZE = 16 * 1024 * 1024;
64+
65+
/// Minimum BSON document size (5 bytes): 4-byte size field + 1-byte null terminator.
66+
static constexpr Poco::Int32 BSON_MIN_DOCUMENT_SIZE = 5;
67+
68+
/// Minimum BSON string size (1 byte for the null terminator).
69+
static constexpr Poco::Int32 BSON_MIN_STRING_SIZE = 1;
70+
71+
/// Maximum OP_MSG message size (48 MB) per MongoDB specification.
72+
static constexpr Poco::Int32 OP_MSG_MAX_SIZE = 48 * 1024 * 1024;
73+
74+
/// Default local threshold for "nearest" read preference (15 ms = 15000 µs).
75+
/// Servers within this threshold of the minimum RTT are eligible for selection.
76+
static constexpr Poco::Int64 DEFAULT_LOCAL_THRESHOLD_US = 15000;
77+
78+
} } // namespace Poco::MongoDB
79+
80+
5481
//
5582
// Automatically link MongoDB library.
5683
//

MongoDB/include/Poco/MongoDB/ObjectId.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ class MongoDB_API ObjectId
9191
//
9292
inline Timestamp ObjectId::timestamp() const noexcept
9393
{
94-
int time;
95-
char* T = reinterpret_cast<char*>(&time);
96-
T[0] = _id[3];
97-
T[1] = _id[2];
98-
T[2] = _id[1];
99-
T[3] = _id[0];
94+
const Poco::Int32 time =
95+
(static_cast<Poco::Int32>(_id[0]) << 24) |
96+
(static_cast<Poco::Int32>(_id[1]) << 16) |
97+
(static_cast<Poco::Int32>(_id[2]) << 8) |
98+
static_cast<Poco::Int32>(_id[3]);
10099
return Timestamp::fromEpochTime(static_cast<time_t>(time));
101100
}
102101

MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "Poco/MongoDB/Connection.h"
2222
#include "Poco/ObjectPool.h"
23+
#include "Poco/Timespan.h"
2324

2425

2526
namespace Poco {
@@ -32,6 +33,9 @@ class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
3233
///
3334
/// If a Connection::SocketFactory is given, it must live for the entire
3435
/// lifetime of the PoolableObjectFactory.
36+
///
37+
/// It is strongly recommended to use one of the timeout-aware constructors
38+
/// to avoid indefinite hangs when the server is unreachable.
3539
{
3640
public:
3741
PoolableObjectFactory(Net::SocketAddress& address):
@@ -46,6 +50,22 @@ class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
4650
{
4751
}
4852

53+
PoolableObjectFactory(Net::SocketAddress& address, Poco::Timespan connectTimeout, Poco::Timespan socketTimeout = 0):
54+
_address(address),
55+
_pSocketFactory(nullptr),
56+
_connectTimeout(connectTimeout),
57+
_socketTimeout(socketTimeout)
58+
{
59+
}
60+
61+
PoolableObjectFactory(const std::string& address, Poco::Timespan connectTimeout, Poco::Timespan socketTimeout = 0):
62+
_address(address),
63+
_pSocketFactory(nullptr),
64+
_connectTimeout(connectTimeout),
65+
_socketTimeout(socketTimeout)
66+
{
67+
}
68+
4969
PoolableObjectFactory(const std::string& uri, MongoDB::Connection::SocketFactory& socketFactory):
5070
_uri(uri),
5171
_pSocketFactory(&socketFactory)
@@ -55,9 +75,15 @@ class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
5575
MongoDB::Connection::Ptr createObject()
5676
{
5777
if (_pSocketFactory)
78+
{
5879
return new MongoDB::Connection(_uri, *_pSocketFactory);
80+
}
5981
else
60-
return new MongoDB::Connection(_address);
82+
{
83+
MongoDB::Connection::Ptr conn = new MongoDB::Connection();
84+
conn->connect(_address, _connectTimeout, _socketTimeout);
85+
return conn;
86+
}
6187
}
6288

6389
bool validateObject(MongoDB::Connection::Ptr pObject)
@@ -80,7 +106,9 @@ class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
80106
private:
81107
Net::SocketAddress _address;
82108
std::string _uri;
83-
MongoDB::Connection::SocketFactory* _pSocketFactory;
109+
MongoDB::Connection::SocketFactory* _pSocketFactory = nullptr;
110+
Poco::Timespan _connectTimeout;
111+
Poco::Timespan _socketTimeout;
84112
};
85113

86114

0 commit comments

Comments
 (0)