Skip to content

Issue 32 lint unsafe #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "ngx"
version = "0.3.0-beta"
version = "0.4.0-beta"
edition = "2021"
autoexamples = false
categories = ["api-bindings", "network-programming"]
Expand All @@ -19,7 +19,7 @@ keywords = ["nginx", "module", "sys"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nginx-sys = { path = "nginx-sys", version = "0.1"}
nginx-sys = { path = "nginx-sys", version = "0.2"}

[badges]
maintenance = { status = "experimental" }
2 changes: 1 addition & 1 deletion nginx-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nginx-sys"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
categories = ["external-ffi-bindings"]
description = "FFI bindings to NGINX"
Expand Down
31 changes: 22 additions & 9 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ pub use bindings::*;
/// let data: &str = "example"; // The string to convert
/// let ptr = str_to_uchar(pool, data);
/// ```
pub fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
let ptr: *mut u_char = unsafe { ngx_palloc(pool, data.len() as _) as _ };
unsafe {
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
}
pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
let ptr: *mut u_char = ngx_palloc(pool, data.len() as _) as _;
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
ptr
}

Expand All @@ -99,9 +97,14 @@ impl ngx_str_t {
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
/// * `data` - The `String` from which to create the nginx string.
///
/// # Safety
/// This function is marked as unsafe because it accepts a raw pointer argument. There is no
/// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
/// avoid indeterminate behavior.
///
/// # Returns
/// An `ngx_str_t` instance representing the given `String`.
pub fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
pub unsafe fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
ngx_str_t {
data: str_to_uchar(pool, data.as_str()),
len: data.len() as _,
Expand All @@ -115,9 +118,14 @@ impl ngx_str_t {
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
/// * `data` - The string slice from which to create the nginx string.
///
/// # Safety
/// This function is marked as unsafe because it accepts a raw pointer argument. There is no
/// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
/// avoid indeterminate behavior.
///
/// # Returns
/// An `ngx_str_t` instance representing the given string slice.
pub fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
pub unsafe fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
ngx_str_t {
data: str_to_uchar(pool, data),
len: data.len() as _,
Expand Down Expand Up @@ -180,11 +188,16 @@ impl TryFrom<ngx_str_t> for &str {
/// let value: &str = "value"; // The value to add
/// let result = add_to_ngx_table(table, pool, key, value);
/// ```
pub fn add_to_ngx_table(table: *mut ngx_table_elt_t, pool: *mut ngx_pool_t, key: &str, value: &str) -> Option<()> {
pub unsafe fn add_to_ngx_table(
table: *mut ngx_table_elt_t,
pool: *mut ngx_pool_t,
key: &str,
value: &str,
) -> Option<()> {
if table.is_null() {
return None;
}
unsafe { table.as_mut() }.map(|table| {
table.as_mut().map(|table| {
table.hash = 1;
table.key.len = key.len() as _;
table.key.data = str_to_uchar(pool, key);
Expand Down
8 changes: 4 additions & 4 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ impl Request {
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
add_to_ngx_table(table, self.0.pool, key, value)
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
}

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

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

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