Skip to content

Commit 1dc1258

Browse files
committed
test.cpp: check for existence before opening file
1 parent e8c1fd9 commit 1dc1258

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include "simplecpp.h"
20+
1921
#include <iostream>
2022
#include <limits>
2123
#include <fstream>
2224
#include <sstream>
25+
#include <stdexcept>
2326
#include <vector>
24-
#include "simplecpp.h"
2527

2628
enum Input {
2729
Stringstream,
@@ -109,6 +111,8 @@ static simplecpp::TokenList makeTokenListFromFstream(const char code[], std::siz
109111
{
110112
const std::string tmpfile = writeFile(code, size, filename);
111113
std::ifstream fin(tmpfile);
114+
if (!fin.is_open())
115+
throw std::runtime_error("could not open " + tmpfile);
112116
simplecpp::TokenList tokenList(fin, filenames, tmpfile, outputList);
113117
remove(tmpfile.c_str());
114118
return tokenList;
@@ -117,6 +121,9 @@ static simplecpp::TokenList makeTokenListFromFstream(const char code[], std::siz
117121
static simplecpp::TokenList makeTokenListFromFile(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
118122
{
119123
const std::string tmpfile = writeFile(code, size, filename);
124+
std::ifstream fin(tmpfile);
125+
if (!fin.is_open())
126+
throw std::runtime_error("could not open " + tmpfile);
120127
simplecpp::TokenList tokenList(tmpfile, filenames, outputList);
121128
remove(tmpfile.c_str());
122129
return tokenList;

0 commit comments

Comments
 (0)