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

Prevent truncating log tag name #83

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl AndroidLogger {

static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();

const LOGGING_TAG_MAX_LEN: usize = 23;
const LOGGING_TAG_MAX_LEN: usize = 128;
const LOGGING_MSG_MAX_LEN: usize = 4000;

impl Default for AndroidLogger {
Expand Down Expand Up @@ -223,10 +223,20 @@ impl Log for AndroidLogger {
.map(|s| s.as_bytes())
.unwrap_or_else(|| module_path.as_bytes());

// truncate the tag here to fit into LOGGING_TAG_MAX_LEN
self.fill_tag_bytes(&mut tag_bytes, tag);
// use stack array as C string
let tag: &CStr = unsafe { CStr::from_ptr(mem::transmute(tag_bytes.as_ptr())) };
// In case we end up allocating, keep the CString alive.
let mut _owned_tag = None;
let tag: &CStr = if tag.len() < tag_bytes.len() {
// use stack array as C string
self.fill_tag_bytes(&mut tag_bytes, tag);
// SAFETY: fill_tag_bytes always puts a nullbyte in tag_bytes.
unsafe { CStr::from_ptr(mem::transmute(tag_bytes.as_ptr())) }
} else {
// Tag longer than available stack buffer; allocate.
// SAFETY: if tag contains nullbytes, the Android logger will just ignore any
// characters that follow it.
_owned_tag = Some(unsafe { CString::from_vec_unchecked(tag.to_vec()) });
_owned_tag.as_ref().unwrap()
};

// message must not exceed LOGGING_MSG_MAX_LEN
// therefore split log message into multiple log calls
Expand Down