Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled null characters in Android logs #12743

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crates/bevy_log/src/android_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use bevy_utils::tracing::{
span::{Attributes, Record},
Event, Id, Level, Subscriber,
};
use std::fmt::{Debug, Write};
use std::{
ffi::CString,
fmt::{Debug, Write},
};
use tracing_subscriber::{field::Visit, layer::Context, registry::LookupSpan, Layer};

#[derive(Default)]
Expand Down Expand Up @@ -85,13 +88,15 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for AndroidLayer {
Level::WARN => android_log_sys::LogPriority::WARN,
Level::ERROR => android_log_sys::LogPriority::ERROR,
};
let message = format!("{}\0", recorder);
let tag = format!("{}\0", meta.name());
let message = CString::new(recorder).expect("message contained null character.");
let tag = CString::new(meta.name()).expect("tag contained null character.");
james7132 marked this conversation as resolved.
Show resolved Hide resolved
// SAFETY: Called only on Android platforms. priority is guaranteed to be in range of c_int.
// The provided tag and message are null terminated properly.
unsafe {
android_log_sys::__android_log_write(
priority as android_log_sys::c_int,
tag.as_ptr() as *const android_log_sys::c_char,
message.as_ptr() as *const android_log_sys::c_char,
tag.as_c_str().as_ptr(),
james7132 marked this conversation as resolved.
Show resolved Hide resolved
message.as_c_str().as_ptr(),
);
}
}
Expand Down