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

fix custom_key_cmp_wrapper being able to unwind to C code (ub) #275

Merged
merged 2 commits into from
Aug 18, 2024

Conversation

antonilol
Copy link
Contributor

custom_key_cmp_wrapper may not panic because it is called from C code that does not understand rust panics. If it does, that is undefined behavior.

custom_key_cmp_wrapper will now abort if the Comparator implementation panics. I also removed the safety comments on compare and compare_elem (the safety comments on the safe trait is what got me to discover this UB)

Review note:

match ord {
    Ordering::Less => -1,
    Ordering::Equal => 0,
    Ordering::Greater => 1,
}

is equivalent to ord as i32

Copy link
Member

@Kerollmops Kerollmops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much @antonilol to make the crate much more safe than it is already!

BTW what do you use heed for? 😊

@@ -371,16 +373,15 @@ impl Drop for EnvInner {

/// A helper function that transforms the LMDB types into Rust types (`MDB_val` into slices)
/// and vice versa, the Rust types into C types (`Ordering` into an integer).
extern "C" fn custom_key_cmp_wrapper<C: Comparator>(
unsafe extern "C" fn custom_key_cmp_wrapper<C: Comparator>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you added unsafe to this function? Isn't it safe now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still dereferences raw pointers. Should I add a /// # Safety doc comment with some explanation? It is a private function though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ho! Yeah! You're right. It's a good idea to add a safety note.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the safety note

and removed safety comments on the safe traits Comparator and LexicographicComparator
@antonilol antonilol force-pushed the fix-ub-unwinding-comparator branch from 2559961 to 40e3bf8 Compare August 18, 2024 10:44
@antonilol
Copy link
Contributor Author

BTW what do you use heed for? 😊

I am currently experimenting with changing the database backend of electrs, it currently uses RocksDB but that takes so long to compile.

Copy link
Member

@Kerollmops Kerollmops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the fixes! Merging 🙏

@Kerollmops Kerollmops merged commit 4a01acb into meilisearch:main Aug 18, 2024
8 checks passed
@antonilol antonilol deleted the fix-ub-unwinding-comparator branch August 18, 2024 11:44
@Kerollmops
Copy link
Member

I am currently experimenting with changing the database backend of electrs, it currently uses RocksDB but that takes so long to compile.

Nice, @antonilol 👌 So, you switch not for the LMDB performances or functionalities but more to reduce compilation times?

@antonilol
Copy link
Contributor Author

Nice, @antonilol 👌 So, you switch not for the LMDB performances or functionalities but more to reduce compilation times?

Mainly compile time, but the memory mapped nature of LMDB should also speed up runtime by a bit. Important functionality is about the same, talking about get, put, delete, and iterators.

maxdeviant referenced this pull request in zed-industries/zed Aug 19, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [heed](https://togithub.com/Kerollmops/heed) | workspace.dependencies
| patch | `0.20.4` -> `0.20.5` |

---

### Release Notes

<details>
<summary>Kerollmops/heed (heed)</summary>

###
[`v0.20.5`](https://togithub.com/meilisearch/heed/releases/tag/v0.20.5):
🛁

[Compare
Source](https://togithub.com/Kerollmops/heed/compare/v0.20.4...v0.20.5)

<p align="center"><img width="280px"
src="https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png"></a></p>
<h1 align="center" >heed</h1>

##### What's Changed
* fix function docs (clippy warnings) by
@&#8203;antonil[https://github.com/meilisearch/heed/pull/273](https://togithub.com/meilisearch/heed/pull/273)ll/273
* fix custom_key_cmp_wrapper being able to unwind to C code (ub) by
@&#8203;antonil[https://github.com/meilisearch/heed/pull/275](https://togithub.com/meilisearch/heed/pull/275)ll/275

##### New Contributors
* @&#8203;antonilol made their first
contributi[https://github.com/meilisearch/heed/pull/273](https://togithub.com/meilisearch/heed/pull/273)ll/273

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 3pm on Wednesday" in timezone
America/New_York, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

Release Notes:

- N/A

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

2 participants