|
| 1 | +From 1d78c8bd4295262a3118f478e6b3a7c7536fa282 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Johannes Altmanninger <aclopte@gmail.com> |
| 3 | +Date: Wed, 19 Mar 2025 09:39:04 +0100 |
| 4 | +Subject: [PATCH] Fix concurrent setlocale() in string escape tests |
| 5 | + |
| 6 | +In our C++ implementation, these tests were run serially. As pointed out in |
| 7 | +https://github.com/fish-shell/fish-shell/issues/11254#issuecomment-2735623229 |
| 8 | +we run them in parallel now, which means that one test could be changing |
| 9 | +the global locale used by another. |
| 10 | + |
| 11 | +In theory this could be fine because all tests are setting setting the |
| 12 | +global locale to the same thing but the existence of a lock suggests that |
| 13 | +setlocale() is not guaranteed to be atomic, so it's possible that another |
| 14 | +thread uses a temporarily-invalid locale. |
| 15 | + |
| 16 | +Fixes #11254 |
| 17 | +--- |
| 18 | + src/tests/string_escape.rs | 14 ++++++++------ |
| 19 | + 1 file changed, 8 insertions(+), 6 deletions(-) |
| 20 | + |
| 21 | +diff --git a/src/tests/string_escape.rs b/src/tests/string_escape.rs |
| 22 | +index ba8ee7534ebf..4428d679cd35 100644 |
| 23 | +--- a/src/tests/string_escape.rs |
| 24 | ++++ b/src/tests/string_escape.rs |
| 25 | +@@ -1,3 +1,5 @@ |
| 26 | ++use std::sync::MutexGuard; |
| 27 | ++ |
| 28 | + use crate::common::{ |
| 29 | + escape_string, str2wcstring, unescape_string, wcs2string, EscapeFlags, EscapeStringStyle, |
| 30 | + UnescapeStringStyle, ENCODE_DIRECT_BASE, ENCODE_DIRECT_END, |
| 31 | +@@ -10,21 +12,21 @@ use rand::{Rng, RngCore}; |
| 32 | + |
| 33 | + /// wcs2string is locale-dependent, so ensure we have a multibyte locale |
| 34 | + /// before using it in a test. |
| 35 | +-fn setlocale() { |
| 36 | +- let _guard = LOCALE_LOCK.lock().unwrap(); |
| 37 | ++fn setlocale() -> MutexGuard<'static, ()> { |
| 38 | ++ let guard = LOCALE_LOCK.lock().unwrap(); |
| 39 | + |
| 40 | + #[rustfmt::skip] |
| 41 | + const UTF8_LOCALES: &[&str] = &[ |
| 42 | + "C.UTF-8", "en_US.UTF-8", "en_GB.UTF-8", "de_DE.UTF-8", "C.utf8", "UTF-8", |
| 43 | + ]; |
| 44 | + if crate::libc::MB_CUR_MAX() > 1 { |
| 45 | +- return; |
| 46 | ++ return guard; |
| 47 | + } |
| 48 | + for locale in UTF8_LOCALES { |
| 49 | + let locale = std::ffi::CString::new(locale.to_owned()).unwrap(); |
| 50 | + unsafe { libc::setlocale(libc::LC_CTYPE, locale.as_ptr()) }; |
| 51 | + if crate::libc::MB_CUR_MAX() > 1 { |
| 52 | +- return; |
| 53 | ++ return guard; |
| 54 | + } |
| 55 | + } |
| 56 | + panic!("No UTF-8 locale found"); |
| 57 | +@@ -100,7 +102,7 @@ fn test_escape_var() { |
| 58 | + } |
| 59 | + |
| 60 | + fn escape_test(escape_style: EscapeStringStyle, unescape_style: UnescapeStringStyle) { |
| 61 | +- setlocale(); |
| 62 | ++ let _locale_guard = setlocale(); |
| 63 | + let seed: u128 = 92348567983274852905629743984572; |
| 64 | + let mut rng = get_seeded_rng(seed); |
| 65 | + |
| 66 | +@@ -174,7 +176,7 @@ fn str2hex(input: &[u8]) -> String { |
| 67 | + /// string comes back through double conversion. |
| 68 | + #[test] |
| 69 | + fn test_convert() { |
| 70 | +- setlocale(); |
| 71 | ++ let _locale_guard = setlocale(); |
| 72 | + let seed = get_rng_seed(); |
| 73 | + let mut rng = get_seeded_rng(seed); |
| 74 | + let mut origin = Vec::new(); |
0 commit comments