Skip to content

Commit c92cb8e

Browse files
authored
Modify ensure_end_with_zero to use CString. (#80)
1 parent dfe1aaa commit c92cb8e

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

phper/src/exceptions.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
// See the Mulan PSL v2 for more details.
1010

11+
//! Apis relate to [crate::sys::zend_throw_exception].
12+
1113
use crate::{classes::ClassEntry, errors::Throwable, sys::*};
1214
use derive_more::Constructor;
1315

phper/src/functions.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
use phper_alloc::ToRefOwned;
2727
use std::{
2828
convert::TryInto,
29+
ffi::{CStr, CString},
2930
marker::PhantomData,
3031
mem::{size_of, transmute, zeroed},
3132
os::raw::c_char,
@@ -131,7 +132,7 @@ impl FunctionEntry {
131132

132133
/// Will leak memory
133134
unsafe fn entry(
134-
name: &str, arguments: &[Argument], handler: Rc<dyn Callable>,
135+
name: &CStr, arguments: &[Argument], handler: Rc<dyn Callable>,
135136
visibility: Option<Visibility>, r#static: Option<bool>,
136137
) -> zend_function_entry {
137138
let mut infos = Vec::new();
@@ -173,74 +174,79 @@ impl FunctionEntry {
173174
}
174175

175176
pub struct FunctionEntity {
176-
name: String,
177+
name: CString,
177178
handler: Rc<dyn Callable>,
178179
arguments: Vec<Argument>,
179180
}
180181

181182
impl FunctionEntity {
183+
#[inline]
182184
pub(crate) fn new(name: impl Into<String>, handler: Rc<dyn Callable>) -> Self {
183-
let name = ensure_end_with_zero(name);
184185
FunctionEntity {
185-
name,
186+
name: ensure_end_with_zero(name),
186187
handler,
187188
arguments: Default::default(),
188189
}
189190
}
190191

192+
#[inline]
191193
pub fn argument(&mut self, argument: Argument) -> &mut Self {
192194
self.arguments.push(argument);
193195
self
194196
}
195197

198+
#[inline]
196199
pub fn arguments(&mut self, arguments: impl IntoIterator<Item = Argument>) -> &mut Self {
197200
self.arguments.extend(arguments);
198201
self
199202
}
200203
}
201204

202205
pub struct MethodEntity {
203-
name: String,
206+
name: CString,
204207
handler: Rc<dyn Callable>,
205208
arguments: Vec<Argument>,
206209
visibility: Visibility,
207210
r#static: bool,
208211
}
209212

210213
impl MethodEntity {
214+
#[inline]
211215
pub(crate) fn new(
212216
name: impl Into<String>, handler: Rc<dyn Callable>, visibility: Visibility,
213217
) -> Self {
214-
let name = ensure_end_with_zero(name);
215218
Self {
216-
name,
219+
name: ensure_end_with_zero(name),
217220
handler,
218221
visibility,
219222
arguments: Default::default(),
220223
r#static: false,
221224
}
222225
}
223226

227+
#[inline]
224228
pub(crate) fn r#static(&mut self, s: bool) -> &mut Self {
225229
self.r#static = s;
226230
self
227231
}
228232

233+
#[inline]
229234
pub fn argument(&mut self, argument: Argument) -> &mut Self {
230235
self.arguments.push(argument);
231236
self
232237
}
233238

239+
#[inline]
234240
pub fn arguments(&mut self, arguments: impl IntoIterator<Item = Argument>) -> &mut Self {
235241
self.arguments.extend(arguments);
236242
self
237243
}
238244
}
239245

240246
pub struct Argument {
241-
pub(crate) name: String,
242-
pub(crate) pass_by_ref: bool,
243-
pub(crate) required: bool,
247+
name: CString,
248+
pass_by_ref: bool,
249+
required: bool,
244250
}
245251

246252
impl Argument {

phper/src/modules.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::{
2222
values::ZVal,
2323
};
2424
use std::{
25+
ffi::CString,
2526
mem::{replace, size_of, take, zeroed},
2627
os::raw::{c_int, c_uchar, c_uint, c_ushort},
2728
ptr::{null, null_mut},
@@ -91,10 +92,10 @@ unsafe extern "C" fn request_shutdown(r#type: c_int, request_number: c_int) -> c
9192
unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {
9293
read_global_module(|module| {
9394
php_info_print_table_start();
94-
if !module.version.is_empty() {
95+
if !module.version.as_bytes().is_empty() {
9596
php_info_print_table_row(2, c_str_ptr!("version"), module.version.as_ptr());
9697
}
97-
if !module.author.is_empty() {
98+
if !module.author.as_bytes().is_empty() {
9899
php_info_print_table_row(2, c_str_ptr!("authors"), module.author.as_ptr());
99100
}
100101
php_info_print_table_end();
@@ -103,9 +104,9 @@ unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {
103104
}
104105

105106
pub struct Module {
106-
name: String,
107-
version: String,
108-
author: String,
107+
name: CString,
108+
version: CString,
109+
author: CString,
109110
module_init: Option<Box<dyn FnOnce(ModuleContext) -> bool + Send + Sync>>,
110111
module_shutdown: Option<Box<dyn FnOnce(ModuleContext) -> bool + Send + Sync>>,
111112
request_init: Option<Box<dyn Fn(ModuleContext) -> bool + Send + Sync>>,
@@ -188,8 +189,11 @@ impl Module {
188189
/// Leak memory to generate `zend_module_entry` pointer.
189190
#[doc(hidden)]
190191
pub unsafe fn module_entry(self) -> *const zend_module_entry {
191-
assert!(!self.name.is_empty(), "module name must be set");
192-
assert!(!self.version.is_empty(), "module version must be set");
192+
assert!(!self.name.as_bytes().is_empty(), "module name must be set");
193+
assert!(
194+
!self.version.as_bytes().is_empty(),
195+
"module version must be set"
196+
);
193197

194198
let entry: Box<zend_module_entry> = Box::new(zend_module_entry {
195199
size: size_of::<zend_module_entry>() as c_ushort,

phper/src/output.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Logs and echo facilities.
1212
1313
use crate::{sys::*, utils::ensure_end_with_zero};
14-
use std::{convert::TryInto, ptr::null};
14+
use std::ptr::null;
1515

1616
#[repr(u32)]
1717
#[derive(Copy, Clone, PartialEq, Eq)]
@@ -40,7 +40,7 @@ pub fn echo(message: impl Into<String>) {
4040
unsafe {
4141
zend_write.expect("function zend_write can't be null")(
4242
message.as_ptr().cast(),
43-
(message.len() - 1).try_into().unwrap(),
43+
message.as_bytes().len().try_into().unwrap(),
4444
);
4545
}
4646
}

phper/src/utils.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
//! Internal useful utils.
1212
13-
pub(crate) fn ensure_end_with_zero(s: impl Into<String>) -> String {
14-
let mut s = s.into();
15-
if !s.ends_with('\0') {
16-
s.push('\0');
17-
}
18-
s
13+
use std::ffi::CString;
14+
15+
pub(crate) fn ensure_end_with_zero(s: impl Into<String>) -> CString {
16+
CString::new(s.into()).expect("CString::new failed")
1917
}

0 commit comments

Comments
 (0)