Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash when providing (by mistake) a directory instead of a file. #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion checktestdata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cstdlib>
#include <iostream>
#include <filesystem>
#include <fstream>
#include <getopt.h>

Expand Down Expand Up @@ -147,7 +148,17 @@ int main(int argc, char **argv)
// Check for testdata file
fstream fdata;
if ( argc>optind+1 ) {
char *datafile = argv[optind+1];
char *datafile = argv[optind+1];
if (std::filesystem::is_directory(datafile)) {
// Filter directory, as otherwise, tellg() returns
// arbitrary values in readtestdata() which makes
// that function crash. There is a small race
// condition with actually opening the file with
// that name below, but it's not solvable easily
// when sticking to C++ libraries.
cerr << "Expected a file instead of a directory '" << datafile << "'.\n";
exit(exit_failure);
}
ios_base::openmode mode = generate ? ios_base::out|ios_base::trunc|ios_base::binary : ios_base::in|ios_base::binary;
fdata.open(datafile, mode);
if ( fdata.fail() ) {
Expand Down