File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -154,6 +154,26 @@ impl Cmdline {
154154 Ok ( ( ) )
155155 }
156156
157+ /// Inserts a string, replacing non-printable ASCII characters with '?'.
158+ /// This is safe for use with strings that may contain emoji or special characters
159+ /// (e.g., from macOS system info) that would otherwise cause InvalidAscii errors.
160+ pub fn insert_str_safe < T : AsRef < str > > ( & mut self , slug : T ) -> Result < ( ) > {
161+ let s = slug. as_ref ( ) ;
162+ let sanitized: String = s. chars ( ) . map ( |c| if valid_char ( c) { c } else { '?' } ) . collect ( ) ;
163+ let trimmed = sanitized. trim ( ) ;
164+ if trimmed. is_empty ( ) {
165+ return Ok ( ( ) ) ;
166+ }
167+
168+ self . has_capacity ( trimmed. len ( ) ) ?;
169+
170+ self . start_push ( ) ;
171+ self . line . push_str ( trimmed) ;
172+ self . end_push ( ) ;
173+
174+ Ok ( ( ) )
175+ }
176+
157177 /// Returns the cmdline in progress without nul termination.
158178 pub fn as_str ( & self ) -> & str {
159179 self . line . as_str ( )
Original file line number Diff line number Diff line change @@ -1948,9 +1948,9 @@ pub fn build_microvm(
19481948 #[ allow( unused_mut) ]
19491949 let mut kernel_cmdline = Cmdline :: new ( arch:: CMDLINE_MAX_SIZE ) ;
19501950 if let Some ( cmdline) = payload_config. kernel_cmdline {
1951- kernel_cmdline. insert_str ( cmdline. as_str ( ) ) . unwrap ( ) ;
1951+ kernel_cmdline. insert_str_safe ( cmdline. as_str ( ) ) . unwrap ( ) ;
19521952 } else if let Some ( cmdline) = & vm_resources. kernel_cmdline . prolog {
1953- kernel_cmdline. insert_str ( cmdline) . unwrap ( ) ;
1953+ kernel_cmdline. insert_str_safe ( cmdline) . unwrap ( ) ;
19541954 } else {
19551955 kernel_cmdline. insert_str ( DEFAULT_KERNEL_CMDLINE ) . unwrap ( ) ;
19561956 }
@@ -1978,7 +1978,7 @@ pub fn build_microvm(
19781978 format ! ( "console={kernel_console}" ) . as_str ( ) ,
19791979 ) ;
19801980 kernel_cmdline = Cmdline :: new ( arch:: CMDLINE_MAX_SIZE ) ;
1981- kernel_cmdline. insert_str ( cmdline) . unwrap ( ) ;
1981+ kernel_cmdline. insert_str_safe ( cmdline) . unwrap ( ) ;
19821982 }
19831983
19841984 #[ cfg( target_os = "windows" ) ]
@@ -2547,7 +2547,7 @@ pub fn build_microvm(
25472547 }
25482548
25492549 if let Some ( s) = & vm_resources. kernel_cmdline . epilog {
2550- vmm. kernel_cmdline . insert_str ( s) . unwrap ( ) ;
2550+ vmm. kernel_cmdline . insert_str_safe ( s) . unwrap ( ) ;
25512551 } ;
25522552
25532553 #[ cfg( all( target_arch = "x86_64" , target_os = "windows" ) ) ]
You can’t perform that action at this time.
0 commit comments