@@ -21,13 +21,14 @@ pub struct HeaderBytes {
21
21
// Offset into the bytes where the header starts. This is necessary to
22
22
// guarantee alignment at the moment.
23
23
offset : usize ,
24
+ structure_len : usize ,
24
25
bytes : Box < [ u8 ] > ,
25
26
}
26
27
27
28
impl HeaderBytes {
28
29
/// Returns the bytes. They are guaranteed to be correctly aligned.
29
30
pub fn as_bytes ( & self ) -> & [ u8 ] {
30
- let slice = & self . bytes [ self . offset ..] ;
31
+ let slice = & self . bytes [ self . offset ..self . offset + self . structure_len ] ;
31
32
// At this point, the alignment is guaranteed. If not, something is
32
33
// broken fundamentally.
33
34
assert_eq ! ( slice. as_ptr( ) . align_offset( 8 ) , 0 ) ;
@@ -166,7 +167,8 @@ impl HeaderBuilder {
166
167
167
168
// We allocate more than necessary so that we can ensure an correct
168
169
// alignment within this data.
169
- let alloc_len = self . expected_len ( ) + 7 ;
170
+ let expected_len = self . expected_len ( ) ;
171
+ let alloc_len = expected_len + 7 ;
170
172
let mut bytes = Vec :: < u8 > :: with_capacity ( alloc_len) ;
171
173
// Pointer to check that no relocation happened.
172
174
let alloc_ptr = bytes. as_ptr ( ) ;
@@ -209,9 +211,11 @@ impl HeaderBuilder {
209
211
// We don't want that!
210
212
let bytes = unsafe { Box :: from_raw ( bytes. leak ( ) ) } ;
211
213
212
- assert_eq ! ( bytes. len( ) , alloc_len) ;
213
-
214
- HeaderBytes { offset, bytes }
214
+ HeaderBytes {
215
+ offset,
216
+ bytes,
217
+ structure_len : expected_len,
218
+ }
215
219
}
216
220
217
221
/// Helper method that adds all the tags to the given vector.
@@ -223,7 +227,7 @@ impl HeaderBuilder {
223
227
false ,
224
228
) ;
225
229
226
- if let Some ( irs) = self . information_request_tag . take ( ) {
230
+ if let Some ( irs) = self . information_request_tag . clone ( ) {
227
231
Self :: build_add_bytes ( bytes, & irs. build ( ) , false )
228
232
}
229
233
if let Some ( tag) = self . address_tag . as_ref ( ) {
@@ -323,7 +327,7 @@ mod tests {
323
327
#[ test]
324
328
fn test_builder ( ) {
325
329
// Step 1/2: Build Header
326
- let mb2_hdr_data = {
330
+ let ( mb2_hdr_data, expected_len ) = {
327
331
let builder = HeaderBuilder :: new ( HeaderTagISA :: I386 ) ;
328
332
// Multiboot2 basic header + end tag
329
333
let mut expected_len = 16 + 8 ;
@@ -359,9 +363,11 @@ mod tests {
359
363
println ! ( "builder: {:#?}" , builder) ;
360
364
println ! ( "expected_len: {} bytes" , builder. expected_len( ) ) ;
361
365
362
- builder. build ( )
366
+ ( builder. build ( ) , expected_len )
363
367
} ;
364
368
369
+ assert_eq ! ( mb2_hdr_data. as_bytes( ) . len( ) , expected_len) ;
370
+
365
371
// Step 2/2: Test the built Header
366
372
{
367
373
let mb2_hdr = mb2_hdr_data. as_ptr ( ) . cast ( ) ;
0 commit comments