Peter Maydell a1ecb43818 elf_ops: Don't try to g_mapped_file_unref(NULL)
Calling g_mapped_file_unref() on a NULL pointer is not valid, and
glib will assert if you try it.

$ qemu-system-arm -M virt -display none -device loader,file=/tmp/bad.elf
qemu-system-arm: -device loader,file=/tmp/bad.elf: GLib: g_mapped_file_unref: assertion 'file != NULL' failed

(One way to produce an ELF file that fails like this is to copy just
the first 16 bytes of a valid ELF file; this is sufficient to fool
the code in load_elf_ram_sym() into thinking it's an ELF file and
calling load_elf32() or load_elf64().)

The failure-exit path in load_elf can be reached from various points
in execution, and for some of those we haven't yet called
g_mapped_file_new_from_fd().  Add a condition to the unref call so we
only call it if we successfully created the GMappedFile to start with.

This will fix the assertion; for the specific case of the generic
loader it will then fall back from "guess this is an ELF file" to
"maybe it's a uImage or a hex file" and eventually to "just load as
a raw data file".

Reported-by: Randy Yates <yates@ieee.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Message-Id: <20200423202011.32686-1-peter.maydell@linaro.org>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
2020-05-04 14:35:23 +02:00
..
2019-08-16 13:31:53 +02:00
2019-08-16 13:31:53 +02:00
2020-01-07 17:24:29 +04:00
2020-04-30 11:52:27 +01:00
2019-08-16 13:31:53 +02:00
2019-06-12 13:20:21 +02:00
2019-09-07 08:31:51 +02:00
2020-02-21 09:15:04 +11:00
2019-08-16 13:31:52 +02:00
2020-04-30 11:52:28 +01:00
2019-10-28 19:06:47 +01:00
2019-06-12 13:20:21 +02:00
2019-08-16 13:31:52 +02:00
2019-08-16 13:31:53 +02:00
2019-08-16 13:31:52 +02:00
2020-03-29 09:52:13 -04:00
2019-08-16 13:31:52 +02:00
2018-12-11 15:45:22 -02:00
2019-08-16 13:31:52 +02:00
2019-11-05 23:33:12 +01:00
2019-08-16 13:31:53 +02:00
2019-06-12 13:20:21 +02:00
2020-01-07 16:06:59 +04:00
2019-08-16 13:31:53 +02:00