From ef02b4ca933fd4c215e169a23b2b069aab712677 Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Mon, 26 Jan 2015 09:43:52 +0100 Subject: [PATCH] multiboot: Simplify to avoid confusing assignment. Found by: Coverity scan. --- grub-core/loader/i386/multiboot_mbi.c | 29 +++++++++++++++--------- grub-core/loader/multiboot_mbi2.c | 32 +++++++++++++++++---------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/grub-core/loader/i386/multiboot_mbi.c b/grub-core/loader/i386/multiboot_mbi.c index f10c087f7..956d0e37b 100644 --- a/grub-core/loader/i386/multiboot_mbi.c +++ b/grub-core/loader/i386/multiboot_mbi.c @@ -121,6 +121,24 @@ load_kernel (grub_file_t file, const char *filename, return grub_multiboot_load_elf (file, filename, buffer); } +static struct multiboot_header * +find_header (char *buffer, grub_ssize_t len) +{ + struct multiboot_header *header; + + /* Look for the multiboot header in the buffer. The header should + be at least 12 bytes and aligned on a 4-byte boundary. */ + for (header = (struct multiboot_header *) buffer; + ((char *) header <= buffer + len - 12); + header = (struct multiboot_header *) ((char *) header + MULTIBOOT_HEADER_ALIGN)) + { + if (header->magic == MULTIBOOT_HEADER_MAGIC + && !(header->magic + header->flags + header->checksum)) + return header; + } + return NULL; +} + grub_err_t grub_multiboot_load (grub_file_t file, const char *filename) { @@ -143,16 +161,7 @@ grub_multiboot_load (grub_file_t file, const char *filename) return grub_errno; } - /* Look for the multiboot header in the buffer. The header should - be at least 12 bytes and aligned on a 4-byte boundary. */ - for (header = (struct multiboot_header *) buffer; - ((char *) header <= buffer + len - 12) || (header = 0); - header = (struct multiboot_header *) ((char *) header + MULTIBOOT_HEADER_ALIGN)) - { - if (header->magic == MULTIBOOT_HEADER_MAGIC - && !(header->magic + header->flags + header->checksum)) - break; - } + header = find_header (buffer, len); if (header == 0) { diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c index 83e8919fa..6f74aeef7 100644 --- a/grub-core/loader/multiboot_mbi2.c +++ b/grub-core/loader/multiboot_mbi2.c @@ -79,6 +79,25 @@ grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize, elf_sections = data; } +static struct multiboot_header * +find_header (grub_properly_aligned_t *buffer, grub_ssize_t len) +{ + struct multiboot_header *header; + /* Look for the multiboot header in the buffer. The header should + be at least 12 bytes and aligned on a 4-byte boundary. */ + for (header = (struct multiboot_header *) buffer; + ((char *) header <= (char *) buffer + len - 12); + header = (struct multiboot_header *) ((grub_uint32_t *) header + MULTIBOOT_HEADER_ALIGN / 4)) + { + if (header->magic == MULTIBOOT_HEADER_MAGIC + && !(header->magic + header->architecture + + header->header_length + header->checksum) + && header->architecture == MULTIBOOT_ARCHITECTURE_CURRENT) + return header; + } + return NULL; +} + grub_err_t grub_multiboot_load (grub_file_t file, const char *filename) { @@ -107,18 +126,7 @@ grub_multiboot_load (grub_file_t file, const char *filename) COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0); - /* Look for the multiboot header in the buffer. The header should - be at least 12 bytes and aligned on a 4-byte boundary. */ - for (header = (struct multiboot_header *) buffer; - ((char *) header <= (char *) buffer + len - 12) || (header = 0); - header = (struct multiboot_header *) ((grub_uint32_t *) header + MULTIBOOT_HEADER_ALIGN / 4)) - { - if (header->magic == MULTIBOOT_HEADER_MAGIC - && !(header->magic + header->architecture - + header->header_length + header->checksum) - && header->architecture == MULTIBOOT_ARCHITECTURE_CURRENT) - break; - } + header = find_header (buffer, len); if (header == 0) {