From 31b14797b842a340ae1299601717c78dc5271ca6 Mon Sep 17 00:00:00 2001 From: Pr Janitor Date: Sat, 4 Apr 2026 11:11:05 +0300 Subject: [PATCH] Add null check for file read in get_elf_header function The get_elf_header function now verifies that the file read operation succeeded before accessing the header contents. This prevents undefined behavior when processing corrupted or empty ELF files. --- flows/verilator/src/binary_parser.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/flows/verilator/src/binary_parser.cc b/flows/verilator/src/binary_parser.cc index 0482805..e56227f 100644 --- a/flows/verilator/src/binary_parser.cc +++ b/flows/verilator/src/binary_parser.cc @@ -68,6 +68,9 @@ Elf64_Ehdr get_elf_header(ifstream& file) { file.seekg(0, file.beg); file.read((char*)&header, sizeof(Elf64_Ehdr)); + if (!file || file.gcount() != sizeof(Elf64_Ehdr)) + throw std::runtime_error("Failed to read ELF header"); + if (header.e_machine != EM_RISCV) throw std::runtime_error("Received non-RISC-V binary");