Skip to content

Mark resource-owning classes non-copyable#2667

Open
pr0toboy wants to merge 1 commit into
melonDS-emu:masterfrom
pr0toboy:claude/delete-copy-resource-classes
Open

Mark resource-owning classes non-copyable#2667
pr0toboy wants to merge 1 commit into
melonDS-emu:masterfrom
pr0toboy:claude/delete-copy-resource-classes

Conversation

@pr0toboy

Copy link
Copy Markdown

Wifi, DSi_I2CHost and the DynamicFIFO<T> template all manage heap-allocated state and have a user-defined destructor that frees it (delete[] Entries, owned DSi_BPTWL/DSi_Camera*, etc.), but none of them declares a copy constructor or a copy assignment operator. The implicit ones copy the owned pointers by value, which would lead to a double-free or a UAF as soon as one of the copies goes out of scope.

C++17's guaranteed copy elision currently hides this on the existing call sites — most notably the aggregate-init of DynamicFIFO<u8> Mailbox[9] in DSi_NWifi — so the issue is latent rather than active. The safety net is one accidental auto x = y; or pass-by-value away from biting, though.

This patch declares the copy ctor and copy assignment as = delete on each class. If a future change ever needs copy semantics, the compiler will refuse it and force the author to think about it, instead of silently producing a UAF.

Build was clean before/after, confirming no existing call site relies on the implicit copies.

Found by cppcheck (noCopyConstructor + noOperatorEq on Wifi, DSi_I2CHost, DynamicFIFO<>).

Wifi, DSi_I2CHost and DynamicFIFO all manage heap-allocated state and
have a user-defined destructor that frees it, but neither declares a
copy constructor nor a copy assignment operator. The implicitly generated
versions copy the owned pointer/buffer by value, which would lead to a
double-free as soon as one of the copies is destroyed. C++17 guaranteed
copy elision currently hides this on the existing call sites (notably
the DynamicFIFO Mailbox[9] aggregate-init in DSi_NWifi), so the bug is
latent, not active — but the safety net is one accidental `auto x = y;`
away from biting.

Declare both as = delete on each class. If a future change ever needs
copy semantics, the compiler will now refuse it and force the author
to write the copy correctly, instead of silently producing a UAF.

Build was clean before/after, confirming no existing call site relies
on the implicit copies.

Found by cppcheck (`noCopyConstructor` + `noOperatorEq` on Wifi,
DSi_I2CHost, and DynamicFIFO<>).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant