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

ConcurrentReadableArray: Use std::uninitialized_copy_n() instead of std::copy() to avoid calling destructors on uninitialized memory #40521

Merged
merged 1 commit into from
Dec 13, 2021

Conversation

grynspan
Copy link
Contributor

ConcurrentReadableArray: Use std::uninitialized_copy_n() instead of std::copy() to avoid calling destructors on uninitialized memory.

This change fixes a (currently benign) misuse of std::copy() in ConcurrentReadableArray when it is resized. std::copy() will call destructors on the destination buffer before copying in new values. Because the memory being copied into is uninitialized, this is undefined behavior. There shouldn't be any impact in the real world though because the only users of ConcurrentReadableArray are POD types so there's nothing to destruct.

I also took the opportunity to switch from memcpy() to std::uninitialized_copy_n() for ConcurrentReadableHashMap which allows us to remove the std::is_trivially_copyable constraint on ElemTy. For POD types, the resulting generated code should be identical.

@grynspan grynspan added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Dec 11, 2021
@grynspan
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - be81c721caeaa571736b379fced70bbf5ab874be

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - be81c721caeaa571736b379fced70bbf5ab874be

@grynspan
Copy link
Contributor Author

Build failure appears unrelated to my changes, caused by swiftlang/swift-package-manager#3929. @tomerd

…td::copy() to avoid calling destructors on uninitialized memory
@grynspan grynspan force-pushed the jgrynspan/ConcurrentReadableArray-copy-bug branch from be81c72 to c9f8136 Compare December 11, 2021 18:26
@grynspan
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c9f8136

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c9f8136

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan merged commit cd6755b into main Dec 13, 2021
@grynspan grynspan deleted the jgrynspan/ConcurrentReadableArray-copy-bug branch December 13, 2021 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants