From: Robert Obkircher <r.obkircher@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH v1 proxmox] router: compare the Fn trait object instead of the ApiHandler address
Date: Fri, 14 Aug 2026 14:40:48 +0200 [thread overview]
Message-ID: <20260814124206.309661-1-r.obkircher@proxmox.com> (raw)
The previous implementation was effectively `ptr::eq(self, rhs)`,
because it compared the addresses of the fields instead of the trait
object fat pointers.
This worked anyway, because the tests only compare const values that
the compiler manages to deduplicate.
I discovered this problem because those handler tests fail under miri.
However, this change does not fix that, because miri assigns a unique
address every time a reference to a function is created, meaning even
`ptr::eq(&some_fn, &some_fn)` evaluates to false.
Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
---
proxmox-router/src/router.rs | 56 ++++++++++++++++--------------------
1 file changed, 25 insertions(+), 31 deletions(-)
diff --git a/proxmox-router/src/router.rs b/proxmox-router/src/router.rs
index fea47ba6..a92c527d 100644
--- a/proxmox-router/src/router.rs
+++ b/proxmox-router/src/router.rs
@@ -514,42 +514,36 @@ impl Eq for ApiHandler {}
#[cfg(feature = "test-harness")]
impl PartialEq for ApiHandler {
fn eq(&self, rhs: &Self) -> bool {
- unsafe {
- #[allow(clippy::missing_transmute_annotations)]
- match (self, rhs) {
- (ApiHandler::Sync(l), ApiHandler::Sync(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- (ApiHandler::SerializingSync(l), ApiHandler::SerializingSync(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- (ApiHandler::StreamSync(l), ApiHandler::StreamSync(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- (ApiHandler::Async(l), ApiHandler::Async(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- (ApiHandler::SerializingAsync(l), ApiHandler::SerializingAsync(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- (ApiHandler::StreamAsync(l), ApiHandler::StreamAsync(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- #[cfg(feature = "server")]
- (ApiHandler::AsyncHttp(l), ApiHandler::AsyncHttp(r)) => {
- core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r)
- }
- #[cfg(feature = "server")]
- (
- ApiHandler::AsyncHttpBodyParameters(l),
- ApiHandler::AsyncHttpBodyParameters(r),
- ) => core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r),
- _ => false,
+ // This can result in false positives and false negatives (see ptr::fn_addr_eq).
+ use core::ptr::eq;
+ match (self, rhs) {
+ (ApiHandler::Sync(l), ApiHandler::Sync(r)) => eq(*l, *r),
+ (ApiHandler::SerializingSync(l), ApiHandler::SerializingSync(r)) => eq(*l, *r),
+ (ApiHandler::StreamSync(l), ApiHandler::StreamSync(r)) => eq(*l, *r),
+ (ApiHandler::Async(l), ApiHandler::Async(r)) => eq(*l, *r),
+ (ApiHandler::SerializingAsync(l), ApiHandler::SerializingAsync(r)) => eq(*l, *r),
+ (ApiHandler::StreamAsync(l), ApiHandler::StreamAsync(r)) => eq(*l, *r),
+ #[cfg(feature = "server")]
+ (ApiHandler::AsyncHttp(l), ApiHandler::AsyncHttp(r)) => eq(*l, *r),
+ #[cfg(feature = "server")]
+ (ApiHandler::AsyncHttpBodyParameters(l), ApiHandler::AsyncHttpBodyParameters(r)) => {
+ eq(*l, *r)
}
+ _ => false,
}
}
}
+#[test]
+#[cfg(feature = "test-harness")]
+fn test_handler_eq() {
+ static DUMMY: ApiHandlerFn = &|_, _, _| unimplemented!();
+ static H1: ApiHandler = ApiHandler::Sync(DUMMY);
+ static H2: ApiHandler = ApiHandler::Sync(DUMMY);
+ assert!(!core::ptr::eq(&H1, &H2)); // statics have unique addresses
+ assert!(H1 == H2); // this used to fail because it compared &&dyn instead of &dyn
+}
+
/// Lookup table to child `Router`s
///
/// Stores a sorted list of `(name, router)` tuples:
--
2.47.3
reply other threads:[~2026-08-14 12:42 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260814124206.309661-1-r.obkircher@proxmox.com \
--to=r.obkircher@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.