Skip to content

Commit

Permalink
Fix translator crash when provided empty file
Browse files Browse the repository at this point in the history
It's checking file for emptiness before translating

Signed-off-by: Ilya Mashkov <ilya.mashkov@intel.com>
  • Loading branch information
imashkov authored and AlexeySachkov committed Oct 25, 2019
1 parent 4581205 commit 9b898c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/check_empty_file.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; RUN: not llvm-spirv %S/empty_file.bc -o - 2>&1 | FileCheck %s

; CHECK: Can't translate, file is empty
Empty file added test/empty_file.bc
Empty file.
10 changes: 10 additions & 0 deletions tools/llvm-spirv/llvm-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ static int convertLLVMToSPIRV(const SPIRV::TranslatorOpts &Opts) {
return 0;
}

static bool isFileEmpty(const std::string &FileName) {
std::ifstream File(FileName);
return File && File.peek() == EOF;
}

static int convertSPIRVToLLVM(const SPIRV::TranslatorOpts &Opts) {
LLVMContext Context;
std::ifstream IFS(InputFile, std::ios::binary);
Expand Down Expand Up @@ -351,6 +356,11 @@ int main(int Ac, char **Av) {

cl::ParseCommandLineOptions(Ac, Av, "LLVM/SPIR-V translator");

if (InputFile != "-" && isFileEmpty(InputFile)) {
errs() << "Can't translate, file is empty\n";
return -1;
}

SPIRV::TranslatorOpts::ExtensionsStatusMap ExtensionsStatus;
// ExtensionsStatus will be properly initialized and update according to
// values passed via --spirv-ext option in parseSPVExtOption function.
Expand Down

0 comments on commit 9b898c6

Please sign in to comment.