log: fix panic when using a log id with a formatted log message

Would panic because of a missing NUL terminator:

thread '<unnamed>' panicked at .. glib/src/gstring.rs:61:9:
assertion failed: !bytes.is_empty() && bytes[bytes.len() - 1] == 0

 3: glib::gstring::GStr::from_utf8_with_nul_unchecked
 4: gstreamer::log::DebugCategory::log_id_unfiltered_internal
      at gstreamer-rs/gstreamer/src/log.rs:465
 5: gstreamer::log::DebugCategory::log_id_unfiltered
      at gstreamer-rs/gstreamer/src/log.rs:425

Also enable logging on the category, so the logging code paths
are actually run here in the tests.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1451>
This commit is contained in:
Tim-Philipp Müller 2024-06-02 17:13:12 +03:00
parent b20ea25147
commit c545154472

View file

@ -460,6 +460,7 @@ impl DebugCategory {
if std::io::Write::write_fmt(&mut w, args).is_err() { if std::io::Write::write_fmt(&mut w, args).is_err() {
return; return;
} }
w.push(0);
self.log_id_literal_unfiltered_internal(id, level, file, function, line, unsafe { self.log_id_literal_unfiltered_internal(id, level, file, function, line, unsafe {
glib::GStr::from_utf8_with_nul_unchecked(&w) glib::GStr::from_utf8_with_nul_unchecked(&w)
@ -1498,8 +1499,14 @@ mod tests {
Some("Blablabla"), Some("Blablabla"),
); );
cat.set_threshold(crate::DebugLevel::Trace);
trace!(cat, id: "123", "test"); trace!(cat, id: "123", "test");
trace!(cat, id: glib::GString::from("123"), "test"); trace!(cat, id: glib::GString::from("123"), "test");
trace!(cat, id: &glib::GString::from("123"), "test"); trace!(cat, id: &glib::GString::from("123"), "test");
// Try with a formatted string too (which is a different code path in the bindings)
let log_id = glib::GString::from("456");
trace!(cat, id: &log_id , "{log_id:?}");
} }
} }