-
Notifications
You must be signed in to change notification settings - Fork 16
fix: remove unsafe code #64
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
Conversation
|
But overall the unsafe was used correctly. It would panic in tests if used incorrectly. Why are you against it? Any reason for that? |
|
I think the question should be the other way around: Why use Sure maybe tests exercise the code enough today (I've not checked) but that code will be changed as time goes on, someone will add some subtle modification... this is the entire reason Rust exists :) |
bdae480 to
0027dcd
Compare
According to your line of thought: it won't be tested and it would be better to panic there in release. Not a lot better outcome tbh.
Not for me. We all have different reasons I guess.
Although true, not a performance bound peace of code. That's just how I code. |
Generally it is considered significantly better as it doesn't involve UB. |
|
My opinion is that it's just a less optimal code (not important but anyway), and it would be better to just cover it with tests.
Considering you have not even checked for tests, I guess you have made it in principle because you think that Rust should not use unsafe 🤷♂️ (which is a huge misbelief).
UB is usually just a crash without any info. But panic contains info. That's all the difference. And we don't want any crash to happen. Your solution does not protect the program from crash (because it still can panic). For that we need tests. And that unsafe will panic in tests. TLDR: I'm just saying that this PR does not add anything useful, just regression in performance. Because by definition, that code should not crash (UB) or panic. And making it panic instead of crash (when it should not either) is a little bit useless. Just needs more tests. |
Of course not, it wouldn't even be possible to write useful Rust programs without
UB is UB, literally anything can happen, including arbitrary code being run if things go wrong enough. Panic is a well-defined sequence of events, and when occuring on a separate thread - such as a worker thread of a networking application - it can also be handled, the application may perform recovery or it may decide to shut down, but still be able to perform cleanups, cache flushes etc. |
|
Overall, I don't mean to argue the merits of Please let me know what the project intends to go for here, so that I can make the appropriate decision regaring usage of this library. @RoDmitry are you the leader of the project and is this a decision towards keeping the unsafe? If that is the case feel free to close the PR. Thank you. |
That's wrong. The general principle is that
No. That's an attempt to make you understand that this PR literally does not change anything. It can be merged, if it makes you feel better. But it does not solve anything. Here is just two lines of unsafe with no UB. And I guess you become sick from too much unsafe when you see C/C++ code 😅. |
|
@RoDmitry I have written a fair amount of C/C++ code in my career and I've been fine, thank you for the concern. I disagree tests provide better guarantee, as a lack of Looking into the generated asm of |
The overall preference of the imaginary safety over performance makes me sad.
But ideally they would also be optimized, which obviously you disagree.
Theoretically in the future someone will add some subtle modification... - that's your take, but aren't tests supposed to protect a code from wrong modifications? Why do you disagree about tests? Tell me more about it, I want to understand it better. And if you give me an example of such a subtle modification which can't be seen in tests, I will not ask any more questions. |
There is no such tradeoff going on here - there is no real performance to be gained by removing the bounds check. It's like two instructions in a function that doesn't even need optimising much in the first place. It's the performance gain that is imaginary here, not the safety. And even if there was a perf gain, it would still be a valid question whether it's good enough to sacrifice Rust safety.
Any example I can think of can be in principle tested by tests. The problem is someone has to write those tests correctly and reviewers need to correctly verify that all possible states are tested etc. In real life this tends to fail. CVE databases are full of memory access errors including from software that is very well tested. This is the reason Rust generally attempts to minimise the space for UB by proof. This function on its own at this moment is probably fine. But if you feel the urge to even unwrap an |
That was unnecessary, I agree 😅.
Maybe you can write tests then? It would add more protection from panics in release. |
Yes, that would be swell 🙏 |
0027dcd to
1e25015
Compare
1e25015 to
1a36e9c
Compare
|
Omg, I have not published a review, and you did not see my comments 🤦♂️. Still getting used to a github review process. |
|
Thanks for review/merging. I hope it doesn't go too badly against your expectations. It makes my work easier 🙏 |
|
Not a performance bound peace of code, so it's fine. I think there are other people who don't like unsafe, so they would be more calm using this library. Thank you. |
|
@vojtechkral hey, I've got a question: why hadn't you just use: if you was looking for an extra safety? |
Change Summary
Going through the new code, found out
unsafeis being used directly 😬 This is a proposal to remove theusafeusage in order to provide higher security and fault-tolerance guarantees. I don't believe any performance is meaningfully lost here. I believe the difference amounts to at most one bounds check in a non-hot path.PR Checklist
^ I will be sure to sign the CLA if the PR gets reviewed and will be on track for merging...