file: don't print ELF flags unless we've verified the bitness.

This was the file(1) crash mentioned in #503.
This commit is contained in:
Elliott Hughes 2024-08-02 16:20:00 -04:00 committed by Rob Landley
parent 17afb6d172
commit d9d0a1c6e6
2 changed files with 5 additions and 1 deletions

View File

@ -70,6 +70,9 @@ toyonly test_line "Android NDK short ELF note" "elf/ndk-elf-note-short" \
"ELF shared object, 32-bit LSB arm, EABI5, soft float, dynamic (/system/bin/linker), for Android 28, BuildID=da6a5f4ca8da163b9339326e626d8a3c, stripped\n" "" ""
toyonly test_line "ELF static fdpic" "elf/fdstatic" \
"ELF executable (fdpic), 32-bit MSB sh, static, stripped\n" "" ""
echo -ne '\x7fELF\00000000000000000000000000000000000000000000' > bad-bits
testing "ELF bad bits" "file bad-bits" "bad-bits: ELF (bad type 12336), (bad class -1) (bad endian 48) unknown arch 12336\n" "" ""
rm -f bad-bits
testing "broken symlink" "file dangler" "dangler: broken symbolic link to $BROKEN\n" "" ""
testing "symlink" "file symlink" "symlink: symbolic link to $LINK\n" "" ""

View File

@ -69,11 +69,12 @@ static void do_elf_file(int fd)
// "x86".
printf("%s", elf_arch_name(arch = elf_int(toybuf+18, 2)));
elf_print_flags(arch, elf_int(toybuf+36+12*bits, 4));
// If what we've seen so far doesn't seem consistent, bail.
if (bail) goto bad;
elf_print_flags(arch, elf_int(toybuf+36+12*bits, 4));
// Stash what we need from the header; it's okay to reuse toybuf after this.
phentsize = elf_int(toybuf+42+12*bits, 2);
phnum = elf_int(toybuf+44+12*bits, 2);