Skip to content

Commit 633f51a

Browse files
committed
feat: set workspace edition = "2024" rust-version = "1.85.0"
rust 1.85 is the MSRV for edition 2024. cargo fmt --all reformats many files for 2024. Manual fix to ngx_container_of! macro for safety rules in 2024, as well as various conditional and in-macro uses of no_mangle - these weren't migrated automatically by `cargo fix` Manual fixes to several other macros that created an &mut Request in an expression, Rust 2024 is stricter about taking &mut of temporaries, so instead the request is let-bound first.
1 parent ebe9172 commit 633f51a

File tree

19 files changed

+142
-133
lines changed

19 files changed

+142
-133
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ members = [
66
]
77

88
[workspace.package]
9-
edition = "2021"
9+
edition = "2024"
1010
license = "Apache-2.0"
1111
homepage = "https://github.com/nginx/ngx-rust"
1212
repository = "https://github.com/nginx/ngx-rust"
13-
rust-version = "1.81.0"
13+
rust-version = "1.85.0"
1414

1515
[package]
1616
name = "ngx"

examples/async.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ngx::ngx_modules!(ngx_http_async_module);
8181

8282
#[used]
8383
#[allow(non_upper_case_globals)]
84-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
84+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
8585
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
8686
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
8787
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
@@ -100,16 +100,16 @@ impl http::Merge for ModuleConfig {
100100

101101
unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
102102
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
103-
let c: *mut ngx_connection_t = (*event).data.cast();
103+
let c: *mut ngx_connection_t = unsafe { (*event).data.cast() };
104104

105-
if (*ctx).done.load(Ordering::Relaxed) {
105+
if unsafe { (*ctx).done.load(Ordering::Relaxed) } {
106106
// Triggering async_access_handler again
107-
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
107+
unsafe { ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events)) };
108108
} else {
109109
// this doesn't have have good performance but works as a simple thread-safe example and
110110
// doesn't causes segfault. The best method that provides both thread-safety and
111111
// performance requires an nginx patch.
112-
ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
112+
unsafe { ngx_post_event(event, addr_of_mut!(ngx_posted_next_events)) };
113113
}
114114
}
115115

examples/awssig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ngx::ngx_modules!(ngx_http_awssigv4_module);
112112

113113
#[used]
114114
#[allow(non_upper_case_globals)]
115-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
115+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
116116
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
117117
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
118118
commands: unsafe { &NGX_HTTP_AWSSIGV4_COMMANDS[0] as *const _ as *mut _ },

examples/curl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ ngx::ngx_modules!(ngx_http_curl_module);
7575

7676
#[used]
7777
#[allow(non_upper_case_globals)]
78-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
78+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
7979
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
8080
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
8181
commands: unsafe { &NGX_HTTP_CURL_COMMANDS[0] as *const _ as *mut _ },

examples/httporigdst.rs

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::ffi::{c_int, c_void};
2-
use std::ptr::addr_of;
2+
use std::ptr::{addr_of, NonNull};
33

44
use ngx::core;
55
use ngx::ffi::{
@@ -47,29 +47,33 @@ impl NgxHttpOrigDstCtx {
4747
}
4848

4949
pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) {
50+
let mut v = NonNull::new(v).unwrap();
51+
let v = unsafe { v.as_mut() };
5052
if self.orig_dst_addr.len == 0 {
51-
(*v).set_not_found(1);
53+
v.set_not_found(1);
5254
return;
5355
}
5456

55-
(*v).set_valid(1);
56-
(*v).set_no_cacheable(0);
57-
(*v).set_not_found(0);
58-
(*v).set_len(self.orig_dst_addr.len as u32);
59-
(*v).data = self.orig_dst_addr.data;
57+
v.set_valid(1);
58+
v.set_no_cacheable(0);
59+
v.set_not_found(0);
60+
v.set_len(self.orig_dst_addr.len as u32);
61+
v.data = self.orig_dst_addr.data;
6062
}
6163

6264
pub unsafe fn bind_port(&self, v: *mut ngx_variable_value_t) {
65+
let mut v = NonNull::new(v).unwrap();
66+
let v = unsafe { v.as_mut() };
6367
if self.orig_dst_port.len == 0 {
64-
(*v).set_not_found(1);
68+
v.set_not_found(1);
6569
return;
6670
}
6771

68-
(*v).set_valid(1);
69-
(*v).set_no_cacheable(0);
70-
(*v).set_not_found(0);
71-
(*v).set_len(self.orig_dst_port.len as u32);
72-
(*v).data = self.orig_dst_port.data;
72+
v.set_valid(1);
73+
v.set_no_cacheable(0);
74+
v.set_not_found(0);
75+
v.set_len(self.orig_dst_port.len as u32);
76+
v.data = self.orig_dst_port.data;
7377
}
7478
}
7579

@@ -91,7 +95,7 @@ ngx::ngx_modules!(ngx_http_orig_dst_module);
9195

9296
#[used]
9397
#[allow(non_upper_case_globals)]
94-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
98+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
9599
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
96100
ctx: std::ptr::addr_of!(NGX_HTTP_ORIG_DST_MODULE_CTX) as _,
97101
commands: std::ptr::null_mut(),
@@ -129,19 +133,21 @@ unsafe fn ngx_get_origdst(
129133
) -> Result<(String, in_port_t), core::Status> {
130134
let c = request.connection();
131135

132-
if (*c).type_ != libc::SOCK_STREAM {
136+
if unsafe { (*c).type_ } != libc::SOCK_STREAM {
133137
ngx_log_debug_http!(request, "httporigdst: connection is not type SOCK_STREAM");
134138
return Err(core::Status::NGX_DECLINED);
135139
}
136140

137-
if ngx_connection_local_sockaddr(c, std::ptr::null_mut(), 0) != core::Status::NGX_OK.into() {
141+
if unsafe { ngx_connection_local_sockaddr(c, std::ptr::null_mut(), 0) }
142+
!= core::Status::NGX_OK.into()
143+
{
138144
ngx_log_debug_http!(request, "httporigdst: no local sockaddr from connection");
139145
return Err(core::Status::NGX_ERROR);
140146
}
141147

142148
let level: c_int;
143149
let optname: c_int;
144-
match (*(*c).local_sockaddr).sa_family as i32 {
150+
match unsafe { (*(*c).local_sockaddr).sa_family } as i32 {
145151
libc::AF_INET => {
146152
level = libc::SOL_IP;
147153
optname = libc::SO_ORIGINAL_DST;
@@ -154,13 +160,15 @@ unsafe fn ngx_get_origdst(
154160

155161
let mut addr: sockaddr_storage = { std::mem::zeroed() };
156162
let mut addrlen: libc::socklen_t = std::mem::size_of_val(&addr) as libc::socklen_t;
157-
let rc = libc::getsockopt(
158-
(*c).fd,
159-
level,
160-
optname,
161-
&mut addr as *mut _ as *mut _,
162-
&mut addrlen as *mut u32,
163-
);
163+
let rc = unsafe {
164+
libc::getsockopt(
165+
(*c).fd,
166+
level,
167+
optname,
168+
&mut addr as *mut _ as *mut _,
169+
&mut addrlen as *mut u32,
170+
)
171+
};
164172
if rc == -1 {
165173
ngx_log_debug_http!(request, "httporigdst: getsockopt failed");
166174
return Err(core::Status::NGX_DECLINED);
@@ -192,10 +200,11 @@ unsafe fn ngx_get_origdst(
192200
http_variable_get!(
193201
ngx_http_orig_dst_addr_variable,
194202
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
195-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
203+
let ctx = request
204+
.get_module_ctx::<NgxHttpOrigDstCtx>(unsafe { &*addr_of!(ngx_http_orig_dst_module) });
196205
if let Some(obj) = ctx {
197206
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
198-
obj.bind_addr(v);
207+
unsafe { obj.bind_addr(v) };
199208
return core::Status::NGX_OK;
200209
}
201210
// lazy initialization:
@@ -204,7 +213,7 @@ http_variable_get!(
204213
// set context
205214
// bind address
206215
ngx_log_debug_http!(request, "httporigdst: context not found, getting address");
207-
let r = ngx_get_origdst(request);
216+
let r = unsafe { ngx_get_origdst(request) };
208217
match r {
209218
Err(e) => {
210219
return e;
@@ -226,10 +235,11 @@ http_variable_get!(
226235
ip,
227236
port,
228237
);
229-
(*new_ctx).save(&ip, port, &request.pool());
230-
(*new_ctx).bind_addr(v);
231-
request
232-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
238+
unsafe { (*new_ctx).save(&ip, port, &request.pool()) };
239+
unsafe { (*new_ctx).bind_addr(v) };
240+
request.set_module_ctx(new_ctx as *mut c_void, unsafe {
241+
&*addr_of!(ngx_http_orig_dst_module)
242+
});
233243
}
234244
}
235245
core::Status::NGX_OK
@@ -239,10 +249,11 @@ http_variable_get!(
239249
http_variable_get!(
240250
ngx_http_orig_dst_port_variable,
241251
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
242-
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
252+
let ctx = request
253+
.get_module_ctx::<NgxHttpOrigDstCtx>(unsafe { &*addr_of!(ngx_http_orig_dst_module) });
243254
if let Some(obj) = ctx {
244255
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
245-
obj.bind_port(v);
256+
unsafe { obj.bind_port(v) };
246257
return core::Status::NGX_OK;
247258
}
248259
// lazy initialization:
@@ -251,7 +262,7 @@ http_variable_get!(
251262
// set context
252263
// bind port
253264
ngx_log_debug_http!(request, "httporigdst: context not found, getting address");
254-
let r = ngx_get_origdst(request);
265+
let r = unsafe { ngx_get_origdst(request) };
255266
match r {
256267
Err(e) => {
257268
return e;
@@ -273,10 +284,11 @@ http_variable_get!(
273284
ip,
274285
port,
275286
);
276-
(*new_ctx).save(&ip, port, &request.pool());
277-
(*new_ctx).bind_port(v);
278-
request
279-
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
287+
unsafe { (*new_ctx).save(&ip, port, &request.pool()) };
288+
unsafe { (*new_ctx).bind_port(v) };
289+
request.set_module_ctx(new_ctx as *mut c_void, unsafe {
290+
&*addr_of!(ngx_http_orig_dst_module)
291+
});
280292
}
281293
}
282294
core::Status::NGX_OK
@@ -292,13 +304,15 @@ impl HttpModule for Module {
292304

293305
// static ngx_int_t ngx_http_orig_dst_add_variables(ngx_conf_t *cf)
294306
unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
295-
for mut v in NGX_HTTP_ORIG_DST_VARS {
296-
let var = ngx_http_add_variable(cf, &mut v.name, v.flags);
297-
if var.is_null() {
307+
for mut v in unsafe { NGX_HTTP_ORIG_DST_VARS } {
308+
let var = NonNull::new(unsafe { ngx_http_add_variable(cf, &mut v.name, v.flags) });
309+
if var.is_none() {
298310
return core::Status::NGX_ERROR.into();
299311
}
300-
(*var).get_handler = v.get_handler;
301-
(*var).data = v.data;
312+
let mut var = var.unwrap();
313+
let var = unsafe { var.as_mut() };
314+
var.get_handler = v.get_handler;
315+
var.data = v.data;
302316
}
303317
core::Status::NGX_OK.into()
304318
}

examples/shared_dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ngx::ngx_modules!(ngx_http_shared_dict_module);
8989

9090
#[used]
9191
#[allow(non_upper_case_globals)]
92-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
92+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
9393
pub static mut ngx_http_shared_dict_module: ngx_module_t = ngx_module_t {
9494
ctx: ptr::addr_of!(NGX_HTTP_SHARED_DICT_MODULE_CTX) as _,
9595
commands: unsafe { ptr::addr_of_mut!(NGX_HTTP_SHARED_DICT_COMMANDS[0]) },

0 commit comments

Comments
 (0)