mirror of
https://git.proxmox.com/git/proxmox-perl-rs
synced 2025-05-29 06:31:51 +00:00
pmg: switch acme to use magic
Instead of blessed raw pointers as these can easily lead to double free corruptions when they're copied in perl. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
e9c2ba606d
commit
e3bc763de4
@ -182,7 +182,6 @@ impl Inner {
|
|||||||
#[perlmod::package(name = "PMG::RS::Acme")]
|
#[perlmod::package(name = "PMG::RS::Acme")]
|
||||||
pub mod export {
|
pub mod export {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
@ -195,54 +194,33 @@ pub mod export {
|
|||||||
|
|
||||||
use super::{AccountData, Inner};
|
use super::{AccountData, Inner};
|
||||||
|
|
||||||
const CLASSNAME: &str = "PMG::RS::Acme";
|
perlmod::declare_magic!(Box<Acme> : &Acme as "PMG::RS::Acme");
|
||||||
|
|
||||||
/// An Acme client instance.
|
/// An Acme client instance.
|
||||||
pub struct Acme {
|
pub struct Acme {
|
||||||
inner: Mutex<Inner>,
|
inner: Mutex<Inner>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a Value> for &'a Acme {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: &'a Value) -> Result<&'a Acme, Error> {
|
|
||||||
Ok(unsafe { value.from_blessed_box(CLASSNAME)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bless(class: Value, mut ptr: Box<Acme>) -> Result<Value, Error> {
|
|
||||||
let value = Value::new_pointer::<Acme>(&mut *ptr);
|
|
||||||
let value = Value::new_ref(&value);
|
|
||||||
let this = value.bless_sv(&class)?;
|
|
||||||
let _perl = Box::leak(ptr);
|
|
||||||
Ok(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new ACME client instance given an account path and an API directory URL.
|
/// Create a new ACME client instance given an account path and an API directory URL.
|
||||||
#[export(raw_return)]
|
#[export(raw_return)]
|
||||||
pub fn new(#[raw] class: Value, api_directory: String) -> Result<Value, Error> {
|
pub fn new(#[raw] class: Value, api_directory: String) -> Result<Value, Error> {
|
||||||
bless(
|
Ok(perlmod::instantiate_magic!(
|
||||||
class,
|
&class,
|
||||||
Box::new(Acme {
|
MAGIC => Box::new(Acme {
|
||||||
inner: Mutex::new(Inner::new(api_directory)?),
|
inner: Mutex::new(Inner::new(api_directory)?),
|
||||||
}),
|
})
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load an existing account.
|
/// Load an existing account.
|
||||||
#[export(raw_return)]
|
#[export(raw_return)]
|
||||||
pub fn load(#[raw] class: Value, account_path: String) -> Result<Value, Error> {
|
pub fn load(#[raw] class: Value, account_path: String) -> Result<Value, Error> {
|
||||||
bless(
|
Ok(perlmod::instantiate_magic!(
|
||||||
class,
|
&class,
|
||||||
Box::new(Acme {
|
MAGIC => Box::new(Acme {
|
||||||
inner: Mutex::new(Inner::load(account_path)?),
|
inner: Mutex::new(Inner::load(account_path)?),
|
||||||
}),
|
})
|
||||||
)
|
))
|
||||||
}
|
|
||||||
|
|
||||||
#[export(name = "DESTROY")]
|
|
||||||
fn destroy(#[raw] this: Value) {
|
|
||||||
perlmod::destructor!(this, Acme: CLASSNAME);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new account.
|
/// Create a new account.
|
||||||
|
Loading…
Reference in New Issue
Block a user