Skip to content

Commit 476c07d

Browse files
committed
Auto merge of rust-lang#123823 - matthiaskrgr:rollup-8zdtggx, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#122882 (Avoid a panic in `set_output_capture` in the default panic handler) - rust-lang#123523 (Account for trait/impl difference when suggesting changing argument from ref to mut ref) - rust-lang#123744 (Silence `unused_imports` for redundant imports) - rust-lang#123784 (Replace `document.write` with `document.head.insertAdjacent`) - rust-lang#123798 (Avoid invalid socket address in length calculation) - rust-lang#123804 (Stop using `HirId` for fn-like parents since closures are not `OwnerNode`s) - rust-lang#123806 (Panic on overflow in `BorrowedCursor::advance`) - rust-lang#123820 (Add my former address to .mailmap) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 721c056 + e4cc715 commit 476c07d

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

core/src/io/borrowed_buf.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@ impl<'a> BorrowedCursor<'a> {
249249
/// Panics if there are less than `n` bytes initialized.
250250
#[inline]
251251
pub fn advance(&mut self, n: usize) -> &mut Self {
252-
assert!(self.buf.init >= self.buf.filled + n);
252+
let filled = self.buf.filled.strict_add(n);
253+
assert!(filled <= self.buf.init);
253254

254-
self.buf.filled += n;
255+
self.buf.filled = filled;
255256
self
256257
}
257258

core/src/net/socket_addr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl fmt::Display for SocketAddrV4 {
591591
if f.precision().is_none() && f.width().is_none() {
592592
write!(f, "{}:{}", self.ip(), self.port())
593593
} else {
594-
const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65536";
594+
const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65535";
595595

596596
let mut buf = DisplayBuffer::<{ LONGEST_IPV4_SOCKET_ADDR.len() }>::new();
597597
// Buffer is long enough for the longest possible IPv4 socket address, so this should never fail.
@@ -621,7 +621,7 @@ impl fmt::Display for SocketAddrV6 {
621621
}
622622
} else {
623623
const LONGEST_IPV6_SOCKET_ADDR: &str =
624-
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967296]:65536";
624+
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967295]:65535";
625625

626626
let mut buf = DisplayBuffer::<{ LONGEST_IPV6_SOCKET_ADDR.len() }>::new();
627627
match self.scope_id() {

std/src/io/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ pub use self::buffered::WriterPanicked;
311311
#[unstable(feature = "raw_os_error_ty", issue = "107792")]
312312
pub use self::error::RawOsError;
313313
pub(crate) use self::stdio::attempt_print_to_stderr;
314-
#[unstable(feature = "internal_output_capture", issue = "none")]
315-
#[doc(no_inline, hidden)]
316-
pub use self::stdio::set_output_capture;
317314
#[stable(feature = "is_terminal", since = "1.70.0")]
318315
pub use self::stdio::IsTerminal;
319316
#[unstable(feature = "print_internals", issue = "none")]
320317
#[doc(hidden)]
321318
pub use self::stdio::{_eprint, _print};
319+
#[unstable(feature = "internal_output_capture", issue = "none")]
320+
#[doc(no_inline, hidden)]
321+
pub use self::stdio::{set_output_capture, try_set_output_capture};
322322
#[stable(feature = "rust1", since = "1.0.0")]
323323
pub use self::{
324324
buffered::{BufReader, BufWriter, IntoInnerError, LineWriter},

std/src/io/stdio.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::panic::{RefUnwindSafe, UnwindSafe};
1515
use crate::sync::atomic::{AtomicBool, Ordering};
1616
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantLock, ReentrantLockGuard};
1717
use crate::sys::stdio;
18+
use crate::thread::AccessError;
1819

1920
type LocalStream = Arc<Mutex<Vec<u8>>>;
2021

@@ -1064,12 +1065,31 @@ impl fmt::Debug for StderrLock<'_> {
10641065
)]
10651066
#[doc(hidden)]
10661067
pub fn set_output_capture(sink: Option<LocalStream>) -> Option<LocalStream> {
1068+
try_set_output_capture(sink).expect(
1069+
"cannot access a Thread Local Storage value \
1070+
during or after destruction",
1071+
)
1072+
}
1073+
1074+
/// Tries to set the thread-local output capture buffer and returns the old one.
1075+
/// This may fail once thread-local destructors are called. It's used in panic
1076+
/// handling instead of `set_output_capture`.
1077+
#[unstable(
1078+
feature = "internal_output_capture",
1079+
reason = "this function is meant for use in the test crate \
1080+
and may disappear in the future",
1081+
issue = "none"
1082+
)]
1083+
#[doc(hidden)]
1084+
pub fn try_set_output_capture(
1085+
sink: Option<LocalStream>,
1086+
) -> Result<Option<LocalStream>, AccessError> {
10671087
if sink.is_none() && !OUTPUT_CAPTURE_USED.load(Ordering::Relaxed) {
10681088
// OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
1069-
return None;
1089+
return Ok(None);
10701090
}
10711091
OUTPUT_CAPTURE_USED.store(true, Ordering::Relaxed);
1072-
OUTPUT_CAPTURE.with(move |slot| slot.replace(sink))
1092+
OUTPUT_CAPTURE.try_with(move |slot| slot.replace(sink))
10731093
}
10741094

10751095
/// Write `args` to the capture buffer if enabled and possible, or `global_s`

std/src/io/tests.rs

+9
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ fn read_buf_exact() {
209209
assert_eq!(c.read_buf_exact(buf.unfilled()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
210210
}
211211

212+
#[test]
213+
#[should_panic]
214+
fn borrowed_cursor_advance_overflow() {
215+
let mut buf = [0; 512];
216+
let mut buf = BorrowedBuf::from(&mut buf[..]);
217+
buf.unfilled().advance(1);
218+
buf.unfilled().advance(usize::MAX);
219+
}
220+
212221
#[test]
213222
fn take_eof() {
214223
struct R;

std/src/panicking.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use crate::sys_common::backtrace;
2424
use crate::thread;
2525

2626
#[cfg(not(test))]
27-
use crate::io::set_output_capture;
27+
use crate::io::try_set_output_capture;
2828
// make sure to use the stderr output configured
2929
// by libtest in the real copy of std
3030
#[cfg(test)]
31-
use realstd::io::set_output_capture;
31+
use realstd::io::try_set_output_capture;
3232

3333
// Binary interface to the panic runtime that the standard library depends on.
3434
//
@@ -284,9 +284,9 @@ fn default_hook(info: &PanicInfo<'_>) {
284284
}
285285
};
286286

287-
if let Some(local) = set_output_capture(None) {
287+
if let Ok(Some(local)) = try_set_output_capture(None) {
288288
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
289-
set_output_capture(Some(local));
289+
try_set_output_capture(Some(local)).ok();
290290
} else if let Some(mut out) = panic_output() {
291291
write(&mut out);
292292
}

0 commit comments

Comments
 (0)