From 487377d1d72f66a2cbfc32cbdb1b1bbe216c4014 Mon Sep 17 00:00:00 2001 From: Rummskartoffel Date: Sun, 16 Jan 2022 14:24:21 +0100 Subject: [PATCH] disasm: Don't fail when trying to disassemble empty files Given an empty file, disasm would try to create a zero-size memory mapping of that file, which would fail with EINVAL. --- Userland/Utilities/disasm.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Userland/Utilities/disasm.cpp b/Userland/Utilities/disasm.cpp index 02ccda6089f..93ecd748456 100644 --- a/Userland/Utilities/disasm.cpp +++ b/Userland/Utilities/disasm.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -27,7 +28,14 @@ ErrorOr serenity_main(Main::Arguments args) args_parser.add_positional_argument(path, "Path to i386 binary file", "path"); args_parser.parse(args); - auto file = TRY(Core::MappedFile::map(path)); + RefPtr file; + u8 const* asm_data = nullptr; + size_t asm_size = 0; + if ((TRY(Core::System::stat(path))).st_size > 0) { + file = TRY(Core::MappedFile::map(path)); + asm_data = static_cast(file->data()); + asm_size = file->size(); + } struct Symbol { size_t value; @@ -41,8 +49,6 @@ ErrorOr serenity_main(Main::Arguments args) }; Vector symbols; - u8 const* asm_data = static_cast(file->data()); - size_t asm_size = file->size(); size_t file_offset = 0; Vector::Iterator current_symbol = symbols.begin(); OwnPtr symbol_provider; // nullptr for non-ELF disassembly.