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 1
1
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- use std:: io :: Write ;
4
+ use std:: fmt ;
5
5
use std:: sync:: atomic:: AtomicU32 ;
6
6
use std:: sync:: Arc ;
7
7
use std:: time:: Duration ;
8
- use std:: { cmp, fmt} ;
9
8
10
9
use log:: error;
11
10
use serde:: Serialize ;
@@ -593,20 +592,12 @@ impl VirtioDevice for Balloon {
593
592
self . irq_trigger . irq_status . clone ( )
594
593
}
595
594
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 {
600
600
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 ( ) ;
610
601
}
611
602
}
612
603
Original file line number Diff line number Diff line change 4
4
// Portions Copyright 2019 Intel Corporation. All Rights Reserved.
5
5
// SPDX-License-Identifier: Apache-2.0
6
6
7
- use std:: cmp;
8
- use std:: io:: Write ;
9
7
use std:: sync:: atomic:: AtomicU32 ;
10
8
use std:: sync:: Arc ;
11
9
@@ -322,19 +320,13 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
322
320
self . irq_trigger . irq_status . clone ( )
323
321
}
324
322
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 {
328
328
error ! ( "Failed to read config space" ) ;
329
329
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 ( ) ;
338
330
}
339
331
}
340
332
Original file line number Diff line number Diff line change 7
7
8
8
#[ cfg( not( test) ) ]
9
9
use std:: io:: Read ;
10
- use std:: io :: Write ;
10
+ use std:: mem ;
11
11
use std:: net:: Ipv4Addr ;
12
12
use std:: sync:: atomic:: AtomicU32 ;
13
13
use std:: sync:: { Arc , Mutex } ;
14
- use std:: { cmp, mem} ;
15
14
16
15
use libc:: EAGAIN ;
17
16
use log:: { error, warn} ;
@@ -832,20 +831,13 @@ impl VirtioDevice for Net {
832
831
self . irq_trigger . irq_status . clone ( )
833
832
}
834
833
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 {
839
839
error ! ( "Failed to read config space" ) ;
840
840
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 ( ) ;
849
841
}
850
842
}
851
843
You can’t perform that action at this time.
0 commit comments