Skip to content

Commit 7ba23b6

Browse files
authored
Merge pull request #153 from rust-osdev/dev5
multiboot2: builder: add terminating null-bytes to tags that hold a string
2 parents a72a674 + 62b8432 commit 7ba23b6

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

multiboot2/src/boot_loader_name.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ impl BootLoaderNameTag {
2525
#[cfg(feature = "builder")]
2626
pub fn new(name: &str) -> Box<Self> {
2727
let mut bytes: Vec<_> = name.bytes().collect();
28-
bytes.push(0);
28+
if !bytes.ends_with(&[0]) {
29+
// terminating null-byte
30+
bytes.push(0);
31+
}
2932
boxed_dst_tag(TagType::BootLoaderName, &bytes)
3033
}
3134

multiboot2/src/command_line.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ impl CommandLineTag {
3232
#[cfg(feature = "builder")]
3333
pub fn new(command_line: &str) -> Box<Self> {
3434
let mut bytes: Vec<_> = command_line.bytes().collect();
35-
bytes.push(0);
35+
if !bytes.ends_with(&[0]) {
36+
// terminating null-byte
37+
bytes.push(0);
38+
}
3639
boxed_dst_tag(TagType::Cmdline, &bytes)
3740
}
3841

multiboot2/src/module.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ impl ModuleTag {
3030
#[cfg(feature = "builder")]
3131
pub fn new(start: u32, end: u32, cmdline: &str) -> Box<Self> {
3232
let mut cmdline_bytes: Vec<_> = cmdline.bytes().collect();
33-
cmdline_bytes.push(0);
33+
if !cmdline_bytes.ends_with(&[0]) {
34+
// terminating null-byte
35+
cmdline_bytes.push(0);
36+
}
3437
let start_bytes = start.to_le_bytes();
3538
let end_bytes = end.to_le_bytes();
3639
let mut content_bytes = [start_bytes, end_bytes].concat();

0 commit comments

Comments
 (0)