Skip to content

Commit 1b88323

Browse files
Matthew Yacobucciivanitskiy
Matthew Yacobucci
authored andcommitted
feat: unsafe updates for raw pointer arguments
Dereferencing raw pointers is inherently unsafe. To satisfy clippy these functions and their callers require an unsafe indicator.
1 parent a1e55cd commit 1b88323

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

Diff for: Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66

77
[package]
88
name = "ngx"
9-
version = "0.3.0-beta"
9+
version = "0.4.0-beta"
1010
edition = "2021"
1111
autoexamples = false
1212
categories = ["api-bindings", "network-programming"]

Diff for: nginx-sys/src/lib.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ pub use bindings::*;
6868
/// let data: &str = "example"; // The string to convert
6969
/// let ptr = str_to_uchar(pool, data);
7070
/// ```
71-
pub fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
72-
let ptr: *mut u_char = unsafe { ngx_palloc(pool, data.len() as _) as _ };
73-
unsafe {
74-
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
75-
}
71+
pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
72+
let ptr: *mut u_char = ngx_palloc(pool, data.len() as _) as _;
73+
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
7674
ptr
7775
}
7876

@@ -99,9 +97,14 @@ impl ngx_str_t {
9997
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
10098
/// * `data` - The `String` from which to create the nginx string.
10199
///
100+
/// # Safety
101+
/// This function is marked as unsafe because it accepts a raw pointer argument. There is no
102+
/// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
103+
/// avoid indeterminate behavior.
104+
///
102105
/// # Returns
103106
/// An `ngx_str_t` instance representing the given `String`.
104-
pub fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
107+
pub unsafe fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
105108
ngx_str_t {
106109
data: str_to_uchar(pool, data.as_str()),
107110
len: data.len() as _,
@@ -115,9 +118,14 @@ impl ngx_str_t {
115118
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
116119
/// * `data` - The string slice from which to create the nginx string.
117120
///
121+
/// # Safety
122+
/// This function is marked as unsafe because it accepts a raw pointer argument. There is no
123+
/// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
124+
/// avoid indeterminate behavior.
125+
///
118126
/// # Returns
119127
/// An `ngx_str_t` instance representing the given string slice.
120-
pub fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
128+
pub unsafe fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
121129
ngx_str_t {
122130
data: str_to_uchar(pool, data),
123131
len: data.len() as _,
@@ -180,11 +188,16 @@ impl TryFrom<ngx_str_t> for &str {
180188
/// let value: &str = "value"; // The value to add
181189
/// let result = add_to_ngx_table(table, pool, key, value);
182190
/// ```
183-
pub fn add_to_ngx_table(table: *mut ngx_table_elt_t, pool: *mut ngx_pool_t, key: &str, value: &str) -> Option<()> {
191+
pub unsafe fn add_to_ngx_table(
192+
table: *mut ngx_table_elt_t,
193+
pool: *mut ngx_pool_t,
194+
key: &str,
195+
value: &str,
196+
) -> Option<()> {
184197
if table.is_null() {
185198
return None;
186199
}
187-
unsafe { table.as_mut() }.map(|table| {
200+
table.as_mut().map(|table| {
188201
table.hash = 1;
189202
table.key.len = key.len() as _;
190203
table.key.data = str_to_uchar(pool, key);

Diff for: src/http/request.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ impl Request {
199199
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
200200
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
201201
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
202-
add_to_ngx_table(table, self.0.pool, key, value)
202+
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
203203
}
204204

205205
/// Add header to the `headers_out` object.
206206
///
207207
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
208208
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
209209
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
210-
add_to_ngx_table(table, self.0.pool, key, value)
210+
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
211211
}
212212

213213
/// Set response body [Content-Length].
@@ -259,7 +259,7 @@ impl Request {
259259
/// Perform internal redirect to a location
260260
pub fn internal_redirect(&self, location: &str) -> Status {
261261
assert!(!location.is_empty(), "uri location is empty");
262-
let uri_ptr = &mut ngx_str_t::from_str(self.0.pool, location) as *mut _;
262+
let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, location) as *mut _ };
263263

264264
// FIXME: check status of ngx_http_named_location or ngx_http_internal_redirect
265265
if location.starts_with('@') {
@@ -285,7 +285,7 @@ impl Request {
285285
module: &ngx_module_t,
286286
post_callback: unsafe extern "C" fn(*mut ngx_http_request_t, *mut c_void, ngx_int_t) -> ngx_int_t,
287287
) -> Status {
288-
let uri_ptr = &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _;
288+
let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ };
289289
// -------------
290290
// allocate memory and set values for ngx_http_post_subrequest_t
291291
let sub_ptr = self.pool().alloc(std::mem::size_of::<ngx_http_post_subrequest_t>());

0 commit comments

Comments
 (0)