Skip to content

Commit 90f7f76

Browse files
author
Marcin Radomski
committed
Prevent truncating log tag name
The currently hard-coded limit is 23 characters. This limit originates from the pre-Android 7.0 32-character limit on system property names, because Android's `liblog` allows setting the log level at runtime through `log.tag` system properties [1]. The limit meant that "log.tag.SOMETAG" must have been shorter than 32 bytes, leaving 23 bytes for the tag itself. The system property length limitation was removed in [2], so since 2017 it no longer applies, and some Android components already use longer tags [3][4]. This change increases the size of the stack-allocated buffer used for null-terminating the tag to 128 bytes, and adds support for even longer tags by falling back to heap-allocated CString when the tag is longer than that. The fallback path will introduce a performance penalty, but at the same time will allow manual level adjustment via log.tag. system property regardless of the tag length. [1] https://cs.android.com/android/platform/superproject/main/+/main:system/logging/logd/README.property;l=50?q=f:readme.property [2] https://android-review.googlesource.com/c/platform/bionic/+/327226 [3] https://cs.android.com/android/platform/superproject/main/+/main:device/generic/goldfish-opengl/system/codecs/omx/plugin/GoldfishVideoDecoderOMXComponent.cpp;l=20 [4] https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/biometrics/fingerprint/2.2/default/BiometricsFingerprint.cpp;l=16
1 parent d51b7ff commit 90f7f76

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl AndroidLogger {
179179

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

182-
const LOGGING_TAG_MAX_LEN: usize = 23;
182+
const LOGGING_TAG_MAX_LEN: usize = 128;
183183
const LOGGING_MSG_MAX_LEN: usize = 4000;
184184

185185
impl Default for AndroidLogger {
@@ -223,10 +223,20 @@ impl Log for AndroidLogger {
223223
.map(|s| s.as_bytes())
224224
.unwrap_or_else(|| module_path.as_bytes());
225225

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

231241
// message must not exceed LOGGING_MSG_MAX_LEN
232242
// therefore split log message into multiple log calls

0 commit comments

Comments
 (0)