Skip to content
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

project-wide tidying, style updates #432

Merged
merged 21 commits into from
Jun 5, 2024
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
8 changes: 6 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Check clippy
- name: Check clippy (default features)
# We allow unknown lints here because sometimes the nightly job
# (below) will have a new lint that we want to suppress.
# If we suppress (e.g. #![allow(clippy::arc_with_non_send_sync)]),
Expand All @@ -213,8 +213,12 @@ jobs:
uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- name: Check clippy
- name: Check clippy (default features)
run: cargo clippy --locked --workspace --all-targets -- -D warnings
- name: Check clippy (all features)
# We only test --all-features on nightly, because two of the features
# (read_buf, core_io_borrowed_buf) require nightly.
run: cargo clippy --locked --workspace --all-targets --all-features -- -D warnings

clang-tidy:
name: Clang Tidy
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ format:
-name '*.[c|h]' \
! -wholename 'src/rustls.h' | \
xargs clang-format -i
sed -i -e 's/ffi_panic_boundary! {/if true {/g' src/*.rs
cargo fmt
sed -i -e 's/if true {/ffi_panic_boundary! {/g' src/*.rs

format-check:
find src tests \
-name '*.[c|h]' \
! -wholename 'src/rustls.h' | \
xargs clang-format --dry-run -Werror -i
sed -i -e 's/ffi_panic_boundary! {/if true {/g' src/*.rs
cargo fmt --check
sed -i -e 's/if true {/ffi_panic_boundary! {/g' src/*.rs

.PHONY: all clean test integration format format-check
77 changes: 37 additions & 40 deletions src/acceptor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use std::sync::Arc;

use libc::{c_void, size_t, EINVAL, EIO};
use rustls::server::{Accepted, AcceptedAlert, Acceptor};
use rustls::ServerConfig;

use crate::connection::rustls_connection;
use crate::error::{map_error, rustls_io_result};
use crate::io::{
rustls_read_callback, rustls_write_callback, CallbackReader, CallbackWriter, ReadCallback,
};
use crate::io::{rustls_read_callback, rustls_write_callback, CallbackReader, CallbackWriter};
use crate::rslice::{rustls_slice_bytes, rustls_str};
use crate::server::rustls_server_config;
use crate::{
Expand Down Expand Up @@ -110,11 +105,11 @@ impl rustls_acceptor {
out_n: *mut size_t,
) -> rustls_io_result {
ffi_panic_boundary! {
let acceptor: &mut Acceptor = try_mut_from_ptr!(acceptor);
let acceptor = try_mut_from_ptr!(acceptor);
if out_n.is_null() {
return rustls_io_result(EINVAL);
}
let callback: ReadCallback = try_callback!(callback);
let callback = try_callback!(callback);

let mut reader = CallbackReader { callback, userdata };

Expand Down Expand Up @@ -175,7 +170,7 @@ impl rustls_acceptor {
out_alert: *mut *mut rustls_accepted_alert,
) -> rustls_result {
ffi_panic_boundary! {
let acceptor: &mut Acceptor = try_mut_from_ptr!(acceptor);
let acceptor = try_mut_from_ptr!(acceptor);
let out_accepted = try_mut_from_ptr_ptr!(out_accepted);
let out_alert = try_mut_from_ptr_ptr!(out_alert);
match acceptor.accept() {
Expand All @@ -187,7 +182,7 @@ impl rustls_acceptor {
Err((e, accepted_alert)) => {
set_boxed_mut_ptr(out_alert, accepted_alert);
map_error(e)
},
}
}
}
}
Expand Down Expand Up @@ -219,7 +214,7 @@ impl rustls_accepted {
accepted: *const rustls_accepted,
) -> rustls_str<'static> {
ffi_panic_boundary! {
let accepted: &Option<Accepted> = try_ref_from_ptr!(accepted);
let accepted = try_ref_from_ptr!(accepted);
let accepted = match accepted {
Some(a) => a,
None => return Default::default(),
Expand Down Expand Up @@ -260,7 +255,7 @@ impl rustls_accepted {
i: usize,
) -> u16 {
ffi_panic_boundary! {
let accepted: &Option<Accepted> = try_ref_from_ptr!(accepted);
let accepted = try_ref_from_ptr!(accepted);
let accepted = match accepted {
Some(a) => a,
None => return 0,
Expand Down Expand Up @@ -298,7 +293,7 @@ impl rustls_accepted {
i: usize,
) -> u16 {
ffi_panic_boundary! {
let accepted: &Option<Accepted> = try_ref_from_ptr!(accepted);
let accepted = try_ref_from_ptr!(accepted);
let accepted = match accepted {
Some(a) => a,
None => return 0,
Expand Down Expand Up @@ -341,7 +336,7 @@ impl rustls_accepted {
i: usize,
) -> rustls_slice_bytes<'static> {
ffi_panic_boundary! {
let accepted: &Option<Accepted> = try_ref_from_ptr!(accepted);
let accepted = try_ref_from_ptr!(accepted);
let accepted = match accepted {
Some(a) => a,
None => return Default::default(),
Expand Down Expand Up @@ -408,9 +403,9 @@ impl rustls_accepted {
out_alert: *mut *mut rustls_accepted_alert,
) -> rustls_result {
ffi_panic_boundary! {
let accepted: &mut Option<Accepted> = try_mut_from_ptr!(accepted);
let accepted = try_mut_from_ptr!(accepted);
let accepted = try_take!(accepted);
let config: Arc<ServerConfig> = try_clone_arc!(config);
let config = try_clone_arc!(config);
let out_conn = try_mut_from_ptr_ptr!(out_conn);
let out_alert = try_mut_from_ptr_ptr!(out_alert);
match accepted.into_connection(config) {
Expand All @@ -422,7 +417,7 @@ impl rustls_accepted {
Err((e, accepted_alert)) => {
set_boxed_mut_ptr(out_alert, accepted_alert);
map_error(e)
},
}
}
}
}
Expand Down Expand Up @@ -469,9 +464,12 @@ impl rustls_accepted_alert {
if out_n.is_null() {
return rustls_io_result(EINVAL);
}
let accepted_alert: &mut AcceptedAlert = try_mut_from_ptr!(accepted_alert);
let mut writer = CallbackWriter { callback: try_callback!(callback), userdata };
let n_written: usize = match accepted_alert.write(&mut writer) {
let accepted_alert = try_mut_from_ptr!(accepted_alert);
let mut writer = CallbackWriter {
callback: try_callback!(callback),
userdata,
};
let n_written = match accepted_alert.write(&mut writer) {
Ok(n) => n,
Err(e) => return rustls_io_result(e.raw_os_error().unwrap_or(EIO)),
};
Expand Down Expand Up @@ -517,7 +515,7 @@ mod tests {

#[test]
fn test_acceptor_new_and_free() {
let acceptor: *mut rustls_acceptor = rustls_acceptor::rustls_acceptor_new();
let acceptor = rustls_acceptor::rustls_acceptor_new();
rustls_acceptor::rustls_acceptor_free(acceptor);
}

Expand All @@ -533,7 +531,7 @@ mod tests {
) -> rustls_io_result {
let vecdeq: *mut VecDeque<u8> = userdata as *mut _;
(*vecdeq).make_contiguous();
let first: &[u8] = (*vecdeq).as_slices().0;
let first = (*vecdeq).as_slices().0;
let n = min(n, first.len());
std::ptr::copy_nonoverlapping(first.as_ptr(), buf, n);
(*vecdeq).drain(0..n).count();
Expand All @@ -560,9 +558,9 @@ mod tests {
fn test_acceptor_corrupt_message() {
let acceptor = make_acceptor();

let mut accepted: *mut rustls_accepted = null_mut();
let mut accepted_alert: *mut rustls_accepted_alert = null_mut();
let mut n: usize = 0;
let mut accepted = null_mut();
let mut accepted_alert = null_mut();
let mut n = 0_usize;
let mut data = VecDeque::new();
for _ in 0..1024 {
data.push_back(0u8);
Expand Down Expand Up @@ -620,7 +618,7 @@ mod tests {
type conn = rustls_connection;
let builder = ccb::rustls_client_config_builder_new();
let protocols: Vec<Vec<u8>> = vec!["zarp".into(), "yuun".into()];
let mut protocols_slices: Vec<rustls_slice_bytes> = vec![];
let mut protocols_slices = vec![];
for p in &protocols {
protocols_slices.push(p.as_slice().into());
}
Expand All @@ -631,7 +629,7 @@ mod tests {
);

let config = ccb::rustls_client_config_builder_build(builder);
let mut client_conn: *mut conn = null_mut();
let mut client_conn = null_mut();
let result = rustls_client_config::rustls_client_connection_new(
config,
"example.com\0".as_ptr() as *const c_char,
Expand All @@ -641,7 +639,7 @@ mod tests {
assert_ne!(client_conn, null_mut());

let mut buf = VecDeque::<u8>::new();
let mut n: usize = 0;
let mut n = 0_usize;
conn::rustls_connection_write_tls(
client_conn,
Some(vecdeque_write),
Expand All @@ -655,11 +653,10 @@ mod tests {
}

fn make_server_config() -> *const rustls_server_config {
let builder: *mut rustls_server_config_builder =
rustls_server_config_builder::rustls_server_config_builder_new();
let builder = rustls_server_config_builder::rustls_server_config_builder_new();
let cert_pem = include_str!("../testdata/example.com/cert.pem").as_bytes();
let key_pem = include_str!("../testdata/example.com/key.pem").as_bytes();
let mut certified_key: *const rustls_certified_key = null();
let mut certified_key = null();
let result = rustls_certified_key::rustls_certified_key_build(
cert_pem.as_ptr(),
cert_pem.len(),
Expand Down Expand Up @@ -687,9 +684,9 @@ mod tests {
fn test_acceptor_success() {
let acceptor = make_acceptor();

let mut accepted: *mut rustls_accepted = null_mut();
let mut accepted_alert: *mut rustls_accepted_alert = null_mut();
let mut n: usize = 0;
let mut accepted = null_mut();
let mut accepted_alert = null_mut();
let mut n = 0;
let mut data = client_hello_bytes();
let data_len = data.len();

Expand All @@ -710,11 +707,11 @@ mod tests {
assert_eq!(accepted_alert, null_mut());

let sni = rustls_accepted::rustls_accepted_server_name(accepted);
let sni_as_slice = unsafe { std::slice::from_raw_parts(sni.data as *const u8, sni.len) };
let sni_as_slice = unsafe { slice::from_raw_parts(sni.data as *const u8, sni.len) };
let sni_as_str = std::str::from_utf8(sni_as_slice).unwrap_or("%!(ERROR)");
assert_eq!(sni_as_str, "example.com");

let mut signature_schemes: Vec<u16> = vec![];
let mut signature_schemes = vec![];
for i in 0.. {
let s = rustls_accepted::rustls_accepted_signature_scheme(accepted, i);
if s == 0 {
Expand All @@ -729,7 +726,7 @@ mod tests {
&[1025, 1027, 1281, 1283, 1537, 2052, 2053, 2054, 2055]
);

let mut alpn: Vec<rustls_slice_bytes> = vec![];
let mut alpn = vec![];
for i in 0.. {
let a = rustls_accepted::rustls_accepted_alpn(accepted, i);
if a.len == 0 {
Expand All @@ -740,13 +737,13 @@ mod tests {

assert_eq!(alpn.len(), 2);
// No need to sort ALPN because order is determine by what the client sent.
let alpn0 = unsafe { std::slice::from_raw_parts(alpn[0].data, alpn[0].len) };
let alpn1 = unsafe { std::slice::from_raw_parts(alpn[1].data, alpn[1].len) };
let alpn0 = unsafe { slice::from_raw_parts(alpn[0].data, alpn[0].len) };
let alpn1 = unsafe { slice::from_raw_parts(alpn[1].data, alpn[1].len) };
assert_eq!(alpn0, "zarp".as_bytes());
assert_eq!(alpn1, "yuun".as_bytes());

let server_config = make_server_config();
let mut conn: *mut rustls_connection = null_mut();
let mut conn = null_mut();
let result = rustls_accepted::rustls_accepted_into_connection(
accepted,
server_config,
Expand Down
Loading