Skip to content

Commit dd1cb98

Browse files
committed
Edition bump and bugfix in log UTF8 handling
1 parent 1107f59 commit dd1cb98

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
This file will document the most important changes for each released version
44

5+
## [v0.2.6]
6+
7+
### Changes
8+
9+
- Update crate to edition 2024
10+
11+
### Bugfixes
12+
13+
- Logger does not panic anymore when given UTF8 with the NUL codepoint. They are now replaced with `char::REPLACEMENT_CHARACTER`
14+
515
## [v0.2.5]
616

717
### Changes

Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ resolver = "2"
33
members = ["unity_native", "unity_native_proc_macro", "unity_native_sys"]
44

55
[workspace.package]
6-
version = "0.2.5"
7-
edition = "2021"
6+
version = "0.2.6"
7+
edition = "2024"
88
authors = ["Wouter de Bruijn <[email protected]>"]
99
repository = "https://github.com/cloone8/unity-native"
1010
license = "MIT"
1111
keywords = []
1212
categories = []
1313

1414
[workspace.dependencies]
15-
unity_native = { path = "unity_native", version = "0.2.5" }
16-
unity_native_proc_macro = { path = "unity_native_proc_macro", version = "0.2.5" }
17-
unity_native_sys = { path = "unity_native_sys", version = "0.2.5" }
18-
bindgen = "0.69.4"
19-
syn = "2.0.68"
20-
quote = "1.0.36"
21-
thiserror = "1.0.61"
22-
log = "0.4.22"
15+
unity_native = { path = "unity_native", version = "0.2.6" }
16+
unity_native_proc_macro = { path = "unity_native_proc_macro", version = "0.2.6" }
17+
unity_native_sys = { path = "unity_native_sys", version = "0.2.6" }
18+
bindgen = "0.71.1"
19+
syn = "2.0.100"
20+
quote = "1.0.40"
21+
thiserror = "2.0.12"
22+
log = "0.4.27"
2323
static_assertions = "1.1.0"
2424
mint = "0.5.9"

unity_native/src/logger.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{ffi::CString, ptr::NonNull};
22

33
use log::Log;
44

5-
use crate::{ffi, unity_api_guid, UnityInterface};
5+
use crate::{UnityInterface, ffi, unity_api_guid};
66

77
/// A wrapper for the Unity Logging API. It supports
88
/// both manual logging using [UnityLogger::log_generic] (and friends),
@@ -97,9 +97,10 @@ impl UnityLogger {
9797
/// the function generating the log, so using the Rust [file!] and [line!]
9898
/// macros is recommended
9999
pub fn log_generic(&self, level: UnityLogType, msg: &str, filename: &str, line: u32) {
100-
let line_c = std::os::raw::c_int::try_from(line).unwrap();
101-
let message_c_str = CString::new(msg).unwrap();
102-
let filename_c_str = CString::new(filename).unwrap();
100+
let line_c = std::os::raw::c_int::try_from(line).unwrap_or(std::os::raw::c_int::MIN);
101+
102+
let message_c_str = filter_str_to_c_string(msg);
103+
let filename_c_str = filter_str_to_c_string(filename);
103104

104105
unsafe {
105106
let logger_raw = self.ptr.as_ref();
@@ -193,3 +194,24 @@ impl Log for UnityRustLogger {
193194
//no-op
194195
}
195196
}
197+
198+
/// Make a nul-terminated UTF8 string by replacing all characters with a NUL
199+
/// in it with the UTF8 replacement character
200+
fn filter_str_to_c_string(s: &str) -> CString {
201+
if !s.as_bytes().contains(&0) {
202+
return CString::new(s).expect("No NUL bytes were detected, check invalid");
203+
}
204+
205+
// A NUL byte was found, reconstruct the string char-by-char
206+
let mut tmp_str = String::new();
207+
208+
for c in s.chars() {
209+
if c == '\0' {
210+
tmp_str.push(char::REPLACEMENT_CHARACTER);
211+
} else {
212+
tmp_str.push(c);
213+
}
214+
}
215+
216+
CString::new(tmp_str).expect("Reconstructed string was invalid")
217+
}

unity_native/src/profiler/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl_from!(f64, Double);
156156
impl_from!(&'a str, String);
157157
impl_from!(&'a [u8], Bytes);
158158

159-
impl<'a> MarkerMetaData<'a> {
159+
impl MarkerMetaData<'_> {
160160
pub(super) fn to_c_compatible_bytes(self) -> Vec<u8> {
161161
match self {
162162
MarkerMetaData::Int32(x) => x.to_ne_bytes().to_vec(),

unity_native/src/profiler/sample.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use super::marker::MarkerMeta;
2-
use super::marker::ProfilerMarker;
31
use super::EventType;
42
use super::UnityProfiler;
3+
use super::marker::MarkerMeta;
4+
use super::marker::ProfilerMarker;
55

66
#[derive(Debug)]
77
pub enum ScopedProfilerSample<'a, 'b, T: MarkerMeta<N>, const N: usize> {
@@ -22,7 +22,7 @@ pub enum ManualProfilerSample<'a, 'b, T: MarkerMeta<N>, const N: usize> {
2222
},
2323
}
2424

25-
impl<'a, 'b, T: MarkerMeta<N>, const N: usize> Drop for ScopedProfilerSample<'a, 'b, T, N> {
25+
impl<T: MarkerMeta<N>, const N: usize> Drop for ScopedProfilerSample<'_, '_, T, N> {
2626
fn drop(&mut self) {
2727
match self {
2828
ScopedProfilerSample::Disabled => {}
@@ -33,7 +33,7 @@ impl<'a, 'b, T: MarkerMeta<N>, const N: usize> Drop for ScopedProfilerSample<'a,
3333
}
3434
}
3535

36-
impl<'a, 'b, T: MarkerMeta<N>, const N: usize> ManualProfilerSample<'a, 'b, T, N> {
36+
impl<T: MarkerMeta<N>, const N: usize> ManualProfilerSample<'_, '_, T, N> {
3737
pub fn end_sample(&mut self) {
3838
match self {
3939
ManualProfilerSample::Disabled => {}
@@ -54,7 +54,7 @@ impl<'a, 'b, T: MarkerMeta<N>, const N: usize> ManualProfilerSample<'a, 'b, T, N
5454
}
5555
}
5656

57-
impl<'a, 'b, T: MarkerMeta<N>, const N: usize> Drop for ManualProfilerSample<'a, 'b, T, N> {
57+
impl<T: MarkerMeta<N>, const N: usize> Drop for ManualProfilerSample<'_, '_, T, N> {
5858
fn drop(&mut self) {
5959
if let ManualProfilerSample::Enabled {
6060
ended,

0 commit comments

Comments
 (0)