File tree Expand file tree Collapse file tree 3 files changed +17
-42
lines changed
src/vmm/src/devices/virtio Expand file tree Collapse file tree 3 files changed +17
-42
lines changed Original file line number Diff line number Diff line change 11// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- use std:: io :: Write ;
4+ use std:: fmt ;
55use std:: sync:: atomic:: AtomicU32 ;
66use std:: sync:: Arc ;
77use std:: time:: Duration ;
8- use std:: { cmp, fmt} ;
98
109use log:: error;
1110use serde:: Serialize ;
@@ -593,20 +592,12 @@ impl VirtioDevice for Balloon {
593592 self . irq_trigger . irq_status . clone ( )
594593 }
595594
596- fn read_config ( & self , offset : u64 , mut data : & mut [ u8 ] ) {
597- let config_space_bytes = self . config_space . as_slice ( ) ;
598- let config_len = config_space_bytes. len ( ) as u64 ;
599- if offset >= config_len {
595+ fn read_config ( & self , offset : u64 , data : & mut [ u8 ] ) {
596+ if let Some ( config_space_bytes) = self . config_space . as_slice ( ) . get ( u64_to_usize ( offset) ..) {
597+ let len = config_space_bytes. len ( ) . min ( data. len ( ) ) ;
598+ data[ ..len] . copy_from_slice ( & config_space_bytes[ ..len] ) ;
599+ } else {
600600 error ! ( "Failed to read config space" ) ;
601- return ;
602- }
603-
604- if let Some ( end) = offset. checked_add ( data. len ( ) as u64 ) {
605- // This write can't fail, offset and end are checked against config_len.
606- data. write_all (
607- & config_space_bytes[ u64_to_usize ( offset) ..u64_to_usize ( cmp:: min ( end, config_len) ) ] ,
608- )
609- . unwrap ( ) ;
610601 }
611602 }
612603
Original file line number Diff line number Diff line change 44// Portions Copyright 2019 Intel Corporation. All Rights Reserved.
55// SPDX-License-Identifier: Apache-2.0
66
7- use std:: cmp;
8- use std:: io:: Write ;
97use std:: sync:: atomic:: AtomicU32 ;
108use std:: sync:: Arc ;
119
@@ -322,19 +320,13 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
322320 self . irq_trigger . irq_status . clone ( )
323321 }
324322
325- fn read_config ( & self , offset : u64 , mut data : & mut [ u8 ] ) {
326- let config_len = self . config_space . len ( ) as u64 ;
327- if offset >= config_len {
323+ fn read_config ( & self , offset : u64 , data : & mut [ u8 ] ) {
324+ if let Some ( config_space_bytes) = self . config_space . as_slice ( ) . get ( u64_to_usize ( offset) ..) {
325+ let len = config_space_bytes. len ( ) . min ( data. len ( ) ) ;
326+ data[ ..len] . copy_from_slice ( & config_space_bytes[ ..len] ) ;
327+ } else {
328328 error ! ( "Failed to read config space" ) ;
329329 self . metrics . cfg_fails . inc ( ) ;
330- return ;
331- }
332- if let Some ( end) = offset. checked_add ( data. len ( ) as u64 ) {
333- // This write can't fail, offset and end are checked against config_len.
334- data. write_all (
335- & self . config_space [ u64_to_usize ( offset) ..u64_to_usize ( cmp:: min ( end, config_len) ) ] ,
336- )
337- . unwrap ( ) ;
338330 }
339331 }
340332
Original file line number Diff line number Diff line change 77
88#[ cfg( not( test) ) ]
99use std:: io:: Read ;
10- use std:: io :: Write ;
10+ use std:: mem ;
1111use std:: net:: Ipv4Addr ;
1212use std:: sync:: atomic:: AtomicU32 ;
1313use std:: sync:: { Arc , Mutex } ;
14- use std:: { cmp, mem} ;
1514
1615use libc:: EAGAIN ;
1716use log:: { error, warn} ;
@@ -832,20 +831,13 @@ impl VirtioDevice for Net {
832831 self . irq_trigger . irq_status . clone ( )
833832 }
834833
835- fn read_config ( & self , offset : u64 , mut data : & mut [ u8 ] ) {
836- let config_space_bytes = self . config_space . as_slice ( ) ;
837- let config_len = config_space_bytes. len ( ) as u64 ;
838- if offset >= config_len {
834+ fn read_config ( & self , offset : u64 , data : & mut [ u8 ] ) {
835+ if let Some ( config_space_bytes) = self . config_space . as_slice ( ) . get ( u64_to_usize ( offset) ..) {
836+ let len = config_space_bytes. len ( ) . min ( data. len ( ) ) ;
837+ data[ ..len] . copy_from_slice ( & config_space_bytes[ ..len] ) ;
838+ } else {
839839 error ! ( "Failed to read config space" ) ;
840840 self . metrics . cfg_fails . inc ( ) ;
841- return ;
842- }
843- if let Some ( end) = offset. checked_add ( data. len ( ) as u64 ) {
844- // This write can't fail, offset and end are checked against config_len.
845- data. write_all (
846- & config_space_bytes[ u64_to_usize ( offset) ..u64_to_usize ( cmp:: min ( end, config_len) ) ] ,
847- )
848- . unwrap ( ) ;
849841 }
850842 }
851843
You can’t perform that action at this time.
0 commit comments