File tree Expand file tree Collapse file tree 4 files changed +120
-3
lines changed Expand file tree Collapse file tree 4 files changed +120
-3
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,47 @@ impl BootLoaderNameTag {
29
29
/// ```
30
30
pub fn name ( & self ) -> Result < & str , Utf8Error > {
31
31
use core:: { mem, slice, str} ;
32
+ // strlen without null byte
32
33
let strlen = self . size as usize - mem:: size_of :: < BootLoaderNameTag > ( ) ;
33
34
let bytes = unsafe { slice:: from_raw_parts ( ( & self . string ) as * const u8 , strlen) } ;
34
35
str:: from_utf8 ( bytes)
35
36
}
36
37
}
38
+
39
+ #[ cfg( test) ]
40
+ mod tests {
41
+ use crate :: TagType ;
42
+
43
+ const MSG : & str = "hello" ;
44
+
45
+ /// Returns the tag structure in bytes in native endian format.
46
+ fn get_bytes ( ) -> std:: vec:: Vec < u8 > {
47
+ // size is: 4 bytes for tag + 4 bytes for size + length of null-terminated string
48
+ let size = ( 4 + 4 + MSG . as_bytes ( ) . len ( ) + 1 ) as u32 ;
49
+ [
50
+ & ( ( TagType :: BootLoaderName as u32 ) . to_ne_bytes ( ) ) ,
51
+ & size. to_ne_bytes ( ) ,
52
+ MSG . as_bytes ( ) ,
53
+ // Null Byte
54
+ & [ 0 ] ,
55
+ ]
56
+ . iter ( )
57
+ . flat_map ( |bytes| bytes. iter ( ) )
58
+ . copied ( )
59
+ . collect ( )
60
+ }
61
+
62
+ /// Tests to parse a string with a terminating null byte from the tag (as the spec defines).
63
+ #[ test]
64
+ fn test_parse_str ( ) {
65
+ let tag = get_bytes ( ) ;
66
+ let tag = unsafe {
67
+ tag. as_ptr ( )
68
+ . cast :: < super :: BootLoaderNameTag > ( )
69
+ . as_ref ( )
70
+ . unwrap ( )
71
+ } ;
72
+ assert_eq ! ( { tag. typ } , TagType :: BootLoaderName ) ;
73
+ assert_eq ! ( tag. name( ) . expect( "must be valid UTF-8" ) , MSG ) ;
74
+ }
75
+ }
Original file line number Diff line number Diff line change 3
3
use crate :: TagType ;
4
4
use core:: mem;
5
5
use core:: slice;
6
- use core:: str:: Utf8Error ;
6
+ use core:: str;
7
7
8
8
/// This tag contains the command line string.
9
9
///
@@ -31,9 +31,48 @@ impl CommandLineTag {
31
31
/// assert_eq!("/bootarg", command_line);
32
32
/// }
33
33
/// ```
34
- pub fn command_line ( & self ) -> Result < & str , Utf8Error > {
34
+ pub fn command_line ( & self ) -> Result < & str , str:: Utf8Error > {
35
+ // strlen without null byte
35
36
let strlen = self . size as usize - mem:: size_of :: < CommandLineTag > ( ) ;
36
37
let bytes = unsafe { slice:: from_raw_parts ( ( & self . string ) as * const u8 , strlen) } ;
37
- core:: str:: from_utf8 ( bytes)
38
+ str:: from_utf8 ( bytes)
39
+ }
40
+ }
41
+
42
+ #[ cfg( test) ]
43
+ mod tests {
44
+ use crate :: TagType ;
45
+
46
+ const MSG : & str = "hello" ;
47
+
48
+ /// Returns the tag structure in bytes in native endian format.
49
+ fn get_bytes ( ) -> std:: vec:: Vec < u8 > {
50
+ // size is: 4 bytes for tag + 4 bytes for size + length of null-terminated string
51
+ let size = ( 4 + 4 + MSG . as_bytes ( ) . len ( ) + 1 ) as u32 ;
52
+ [
53
+ & ( ( TagType :: Cmdline as u32 ) . to_ne_bytes ( ) ) ,
54
+ & size. to_ne_bytes ( ) ,
55
+ MSG . as_bytes ( ) ,
56
+ // Null Byte
57
+ & [ 0 ] ,
58
+ ]
59
+ . iter ( )
60
+ . flat_map ( |bytes| bytes. iter ( ) )
61
+ . copied ( )
62
+ . collect ( )
63
+ }
64
+
65
+ /// Tests to parse a string with a terminating null byte from the tag (as the spec defines).
66
+ #[ test]
67
+ fn test_parse_str ( ) {
68
+ let tag = get_bytes ( ) ;
69
+ let tag = unsafe {
70
+ tag. as_ptr ( )
71
+ . cast :: < super :: CommandLineTag > ( )
72
+ . as_ref ( )
73
+ . unwrap ( )
74
+ } ;
75
+ assert_eq ! ( { tag. typ } , TagType :: Cmdline ) ;
76
+ assert_eq ! ( tag. command_line( ) . expect( "must be valid UTF-8" ) , MSG ) ;
38
77
}
39
78
}
Original file line number Diff line number Diff line change @@ -190,6 +190,8 @@ impl ElfSection {
190
190
use core:: { slice, str} ;
191
191
192
192
let name_ptr = unsafe { self . string_table ( ) . offset ( self . get ( ) . name_index ( ) as isize ) } ;
193
+
194
+ // strlen without null byte
193
195
let strlen = {
194
196
let mut len = 0 ;
195
197
while unsafe { * name_ptr. offset ( len) } != 0 {
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ impl ModuleTag {
25
25
/// will return `--test cmdline-option`.
26
26
pub fn cmdline ( & self ) -> Result < & str , Utf8Error > {
27
27
use core:: { mem, slice, str} ;
28
+ // strlen without null byte
28
29
let strlen = self . size as usize - mem:: size_of :: < ModuleTag > ( ) ;
29
30
let bytes = unsafe { slice:: from_raw_parts ( ( & self . cmdline_str ) as * const u8 , strlen) } ;
30
31
str:: from_utf8 ( bytes)
@@ -88,3 +89,39 @@ impl<'a> Debug for ModuleIter<'a> {
88
89
list. finish ( )
89
90
}
90
91
}
92
+
93
+ #[ cfg( test) ]
94
+ mod tests {
95
+ use crate :: TagType ;
96
+
97
+ const MSG : & str = "hello" ;
98
+
99
+ /// Returns the tag structure in bytes in native endian format.
100
+ fn get_bytes ( ) -> std:: vec:: Vec < u8 > {
101
+ // size is: 4 bytes for tag + 4 bytes for size + length of null-terminated string
102
+ // 4 bytes mod_start + 4 bytes mod_end
103
+ let size = ( 4 + 4 + 4 + 4 + MSG . as_bytes ( ) . len ( ) + 1 ) as u32 ;
104
+ [
105
+ & ( ( TagType :: Module as u32 ) . to_ne_bytes ( ) ) ,
106
+ & size. to_ne_bytes ( ) ,
107
+ & 0_u32 . to_ne_bytes ( ) ,
108
+ & 0_u32 . to_ne_bytes ( ) ,
109
+ MSG . as_bytes ( ) ,
110
+ // Null Byte
111
+ & [ 0 ] ,
112
+ ]
113
+ . iter ( )
114
+ . flat_map ( |bytes| bytes. iter ( ) )
115
+ . copied ( )
116
+ . collect ( )
117
+ }
118
+
119
+ /// Tests to parse a string with a terminating null byte from the tag (as the spec defines).
120
+ #[ test]
121
+ fn test_parse_str ( ) {
122
+ let tag = get_bytes ( ) ;
123
+ let tag = unsafe { tag. as_ptr ( ) . cast :: < super :: ModuleTag > ( ) . as_ref ( ) . unwrap ( ) } ;
124
+ assert_eq ! ( { tag. typ } , TagType :: Module ) ;
125
+ assert_eq ! ( tag. cmdline( ) . expect( "must be valid UTF-8" ) , MSG ) ;
126
+ }
127
+ }
You can’t perform that action at this time.
0 commit comments