Skip to content

Commit 2e4a9f0

Browse files
authored
Fix C++ Rule of 5 violations in msquic.hpp to prevent memory corruption (#5128)
1 parent ff2ec6c commit 2e4a9f0

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/inc/msquic.hpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ struct CxPlatEvent {
5353
CxPlatEvent(bool ManualReset) noexcept { CxPlatEventInitialize(&Handle, ManualReset, FALSE); }
5454
CxPlatEvent(CXPLAT_EVENT event) noexcept : Handle(event) { }
5555
~CxPlatEvent() noexcept { CxPlatEventUninitialize(Handle); }
56+
CxPlatEvent(const CxPlatEvent&) = delete;
57+
CxPlatEvent& operator=(const CxPlatEvent&) = delete;
58+
CxPlatEvent(CxPlatEvent&&) = delete;
59+
CxPlatEvent& operator=(CxPlatEvent&&) = delete;
5660
CXPLAT_EVENT* operator &() noexcept { return &Handle; }
5761
operator CXPLAT_EVENT() const noexcept { return Handle; }
5862
void Set() { CxPlatEventSet(Handle); }
@@ -65,6 +69,10 @@ struct CxPlatRundown {
6569
CXPLAT_RUNDOWN_REF Ref;
6670
CxPlatRundown() noexcept { CxPlatRundownInitialize(&Ref); }
6771
~CxPlatRundown() noexcept { CxPlatRundownUninitialize(&Ref); }
72+
CxPlatRundown(const CxPlatRundown&) = delete;
73+
CxPlatRundown& operator=(const CxPlatRundown&) = delete;
74+
CxPlatRundown(CxPlatRundown&&) = delete;
75+
CxPlatRundown& operator=(CxPlatRundown&&) = delete;
6876
bool Acquire() noexcept { return CxPlatRundownAcquire(&Ref); }
6977
void Release() noexcept { CxPlatRundownRelease(&Ref); }
7078
void ReleaseAndWait() { CxPlatRundownReleaseAndWait(&Ref); }
@@ -74,6 +82,10 @@ struct CxPlatLock {
7482
CXPLAT_LOCK Handle;
7583
CxPlatLock() noexcept { CxPlatLockInitialize(&Handle); }
7684
~CxPlatLock() noexcept { CxPlatLockUninitialize(&Handle); }
85+
CxPlatLock(const CxPlatLock&) = delete;
86+
CxPlatLock& operator=(const CxPlatLock&) = delete;
87+
CxPlatLock(CxPlatLock&&) = delete;
88+
CxPlatLock& operator=(CxPlatLock&&) = delete;
7789
void Acquire() noexcept { CxPlatLockAcquire(&Handle); }
7890
void Release() noexcept { CxPlatLockRelease(&Handle); }
7991
};
@@ -84,6 +96,10 @@ struct CxPlatLockDispatch {
8496
CXPLAT_DISPATCH_LOCK Handle;
8597
CxPlatLockDispatch() noexcept { CxPlatDispatchLockInitialize(&Handle); }
8698
~CxPlatLockDispatch() noexcept { CxPlatDispatchLockUninitialize(&Handle); }
99+
CxPlatLockDispatch(const CxPlatLockDispatch&) = delete;
100+
CxPlatLockDispatch& operator=(const CxPlatLockDispatch&) = delete;
101+
CxPlatLockDispatch(CxPlatLockDispatch&&) = delete;
102+
CxPlatLockDispatch& operator=(CxPlatLockDispatch&&) = delete;
87103
void Acquire() noexcept { CxPlatDispatchLockAcquire(&Handle); }
88104
void Release() noexcept { CxPlatDispatchLockRelease(&Handle); }
89105
};
@@ -93,6 +109,10 @@ struct CxPlatPool {
93109
CXPLAT_POOL Handle;
94110
CxPlatPool(uint32_t Size, uint32_t Tag = 0, bool IsPaged = false) noexcept { CxPlatPoolInitialize(IsPaged, Size, Tag, &Handle); }
95111
~CxPlatPool() noexcept { CxPlatPoolUninitialize(&Handle); }
112+
CxPlatPool(const CxPlatPool&) = delete;
113+
CxPlatPool& operator=(const CxPlatPool&) = delete;
114+
CxPlatPool(CxPlatPool&&) = delete;
115+
CxPlatPool& operator=(CxPlatPool&&) = delete;
96116
void* Alloc() noexcept { return CxPlatPoolAlloc(&Handle); }
97117
void Free(void* Ptr) noexcept { CxPlatPoolFree(Ptr); }
98118
};
@@ -126,6 +146,10 @@ class CxPlatPoolT {
126146
public:
127147
CxPlatPoolT() noexcept { CxPlatPoolInitialize(Paged, sizeof(T), Tag, &Pool); }
128148
~CxPlatPoolT() noexcept { CxPlatPoolUninitialize(&Pool); }
149+
CxPlatPoolT(const CxPlatPoolT&) = delete;
150+
CxPlatPoolT& operator=(const CxPlatPoolT&) = delete;
151+
CxPlatPoolT(CxPlatPoolT&&) = delete;
152+
CxPlatPoolT& operator=(CxPlatPoolT&&) = delete;
129153
template <class... Args>
130154
T* Alloc(Args&&... args) noexcept {
131155
void* Raw = CxPlatPoolAlloc(&Pool);
@@ -146,6 +170,10 @@ struct CxPlatHashTable {
146170
CXPLAT_HASHTABLE Table;
147171
CxPlatHashTable() noexcept { Initialized = CxPlatHashtableInitializeEx(&Table, CXPLAT_HASH_MIN_SIZE); }
148172
~CxPlatHashTable() noexcept { if (Initialized) { CxPlatHashtableUninitialize(&Table); } }
173+
CxPlatHashTable(const CxPlatHashTable&) = delete;
174+
CxPlatHashTable& operator=(const CxPlatHashTable&) = delete;
175+
CxPlatHashTable(CxPlatHashTable&&) = delete;
176+
CxPlatHashTable& operator=(CxPlatHashTable&&) = delete;
149177
void Insert(CXPLAT_HASHTABLE_ENTRY* Entry) noexcept { CxPlatHashtableInsert(&Table, Entry, Entry->Signature, nullptr); }
150178
void Remove(CXPLAT_HASHTABLE_ENTRY* Entry) noexcept { CxPlatHashtableRemove(&Table, Entry, nullptr); }
151179
CXPLAT_HASHTABLE_ENTRY* Lookup(uint64_t Signature) noexcept {
@@ -188,6 +216,10 @@ class CxPlatThread {
188216
CxPlatThreadDelete(&Thread);
189217
}
190218
}
219+
CxPlatThread(const CxPlatThread&) = delete;
220+
CxPlatThread& operator=(const CxPlatThread&) = delete;
221+
CxPlatThread(CxPlatThread&&) = delete;
222+
CxPlatThread& operator=(CxPlatThread&&) = delete;
191223
QUIC_STATUS Create(CXPLAT_THREAD_CONFIG* Config) noexcept {
192224
auto Status = CxPlatThreadCreate(Config, &Thread);
193225
if (QUIC_SUCCEEDED(Status)) {
@@ -240,6 +272,10 @@ class CxPlatWatchdog {
240272
~CxPlatWatchdog() noexcept {
241273
ShutdownEvent.Set();
242274
}
275+
CxPlatWatchdog(const CxPlatWatchdog&) = delete;
276+
CxPlatWatchdog& operator=(const CxPlatWatchdog&) = delete;
277+
CxPlatWatchdog(CxPlatWatchdog&&) = delete;
278+
CxPlatWatchdog& operator=(CxPlatWatchdog&&) = delete;
243279
};
244280

245281
#endif // CXPLAT_FRE_ASSERT
@@ -446,6 +482,10 @@ class MsQuicApi : public QUIC_API_TABLE {
446482
memset(thisTable, 0, sizeof(*thisTable));
447483
}
448484
}
485+
MsQuicApi(const MsQuicApi&) = delete;
486+
MsQuicApi& operator=(const MsQuicApi&) = delete;
487+
MsQuicApi(MsQuicApi&&) = delete;
488+
MsQuicApi& operator=(MsQuicApi&&) = delete;
449489
QUIC_STATUS GetInitStatus() const noexcept { return InitStatus; }
450490
bool IsValid() const noexcept { return QUIC_SUCCEEDED(InitStatus); }
451491
};
@@ -473,6 +513,10 @@ struct MsQuicExecution {
473513
delete [] Configs;
474514
}
475515
}
516+
MsQuicExecution(const MsQuicExecution&) = delete;
517+
MsQuicExecution& operator=(const MsQuicExecution&) = delete;
518+
MsQuicExecution(MsQuicExecution&&) = delete;
519+
MsQuicExecution& operator=(MsQuicExecution&&) = delete;
476520
void Initialize(
477521
_In_ QUIC_GLOBAL_EXECUTION_CONFIG_FLAGS Flags, // Used for datapath type
478522
_In_ uint32_t PollingIdleTimeoutUs,
@@ -537,6 +581,8 @@ struct MsQuicRegistration {
537581
bool IsValid() const noexcept { return QUIC_SUCCEEDED(InitStatus); }
538582
MsQuicRegistration(const MsQuicRegistration& Other) = delete;
539583
MsQuicRegistration& operator=(const MsQuicRegistration& Other) = delete;
584+
MsQuicRegistration(MsQuicRegistration&& Other) = delete;
585+
MsQuicRegistration& operator=(MsQuicRegistration&& Other) = delete;
540586
void Shutdown(
541587
_In_ QUIC_CONNECTION_SHUTDOWN_FLAGS Flags,
542588
_In_ QUIC_UINT62 ErrorCode
@@ -816,6 +862,8 @@ struct MsQuicConfiguration {
816862
bool IsValid() const noexcept { return QUIC_SUCCEEDED(InitStatus); }
817863
MsQuicConfiguration(const MsQuicConfiguration& Other) = delete;
818864
MsQuicConfiguration& operator=(const MsQuicConfiguration& Other) = delete;
865+
MsQuicConfiguration(MsQuicConfiguration&& Other) = delete;
866+
MsQuicConfiguration& operator=(MsQuicConfiguration&& Other) = delete;
819867
QUIC_STATUS
820868
LoadCredential(_In_ const QUIC_CREDENTIAL_CONFIG* CredConfig) noexcept {
821869
return MsQuic->ConfigurationLoadCredential(Handle, CredConfig);
@@ -1016,6 +1064,8 @@ struct MsQuicListener {
10161064
bool IsValid() const { return QUIC_SUCCEEDED(InitStatus); }
10171065
MsQuicListener(const MsQuicListener& Other) = delete;
10181066
MsQuicListener& operator=(const MsQuicListener& Other) = delete;
1067+
MsQuicListener(MsQuicListener&& Other) = delete;
1068+
MsQuicListener& operator=(MsQuicListener&& Other) = delete;
10191069
operator HQUIC () const noexcept { return Handle; }
10201070

10211071
private:
@@ -1337,6 +1387,8 @@ struct MsQuicConnection {
13371387
bool IsValid() const { return QUIC_SUCCEEDED(InitStatus); }
13381388
MsQuicConnection(const MsQuicConnection& Other) = delete;
13391389
MsQuicConnection& operator=(const MsQuicConnection& Other) = delete;
1390+
MsQuicConnection(MsQuicConnection&& Other) = delete;
1391+
MsQuicConnection& operator=(MsQuicConnection&& Other) = delete;
13401392
operator HQUIC () const noexcept { return Handle; }
13411393

13421394
static
@@ -1723,6 +1775,8 @@ struct MsQuicStream {
17231775
bool IsValid() const { return QUIC_SUCCEEDED(InitStatus); }
17241776
MsQuicStream(const MsQuicStream& Other) = delete;
17251777
MsQuicStream& operator=(const MsQuicStream& Other) = delete;
1778+
MsQuicStream(MsQuicStream&& Other) = delete;
1779+
MsQuicStream& operator=(MsQuicStream&& Other) = delete;
17261780
operator HQUIC () const noexcept { return Handle; }
17271781

17281782
static
@@ -1765,6 +1819,10 @@ struct ConnectionScope {
17651819
ConnectionScope() noexcept : Handle(nullptr) { }
17661820
ConnectionScope(HQUIC handle) noexcept : Handle(handle) { }
17671821
~ConnectionScope() noexcept { if (Handle) { MsQuic->ConnectionClose(Handle); } }
1822+
ConnectionScope(const ConnectionScope&) = delete;
1823+
ConnectionScope& operator=(const ConnectionScope&) = delete;
1824+
ConnectionScope(ConnectionScope&&) = delete;
1825+
ConnectionScope& operator=(ConnectionScope&&) = delete;
17681826
operator HQUIC() const noexcept { return Handle; }
17691827
};
17701828

@@ -1775,6 +1833,10 @@ struct StreamScope {
17751833
StreamScope() noexcept : Handle(nullptr) { }
17761834
StreamScope(HQUIC handle) noexcept : Handle(handle) { }
17771835
~StreamScope() noexcept { if (Handle) { MsQuic->StreamClose(Handle); } }
1836+
StreamScope(const StreamScope&) = delete;
1837+
StreamScope& operator=(const StreamScope&) = delete;
1838+
StreamScope(StreamScope&&) = delete;
1839+
StreamScope& operator=(StreamScope&&) = delete;
17781840
operator HQUIC() const noexcept { return Handle; }
17791841
};
17801842

@@ -1785,6 +1847,10 @@ struct ConfigurationScope {
17851847
ConfigurationScope() noexcept : Handle(nullptr) { }
17861848
ConfigurationScope(HQUIC handle) noexcept : Handle(handle) { }
17871849
~ConfigurationScope() noexcept { if (Handle) { MsQuic->ConfigurationClose(Handle); } }
1850+
ConfigurationScope(const ConfigurationScope&) = delete;
1851+
ConfigurationScope& operator=(const ConfigurationScope&) = delete;
1852+
ConfigurationScope(ConfigurationScope&&) = delete;
1853+
ConfigurationScope& operator=(ConfigurationScope&&) = delete;
17881854
operator HQUIC() const noexcept { return Handle; }
17891855
};
17901856

@@ -1799,8 +1865,12 @@ struct QuicBufferScope {
17991865
Buffer->Length = Size;
18001866
Buffer->Buffer = (uint8_t*)(Buffer + 1);
18011867
}
1802-
operator QUIC_BUFFER* () noexcept { return Buffer; }
18031868
~QuicBufferScope() noexcept { if (Buffer) { delete[](uint8_t*) Buffer; } }
1869+
QuicBufferScope(const QuicBufferScope&) = delete;
1870+
QuicBufferScope& operator=(const QuicBufferScope&) = delete;
1871+
QuicBufferScope(QuicBufferScope&&) = delete;
1872+
QuicBufferScope& operator=(QuicBufferScope&&) = delete;
1873+
operator QUIC_BUFFER* () noexcept { return Buffer; }
18041874
};
18051875

18061876
static_assert(sizeof(QuicBufferScope) == sizeof(QUIC_BUFFER*), "Scope guards should be the same size as the guarded type");

0 commit comments

Comments
 (0)