gtk-rs / gtk4-rs

Rust bindings of GTK 4
https://gtk-rs.org/gtk4-rs/
MIT License
1.75k stars 168 forks source link

glib::Regex forced main thread to panic at the end of while loop #1749

Closed Wanpaku closed 1 month ago

Wanpaku commented 1 month ago

Bug description I'm trying to find all words that matching pattern of glib::Regex But in the end of the loop it panics:

thread 'main' panicked at /.cargo/registry/src/index.crates.io-6f17d22bba15001f/glib-0.19.6/src/match_info.rs:335:13: assertion left == right failed left: true right: false

let re = Regex::new(&regex_key,
        glib::RegexCompileFlags::MULTILINE, glib::RegexMatchFlags::DEFAULT)
        .expect("Can't create Regex").expect("Can't create Regex");
    let g_dict_body = glib::GString::from_string_unchecked(dict_body.into());
    let words = re.match_(g_dict_body.as_gstr(),
        glib::RegexMatchFlags::DEFAULT).expect("Can't get MatchInfo");
    while words.matches(){
        let word = words.fetch(0).expect("Can't get word");
        let (word_start, word_end) = 
            words.fetch_pos(0).expect("Can't get word start and end.");
        println!("word: {}, word start: {}, word end: {}",
            word, word_start, word_end);
        let _ = words.next();
    }

At the Rust forum another user pointed out this « match_info rs source

pub fn next(&self) -> Result<(), crate::Error> {
    unsafe {
        let mut error = std::ptr::null_mut();
        let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
        debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
        if error.is_null() {
            Ok(())
        } else {
            Err(from_glib_full(error))
        }
    }
}

So is_ok is GFALSE and error is null.

I would guess this means that glib has done something that the author of the rust bindings didn't know could happen. » The Rust forum topic

Backtrace

stack backtrace: 0: 0x562cf0e91c85 - ::fmt::h01ae9338a6ae91eb 1: 0x562cf0eb6543 - core::fmt::write::h491257998796a622 2: 0x562cf0e8fc5f - std::io::Write::write_fmt::hb2c64deac04aca6a 3: 0x562cf0e91a90 - std::sys_common::backtrace::print::h06f8cce91d6289fa 4: 0x562cf0e92f75 - std::panicking::default_hook::{{closure}}::hcd1fdb5834ba5bf8 5: 0x562cf0e92cf3 - std::panicking::default_hook::h40cebc24e84e383d 6: 0x562cf0e93408 - std::panicking::rust_panic_with_hook::h619a773c3e95c6fc 7: 0x562cf0e932e6 - std::panicking::begin_panic_handler::{{closure}}::h991678da18466d9f 8: 0x562cf0e92179 - std::sys_common::backtrace::rust_end_short_backtrace::h88c53d196138c39d 9: 0x562cf0e93037 - rust_begin_unwind 10: 0x562cf0d28966 - core::panicking::panic_fmt::h63d411b0acff0bd2 11: 0x562cf0d28d4e - core::panicking::assert_failed_inner::h15834c4f25c74195 12: 0x562cf0e620be - core::panicking::assert_failed::h0d53b73951d222ca at /usr/src/debug/rust/rustc-1.78.0-src/library/core/src/panicking.rs:298:5 13: 0x562cf0e57c2e - glib::match_info::MatchInfo::next::h7173fb4c461f339e at /home/tsurugi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/glib-0.19.6/src/match_info.rs:335:13 14: 0x562cf0db0cab - movar_rs::main_window::fileloader::create_dict_indexes::hc36d9bff9a2c5571 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main_window/fileloader.rs:155:17 15: 0x562cf0dafa60 - movar_rs::main_window::fileloader::download_dictionaries::hfa9318759374fa12 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main_window/fileloader.rs:95:25 16: 0x562cf0d6d7b6 - movar_rs::main_window::get_full_dict_indexes::hc1bfbed696abb875 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main_window.rs:379:29 17: 0x562cf0d68d73 - movar_rs::main_window::set_main_window::hb942b04af38470e3 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main_window.rs:33:30 18: 0x562cf0d32b9a - movar_rs::activate_main_window::h00e45c6e73880af4 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main.rs:21:9 19: 0x562cf0d445b8 - core::ops::function::Fn::call::h04e9ec6eae6b9f9d at /usr/src/debug/rust/rustc-1.78.0-src/library/core/src/ops/function.rs:79:5 20: 0x562cf0db9061 - gio::auto::application::ApplicationExt::connect_activate::activate_trampoline::ha6c83f7ec34a7164 at /home/tsurugi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/gio-0.19.5/src/auto/application.rs:458:13 21: 0x7fb70d564730 - g_closure_invoke 22: 0x7fb70d593896 - 23: 0x7fb70d5847a2 - 24: 0x7fb70d5849d7 - g_signal_emit_valist 25: 0x7fb70d584a94 - g_signal_emit 26: 0x7fb70d7d6cc1 - 27: 0x7fb70d7d6e6c - g_application_run 28: 0x562cf0d5a66e - gio::application::ApplicationExtManual::run_with_args::hebe31de1a0e7b0e3 at /home/tsurugi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/gio-0.19.5/src/application.rs:29:13 29: 0x562cf0d5a789 - gio::application::ApplicationExtManual::run::h48af189bc2fb9470 at /home/tsurugi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/gio-0.19.5/src/application.rs:22:9 30: 0x562cf0d32b5a - movar_rs::main::h90c42fc2e9a02a00 at /home/tsurugi/Програмування/Rust/Beginner projects/Movar_rs/src/main.rs:16:5 31: 0x562cf0d454ab - core::ops::function::FnOnce::call_once::h6a60e1e163879d6a at /usr/src/debug/rust/rustc-1.78.0-src/library/core/src/ops/function.rs:250:5 32: 0x562cf0d8340e - std::sys_common::backtrace::rust_begin_short_backtrace::hebc5bda3b7b039d3 at /usr/src/debug/rust/rustc-1.78.0-src/library/std/src/sys_common/backtrace.rs:155:18 33: 0x562cf0d9c551 - std::rt::lang_start::{{closure}}::h9912089c703b0c05 at /usr/src/debug/rust/rustc-1.78.0-src/library/std/src/rt.rs:166:18 34: 0x562cf0e8bc1e - std::rt::lang_start_internal::h89fc545bf879bd54 35: 0x562cf0d9c52a - std::rt::lang_start::h41a081b57723ca18 at /usr/src/debug/rust/rustc-1.78.0-src/library/std/src/rt.rs:165:17 36: 0x562cf0d32bbe - main 37: 0x7fb709241d4a - 38: 0x7fb709241e0c - __libc_start_main 39: 0x562cf0d29035 - _start 40: 0x0 -

sdroege commented 1 month ago

Thanks for reporting. See https://github.com/gtk-rs/gtk-rs-core/pull/1410