target/image: report error if ELF file contains no loadable sections 72/6172/2
authorChristian Hoff <christian.hoff@advantest.com>
Tue, 20 Apr 2021 17:14:30 +0000 (19:14 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 1 May 2021 12:36:28 +0000 (13:36 +0100)
The existing code asserted in that case, which is not correct. This
would allow the user to crash OpenOCD with a bad ELF file, which is
not what we want. A proper error should be reported in that case and
OpenOCD should not crash.

Change-Id: Ied5a6a6fd4ee0fd163f3fe850d304a121ecbe33a
Signed-off-by: Christian Hoff <christian.hoff@advantest.com>
Reviewed-on: http://openocd.zylin.com/6172
Reviewed-by: Jonathan McDowell <noodles-openocd@earth.li>
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
src/target/image.c

index 9608375a5c0e72be00cd4c03b45042333b1a84aa..8f72329bdb2d8d40e7c35a8d3de249bb6ff85c67 100644 (file)
@@ -426,7 +426,10 @@ static int image_elf32_read_headers(struct image *image)
                        (field32(elf, elf->segments32[i].p_filesz) != 0))
                        image->num_sections++;
 
-       assert(image->num_sections > 0);
+       if (image->num_sections == 0) {
+               LOG_ERROR("invalid ELF file, no loadable segments");
+               return ERROR_IMAGE_FORMAT_ERROR;
+       }
 
        /**
         * some ELF linkers produce binaries with *all* the program header
@@ -548,7 +551,10 @@ static int image_elf64_read_headers(struct image *image)
                        (field64(elf, elf->segments64[i].p_filesz) != 0))
                        image->num_sections++;
 
-       assert(image->num_sections > 0);
+       if (image->num_sections == 0) {
+               LOG_ERROR("invalid ELF file, no loadable segments");
+               return ERROR_IMAGE_FORMAT_ERROR;
+       }
 
        /**
         * some ELF linkers produce binaries with *all* the program header

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)