Skip to content

Commit 8edaa45

Browse files
committed
rustls_error: correct output buffer length handling
This function does not need to keep an output buffer byte spare to zero-terminate the string, as it doesn't terminate the output. So don't do that. As an effect, this fixes the integer underflow followed by buffer overflow for `rustls_error` with an empty output buffer. `test_rustls_error_into_empty_buffer` is a regression test for that case.
1 parent 79a0d38 commit 8edaa45

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

librustls/src/error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl rustls_result {
235235
return;
236236
}
237237
let error_str = rustls_result::from(result).to_string();
238-
let out_len = min(len - 1, error_str.len());
238+
let out_len = min(len, error_str.len());
239239
unsafe {
240240
std::ptr::copy_nonoverlapping(error_str.as_ptr() as *mut c_char, buf, out_len);
241241
*out_n = out_len;
@@ -785,6 +785,13 @@ mod tests {
785785
assert_eq!(&output, "peer sent no certificates");
786786
}
787787

788+
#[test]
789+
fn test_rustls_error_into_empty_buffer() {
790+
let mut n = 99;
791+
rustls_result::rustls_error(0, &mut [] as *mut _, 0, &mut n);
792+
assert_eq!(n, 0);
793+
}
794+
788795
#[test]
789796
fn test_rustls_result_is_cert_error() {
790797
assert!(!rustls_result::rustls_result_is_cert_error(0));

0 commit comments

Comments
 (0)