Skip to content

Commit e521fd4

Browse files
committed
builder: fix wrong returned slice size
1 parent 41e42cf commit e521fd4

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

multiboot2-header/src/builder/header.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ pub struct HeaderBytes {
2121
// Offset into the bytes where the header starts. This is necessary to
2222
// guarantee alignment at the moment.
2323
offset: usize,
24+
structure_len: usize,
2425
bytes: Box<[u8]>,
2526
}
2627

2728
impl HeaderBytes {
2829
/// Returns the bytes. They are guaranteed to be correctly aligned.
2930
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];
3132
// At this point, the alignment is guaranteed. If not, something is
3233
// broken fundamentally.
3334
assert_eq!(slice.as_ptr().align_offset(8), 0);
@@ -166,7 +167,8 @@ impl HeaderBuilder {
166167

167168
// We allocate more than necessary so that we can ensure an correct
168169
// 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;
170172
let mut bytes = Vec::<u8>::with_capacity(alloc_len);
171173
// Pointer to check that no relocation happened.
172174
let alloc_ptr = bytes.as_ptr();
@@ -209,9 +211,11 @@ impl HeaderBuilder {
209211
// We don't want that!
210212
let bytes = unsafe { Box::from_raw(bytes.leak()) };
211213

212-
assert_eq!(bytes.len(), alloc_len);
213-
214-
HeaderBytes { offset, bytes }
214+
HeaderBytes {
215+
offset,
216+
bytes,
217+
structure_len: expected_len,
218+
}
215219
}
216220

217221
/// Helper method that adds all the tags to the given vector.
@@ -223,7 +227,7 @@ impl HeaderBuilder {
223227
false,
224228
);
225229

226-
if let Some(irs) = self.information_request_tag.take() {
230+
if let Some(irs) = self.information_request_tag.clone() {
227231
Self::build_add_bytes(bytes, &irs.build(), false)
228232
}
229233
if let Some(tag) = self.address_tag.as_ref() {
@@ -323,7 +327,7 @@ mod tests {
323327
#[test]
324328
fn test_builder() {
325329
// Step 1/2: Build Header
326-
let mb2_hdr_data = {
330+
let (mb2_hdr_data, expected_len) = {
327331
let builder = HeaderBuilder::new(HeaderTagISA::I386);
328332
// Multiboot2 basic header + end tag
329333
let mut expected_len = 16 + 8;
@@ -359,9 +363,11 @@ mod tests {
359363
println!("builder: {:#?}", builder);
360364
println!("expected_len: {} bytes", builder.expected_len());
361365

362-
builder.build()
366+
(builder.build(), expected_len)
363367
};
364368

369+
assert_eq!(mb2_hdr_data.as_bytes().len(), expected_len);
370+
365371
// Step 2/2: Test the built Header
366372
{
367373
let mb2_hdr = mb2_hdr_data.as_ptr().cast();

multiboot2/src/builder/information.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ pub struct BootInformationBytes {
2020
// Offset into the bytes where the MBI starts. This is necessary to
2121
// guarantee alignment at the moment.
2222
offset: usize,
23+
structure_len: usize,
2324
bytes: Box<[u8]>,
2425
}
2526

2627
impl BootInformationBytes {
2728
/// Returns the bytes. They are guaranteed to be correctly aligned.
2829
pub fn as_bytes(&self) -> &[u8] {
29-
let slice = &self.bytes[self.offset..];
30+
let slice = &self.bytes[self.offset..self.offset + self.structure_len];
3031
// At this point, the alignment is guaranteed. If not, something is
3132
// broken fundamentally.
3233
assert_eq!(slice.as_ptr().align_offset(8), 0);
@@ -186,7 +187,8 @@ impl InformationBuilder {
186187

187188
// We allocate more than necessary so that we can ensure an correct
188189
// alignment within this data.
189-
let alloc_len = self.expected_len() + 7;
190+
let expected_len = self.expected_len();
191+
let alloc_len = expected_len + 7;
190192
let mut bytes = Vec::<u8>::with_capacity(alloc_len);
191193
// Pointer to check that no relocation happened.
192194
let alloc_ptr = bytes.as_ptr();
@@ -231,7 +233,11 @@ impl InformationBuilder {
231233

232234
assert_eq!(bytes.len(), alloc_len);
233235

234-
BootInformationBytes { offset, bytes }
236+
BootInformationBytes {
237+
offset,
238+
bytes,
239+
structure_len: expected_len,
240+
}
235241
}
236242

237243
/// Helper method that adds all the tags to the given vector.
@@ -381,7 +387,7 @@ mod tests {
381387
#[test]
382388
fn test_builder() {
383389
// Step 1/2: Build MBI
384-
let mb2i_data = {
390+
let (mb2i_data, expected_len) = {
385391
let mut builder = InformationBuilder::new();
386392

387393
// Multiboot2 basic information + end tag
@@ -408,9 +414,11 @@ mod tests {
408414
println!("expected_len: {} bytes", builder.expected_len());
409415
assert_eq!(builder.expected_len(), expected_len);
410416

411-
builder.build()
417+
(builder.build(), expected_len)
412418
};
413419

420+
assert_eq!(mb2i_data.as_bytes().len(), expected_len);
421+
414422
// Step 2/2: Test the built MBI
415423
{
416424
let mb2i = unsafe { BootInformation::load(mb2i_data.as_ptr().cast()) }

0 commit comments

Comments
 (0)