From ac03df36406e9a1ebc000bf4ba2bb9d4890ec360 Mon Sep 17 00:00:00 2001 From: Anders Rein Date: Mon, 4 Sep 2023 15:53:35 +0200 Subject: [PATCH 1/3] Fixed issue of segfaulting when not providing any input files Also made sure that braw-decode provide a non-zero exit code when there are too many or too few input files in the command line argument. --- src/braw.cpp | 4 +++- src/main.cpp | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/braw.cpp b/src/braw.cpp index 5208c0c..f365d70 100644 --- a/src/braw.cpp +++ b/src/braw.cpp @@ -117,7 +117,9 @@ Braw::Braw() Braw::~Braw() { - codec->FlushJobs(); + if (codec) { + codec->FlushJobs(); + } if (clip != nullptr) clip->Release(); diff --git a/src/main.cpp b/src/main.cpp index dc81b98..2b8755c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,12 +31,16 @@ int main(int argc, char *argv[]) if(files.size() == 1) { braw_decoder.openFile(files[0]); - }else{ + } else if (files.size() == 0) { + std::cerr << "Missing input file argument" << std::endl; + return 1; + } else { std::cerr << "Too many files provided" << std::endl; for(int i = 0; i != files.size(); ++i) { std::cerr << "[" << i << "] " << files[i] << std::endl; } + return 1; } return 0; From f6a5cf47205da4c6f3ad11f8c75ab04c585705b2 Mon Sep 17 00:00:00 2001 From: Anders Rein Date: Mon, 4 Sep 2023 15:58:36 +0200 Subject: [PATCH 2/3] Made sure non-zero exit code is returned when invalid command line parameters are provided --- src/argparse.cpp | 2 -- src/main.cpp | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/argparse.cpp b/src/argparse.cpp index ee632ad..4749aab 100644 --- a/src/argparse.cpp +++ b/src/argparse.cpp @@ -48,8 +48,6 @@ void ArgParse::printHelp() } } - - std::exit(0); } void ArgParse::parse() diff --git a/src/main.cpp b/src/main.cpp index 2b8755c..e4a100e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ void test_func() void help() { args->printHelp(); + std::exit(0); } From c5b38fa06bd1ab9ddecb68f16f56eb8e108639d5 Mon Sep 17 00:00:00 2001 From: Anders Rein Date: Mon, 4 Sep 2023 16:17:47 +0200 Subject: [PATCH 3/3] Improved error handling --- src/braw.cpp | 16 +++++++++++++++- src/main.cpp | 7 ++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/braw.cpp b/src/braw.cpp index f365d70..efdd292 100644 --- a/src/braw.cpp +++ b/src/braw.cpp @@ -138,9 +138,23 @@ void Braw::openFile(std::string filepath) // Setup BRAW SDK factory = CreateBlackmagicRawFactoryInstanceFromPath(lib); + if (factory == nullptr) { + throw std::runtime_error("Failed to initialize BRAW SDK"); + } factory->CreateCodec(&codec); const char *c = info->filename.c_str(); - codec->OpenClip(c, &clip); + HRESULT hr = codec->OpenClip(c, &clip); + if (hr == E_INVALIDARG) { + throw std::runtime_error("Invalid filename: " + filepath); + } + if (hr == E_FAIL) { + throw std::runtime_error("Failed to open clip. File might be corrupt"); + } + if (hr != S_OK) { + std::stringstream ss; + ss << "Internal Braw error. HRESULT=" << hr; + throw std::runtime_error(ss.str()); + } codec->SetCallback(&frameProcessor); clip->GetFrameCount(&(info->frameCount)); diff --git a/src/main.cpp b/src/main.cpp index e4a100e..e380ca3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,12 @@ int main(int argc, char *argv[]) std::vector files = args->getArgsRemaining(); if(files.size() == 1) { - braw_decoder.openFile(files[0]); + try { + braw_decoder.openFile(files[0]); + } catch (std::exception& e) { + std::cerr << "ERROR: " << e.what() << std::endl; + return 1; + } } else if (files.size() == 0) { std::cerr << "Missing input file argument" << std::endl; return 1;