Skip to content

Commit dadcb3e

Browse files
committed
refactor: changes to support edition = "2024"
enforced by workspace level lints, now specified in each Cargo.toml, except nginx-sys, because bindgen can't be set to Edition2024 until MSRV hits 1.85 this change set is created automatically by `cargo fix --edition --all`, then with manual fixes afterwards: * Back out any use of unsafe(no_mangle) for plain no_mangle, since 1.81 wont accept it * remove uses of expr_2021 in macros, which is not available in the MSRV. * Manual fix to ngx_container_of! macro for safety rules in 2024, 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 07ce1f7 commit dadcb3e

29 files changed

+554
-411
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ homepage = "https://github.com/nginx/ngx-rust"
1212
repository = "https://github.com/nginx/ngx-rust"
1313
rust-version = "1.81.0"
1414

15+
[workspace.lints.rust]
16+
rust-2024-compatibility = "warn"
17+
edition-2024-expr-fragment-specifier = "allow"
18+
1519
[package]
1620
name = "ngx"
1721
version = "0.5.0-beta"
@@ -70,3 +74,6 @@ maintenance = { status = "experimental" }
7074

7175
[dev-dependencies]
7276
tempfile = { version = "3.20.0", default-features = false }
77+
78+
[lints]
79+
workspace = true

examples/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ default = ["export-modules", "ngx/vendored"]
6565
# See https://github.com/rust-lang/rust/issues/20267
6666
export-modules = []
6767
linux = []
68+
69+
[lints]
70+
workspace = true

examples/async.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ impl http::HttpModule for Module {
2424
}
2525

2626
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
27-
// SAFETY: this function is called with non-NULL cf always
28-
let cf = &mut *cf;
29-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
30-
31-
let h = ngx_array_push(
32-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
33-
) as *mut ngx_http_handler_pt;
34-
if h.is_null() {
35-
return core::Status::NGX_ERROR.into();
27+
unsafe {
28+
// SAFETY: this function is called with non-NULL cf always
29+
let cf = &mut *cf;
30+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
31+
32+
let h = ngx_array_push(
33+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
34+
) as *mut ngx_http_handler_pt;
35+
if h.is_null() {
36+
return core::Status::NGX_ERROR.into();
37+
}
38+
// set an Access phase handler
39+
*h = Some(async_access_handler);
40+
core::Status::NGX_OK.into()
3641
}
37-
// set an Access phase handler
38-
*h = Some(async_access_handler);
39-
core::Status::NGX_OK.into()
4042
}
4143
}
4244

@@ -98,16 +100,16 @@ impl http::Merge for ModuleConfig {
98100

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

103-
if (*ctx).done.load(Ordering::Relaxed) {
105+
if unsafe { (*ctx).done.load(Ordering::Relaxed) } {
104106
// Triggering async_access_handler again
105-
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
107+
unsafe { ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events)) };
106108
} else {
107109
// this doesn't have have good performance but works as a simple thread-safe example and
108110
// doesn't causes segfault. The best method that provides both thread-safety and
109111
// performance requires an nginx patch.
110-
ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
112+
unsafe { ngx_post_event(event, addr_of_mut!(ngx_posted_next_events)) };
111113
}
112114
}
113115

examples/awssig.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Allow following allow to work in MSRV 1.81
2+
#![allow(unknown_lints)]
3+
// Suppress spurrions lint for 2024 compatibility
4+
#![allow(tail_expr_drop_order)]
5+
16
use std::ffi::{c_char, c_void};
27

38
use http::HeaderMap;
@@ -19,19 +24,21 @@ impl HttpModule for Module {
1924
}
2025

2126
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
22-
// SAFETY: this function is called with non-NULL cf always
23-
let cf = &mut *cf;
24-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
25-
26-
let h = ngx_array_push(
27-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
28-
) as *mut ngx_http_handler_pt;
29-
if h.is_null() {
30-
return core::Status::NGX_ERROR.into();
27+
unsafe {
28+
// SAFETY: this function is called with non-NULL cf always
29+
let cf = &mut *cf;
30+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
31+
32+
let h = ngx_array_push(
33+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
34+
) as *mut ngx_http_handler_pt;
35+
if h.is_null() {
36+
return core::Status::NGX_ERROR.into();
37+
}
38+
// set an phase handler
39+
*h = Some(awssigv4_header_handler);
40+
core::Status::NGX_OK.into()
3141
}
32-
// set an phase handler
33-
*h = Some(awssigv4_header_handler);
34-
core::Status::NGX_OK.into()
3542
}
3643
}
3744

@@ -298,10 +305,13 @@ http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
298305
for (name, value) in request.headers_in_iterator() {
299306
if let Ok(name) = name.to_str() {
300307
if name.to_lowercase() == "host" {
301-
if let Ok(value) = http::HeaderValue::from_bytes(value.as_bytes()) {
302-
headers.insert(http::header::HOST, value);
303-
} else {
304-
return core::Status::NGX_DECLINED;
308+
match http::HeaderValue::from_bytes(value.as_bytes()) {
309+
Ok(value) => {
310+
headers.insert(http::header::HOST, value);
311+
}
312+
_ => {
313+
return core::Status::NGX_DECLINED;
314+
}
305315
}
306316
}
307317
} else {

examples/curl.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ impl http::HttpModule for Module {
1818
}
1919

2020
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
21-
// SAFETY: this function is called with non-NULL cf always
22-
let cf = &mut *cf;
23-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
24-
25-
let h = ngx_array_push(
26-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
27-
) as *mut ngx_http_handler_pt;
28-
if h.is_null() {
29-
return core::Status::NGX_ERROR.into();
21+
unsafe {
22+
// SAFETY: this function is called with non-NULL cf always
23+
let cf = &mut *cf;
24+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
25+
26+
let h = ngx_array_push(
27+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
28+
) as *mut ngx_http_handler_pt;
29+
if h.is_null() {
30+
return core::Status::NGX_ERROR.into();
31+
}
32+
// set an Access phase handler
33+
*h = Some(curl_access_handler);
34+
core::Status::NGX_OK.into()
3035
}
31-
// set an Access phase handler
32-
*h = Some(curl_access_handler);
33-
core::Status::NGX_OK.into()
3436
}
3537
}
3638

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

@@ -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;
@@ -152,15 +158,17 @@ unsafe fn ngx_get_origdst(
152158
}
153159
}
154160

155-
let mut addr: sockaddr_storage = { std::mem::zeroed() };
161+
let mut addr: sockaddr_storage = unsafe { 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
}

0 commit comments

Comments
 (0)