From 4652e58ee182d2ca81754a78293fb961a601d395 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sat, 20 Nov 2021 02:06:37 +0100 Subject: [PATCH 1/5] we can expect non-utf8 strings in tracing messages --- src/tracing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tracing.rs b/src/tracing.rs index f348575774..099262530b 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -76,7 +76,7 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) { let cb = CALLBACK.load(Ordering::SeqCst); panic::wrap(|| unsafe { let cb: TracingCb = std::mem::transmute(cb); - let msg = std::ffi::CStr::from_ptr(msg).to_str().unwrap(); - cb(Binding::from_raw(level), msg); + let msg = std::ffi::CStr::from_ptr(msg).to_string_lossy(); + cb(Binding::from_raw(level), msg.as_ref()); }); } From 50a990fd81bc7f410292bc444dd667630406411c Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 22 Nov 2021 21:22:39 +0100 Subject: [PATCH 2/5] pass tracing msg as slice instead of &str --- src/tracing.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tracing.rs b/src/tracing.rs index 099262530b..12c3c9280e 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -57,7 +57,7 @@ impl Binding for TraceLevel { } } -pub type TracingCb = fn(TraceLevel, &str); +pub type TracingCb = fn(TraceLevel, &[u8]); static CALLBACK: AtomicUsize = AtomicUsize::new(0); @@ -76,7 +76,10 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) { let cb = CALLBACK.load(Ordering::SeqCst); panic::wrap(|| unsafe { let cb: TracingCb = std::mem::transmute(cb); - let msg = std::ffi::CStr::from_ptr(msg).to_string_lossy(); - cb(Binding::from_raw(level), msg.as_ref()); + + if !msg.is_null() { + let msg = std::ffi::CStr::from_ptr(msg).to_bytes(); + cb(Binding::from_raw(level), msg); + } }); } From 9cdc7af9d79b586708fc0886330b1b4b0930b5dc Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 22 Nov 2021 21:28:20 +0100 Subject: [PATCH 3/5] doc comment --- src/tracing.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tracing.rs b/src/tracing.rs index 12c3c9280e..6a1481b698 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -57,6 +57,12 @@ impl Binding for TraceLevel { } } +/// Callback type used to pass tracing events to the subscriber. +/// see `trace_set` to register a scubscriber. +/// +/// Note: +/// libgit2 might pass non-utf8 strings therefore we +/// pass the message as a byte slice pub type TracingCb = fn(TraceLevel, &[u8]); static CALLBACK: AtomicUsize = AtomicUsize::new(0); From 33fcb27cfa688533362efa5738952af39db913d2 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 29 Nov 2021 22:52:04 +0100 Subject: [PATCH 4/5] Revert "pass tracing msg as slice instead of &str" This reverts commit 50a990fd81bc7f410292bc444dd667630406411c. # Conflicts: # src/tracing.rs --- src/tracing.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/tracing.rs b/src/tracing.rs index 6a1481b698..5f34027c3e 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -63,7 +63,7 @@ impl Binding for TraceLevel { /// Note: /// libgit2 might pass non-utf8 strings therefore we /// pass the message as a byte slice -pub type TracingCb = fn(TraceLevel, &[u8]); +pub type TracingCb = fn(TraceLevel, &str); static CALLBACK: AtomicUsize = AtomicUsize::new(0); @@ -82,10 +82,7 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) { let cb = CALLBACK.load(Ordering::SeqCst); panic::wrap(|| unsafe { let cb: TracingCb = std::mem::transmute(cb); - - if !msg.is_null() { - let msg = std::ffi::CStr::from_ptr(msg).to_bytes(); - cb(Binding::from_raw(level), msg); - } + let msg = std::ffi::CStr::from_ptr(msg).to_string_lossy(); + cb(Binding::from_raw(level), msg.as_ref()); }); } From 4cfb7740e45230fa1ccfcfca3abfa525df49743a Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 29 Nov 2021 22:55:03 +0100 Subject: [PATCH 5/5] update doc comment and add todo --- src/tracing.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/tracing.rs b/src/tracing.rs index 5f34027c3e..691410cfc3 100644 --- a/src/tracing.rs +++ b/src/tracing.rs @@ -57,12 +57,9 @@ impl Binding for TraceLevel { } } +//TODO: pass raw &[u8] and leave conversion to consumer (breaking API) /// Callback type used to pass tracing events to the subscriber. /// see `trace_set` to register a scubscriber. -/// -/// Note: -/// libgit2 might pass non-utf8 strings therefore we -/// pass the message as a byte slice pub type TracingCb = fn(TraceLevel, &str); static CALLBACK: AtomicUsize = AtomicUsize::new(0);