From 5d75ca0e3011c06b0f0e79f8af8b594ae7e474e4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 9 Mar 2021 23:36:35 -0500 Subject: [PATCH] Build fixes in Rust. --- core/SharedPtr.hpp | 46 ++++++++-------------- rust-zerotier-core/build.rs | 1 + rust-zerotier-core/src/certificate.rs | 16 +++++--- rust-zerotier-service/build.rs | 1 + rust-zerotier-service/src/commands/cert.rs | 2 +- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/core/SharedPtr.hpp b/core/SharedPtr.hpp index 2e65d5759..6a7fc5a99 100644 --- a/core/SharedPtr.hpp +++ b/core/SharedPtr.hpp @@ -40,21 +40,13 @@ public: {} ZT_INLINE ~SharedPtr() - { - if (likely(m_ptr != nullptr)) { - if (unlikely(const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)) - delete m_ptr; - } - } + { _release(); } ZT_INLINE SharedPtr &operator=(const SharedPtr &sp) { if (likely(m_ptr != sp.m_ptr)) { T *const p = sp._getAndInc(); - if (likely(m_ptr != nullptr)) { - if (unlikely(const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)) - delete m_ptr; - } + _release(); m_ptr = p; } return *this; @@ -62,12 +54,8 @@ public: ZT_INLINE void set(T *ptr) noexcept { - if (likely(m_ptr != nullptr)) { - if (unlikely(const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)) - delete m_ptr; - } - const_cast *>(&(ptr->__refCount))->fetch_add(1, std::memory_order_relaxed); - m_ptr = ptr; + _release(); + const_cast *>(&((m_ptr = ptr)->__refCount))->fetch_add(1, std::memory_order_relaxed); } /** @@ -92,10 +80,7 @@ public: */ ZT_INLINE void move(SharedPtr &from) { - if (m_ptr != nullptr) { - if (const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1) - delete m_ptr; - } + _release(); m_ptr = from.m_ptr; from.m_ptr = nullptr; } @@ -120,11 +105,8 @@ public: */ ZT_INLINE void zero() { - if (likely(m_ptr != nullptr)) { - if (unlikely(const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)) - delete m_ptr; - m_ptr = nullptr; - } + _release(); + m_ptr = nullptr; } /** @@ -174,16 +156,16 @@ public: { return (m_ptr != sp.m_ptr); } ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept - { return (m_ptr > sp.m_ptr); } + { return (reinterpret_cast(m_ptr) > reinterpret_cast(sp.m_ptr)); } ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept - { return (m_ptr < sp.m_ptr); } + { return (reinterpret_cast(m_ptr) < reinterpret_cast(sp.m_ptr)); } ZT_INLINE bool operator>=(const SharedPtr &sp) const noexcept - { return (m_ptr >= sp.m_ptr); } + { return (reinterpret_cast(m_ptr) >= reinterpret_cast(sp.m_ptr)); } ZT_INLINE bool operator<=(const SharedPtr &sp) const noexcept - { return (m_ptr <= sp.m_ptr); } + { return (reinterpret_cast(m_ptr) <= reinterpret_cast(sp.m_ptr)); } private: ZT_INLINE T *_getAndInc() const noexcept @@ -193,6 +175,12 @@ private: return m_ptr; } + ZT_INLINE void _release() const noexcept + { + if (unlikely((m_ptr != nullptr)&&(const_cast *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1))) + delete m_ptr; + } + T *m_ptr; }; diff --git a/rust-zerotier-core/build.rs b/rust-zerotier-core/build.rs index f5a806a82..957c1bcb3 100644 --- a/rust-zerotier-core/build.rs +++ b/rust-zerotier-core/build.rs @@ -1,4 +1,5 @@ #[allow(unused_assignments)] +#[allow(unused_mut)] fn main() { let d = env!("CARGO_MANIFEST_DIR"); println!("cargo:rustc-link-search=native={}/../build/core", d); diff --git a/rust-zerotier-core/src/certificate.rs b/rust-zerotier-core/src/certificate.rs index 17c80c643..90ec6bd43 100644 --- a/rust-zerotier-core/src/certificate.rs +++ b/rust-zerotier-core/src/certificate.rs @@ -13,7 +13,6 @@ use std::ffi::CString; use std::hash::{Hash, Hasher}; -use std::mem::zeroed; use std::os::raw::{c_char, c_uint, c_void}; use std::ptr::{copy_nonoverlapping, null, null_mut}; @@ -23,6 +22,7 @@ use serde::{Deserialize, Serialize}; use crate::*; use crate::capi as ztcore; +use std::mem::zeroed; /// Maximum length of a string in a certificate (mostly for the certificate name fields). pub const CERTIFICATE_MAX_STRING_LENGTH: isize = ztcore::ZT_CERTIFICATE_MAX_STRING_LENGTH as isize; @@ -47,12 +47,18 @@ pub struct CertificateSerialNo(pub [u8; 48]); impl CertificateSerialNo { #[inline(always)] pub fn new() -> CertificateSerialNo { CertificateSerialNo([0; 48]) } - pub fn new_from_string(s: &str) -> Result { hex::decode(s).map_or_else(|_| { Err(ResultCode::ErrorBadParameter) }, |b| { Ok(CertificateSerialNo::from(b.unwrap().as_slice())) }) } + pub fn new_from_string(s: &str) -> Result { hex::decode(s).map_or_else(|_| { Err(ResultCode::ErrorBadParameter) }, |b| { Ok(CertificateSerialNo::from(b)) }) } } -impl From<&[u8; 48]> for CertificateSerialNo { - #[inline(always)] - fn from(a: &[u8; 48]) -> CertificateSerialNo { CertificateSerialNo(*a) } +impl> From for CertificateSerialNo { + fn from(a: A) -> CertificateSerialNo { + let mut sn = CertificateSerialNo::new(); + let aa = a.as_ref(); + for i in 0..aa.len() { + sn.0[i] = aa[i]; + } + sn + } } impl Hash for CertificateSerialNo { diff --git a/rust-zerotier-service/build.rs b/rust-zerotier-service/build.rs index 7cd4e3811..d71096be8 100644 --- a/rust-zerotier-service/build.rs +++ b/rust-zerotier-service/build.rs @@ -1,4 +1,5 @@ #[allow(unused_assignments)] +#[allow(unused_mut)] fn main() { let d = env!("CARGO_MANIFEST_DIR"); println!("cargo:rustc-link-search=native={}/../build/core", d); diff --git a/rust-zerotier-service/src/commands/cert.rs b/rust-zerotier-service/src/commands/cert.rs index 134cfc844..5fc43f187 100644 --- a/rust-zerotier-service/src/commands/cert.rs +++ b/rust-zerotier-service/src/commands/cert.rs @@ -38,7 +38,7 @@ fn newsid<'a>(store: &Store, cli_args: Option<&ArgMatches<'a>>, auth_token: &Opt std::fs::write(path, sid.as_bytes()).map_or_else(|e| { eprintln!("FATAL: error writing '{}': {}", path, e.to_string()); e.raw_os_error().unwrap_or(1) - }, || { + }, |_| { 0 }) }