ENH: improve read handling of bad streams #1033

- a failed attempt to read a value (eg, word, label, scalar) from a
  stream now always provokes a FatalIOError.
  This helps avoid some difficult to trace input errors.
This commit is contained in:
Mark Olesen
2018-11-01 15:59:25 +00:00
parent 3f017a01c0
commit 0ce7e364a4
14 changed files with 120 additions and 90 deletions

View File

@ -208,12 +208,9 @@ Foam::Istream& Foam::ITstream::read(token& tok)
{ {
if (eof()) if (eof())
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "attempt to read beyond EOF"
*this
) << "attempt to read beyond EOF"
<< exit(FatalIOError); << exit(FatalIOError);
setBad(); setBad();
} }
else else

View File

@ -54,13 +54,11 @@ void Foam::dimensioned<Type>::initialize(Istream& is)
if (dims != dimensions_) if (dims != dimensions_)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(is)
( << "The dimensions " << dims
is << " provided do not match the required dimensions "
) << "The dimensions " << dims << dimensions_
<< " provided do not match the required dimensions " << abort(FatalIOError);
<< dimensions_
<< abort(FatalIOError);
} }
} }

View File

@ -40,6 +40,7 @@ const Scalar pTraits<Scalar>::rootMax = ScalarROOTVGREAT;
const char* const pTraits<Scalar>::componentNames[] = { "" }; const char* const pTraits<Scalar>::componentNames[] = { "" };
pTraits<Scalar>::pTraits(const Scalar& val) pTraits<Scalar>::pTraits(const Scalar& val)
: :
p_(val) p_(val)
@ -132,6 +133,9 @@ Istream& operator>>(Istream& is, Scalar& val)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get scalar value"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -139,16 +143,18 @@ Istream& operator>>(Istream& is, Scalar& val)
if (t.isNumber()) if (t.isNumber())
{ {
val = t.number(); val = t.number();
is.check(FUNCTION_NAME);
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected Scalar, found " << t.info() << "Wrong token type - expected scalar value, found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is;
} }
is.check(FUNCTION_NAME);
return is; return is;
} }

View File

@ -161,6 +161,9 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get bool"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -171,26 +174,26 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
} }
else if (t.isWord()) else if (t.isWord())
{ {
// Allow reading invalid value, but report immediately // Permit invalid value, but catch immediately for better messages
sw = Switch(t.wordToken(), true); sw = Switch(t.wordToken(), true);
if (!sw.valid()) if (!sw.valid())
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "expected 'true/false', 'on/off' ... found " << t.wordToken() << "Expected 'true/false', 'on/off' ... found "
<< t.wordToken()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected bool, found " << t << "Wrong token type - expected bool, found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -43,6 +43,9 @@ Foam::Istream& Foam::operator>>(Istream& is, direction& d)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get direction"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -53,11 +56,11 @@ Foam::Istream& Foam::operator>>(Istream& is, direction& d)
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected direction, found " << t.info() << "Wrong token type - expected label (direction), found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -80,6 +80,9 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get int32"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -90,11 +93,11 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected int32_t, found " << t.info() << "Wrong token type - expected label (int32), found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -80,6 +80,9 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get int64"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -90,11 +93,11 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected int64_t, found " << t.info() << "Wrong token type - expected label (int64), found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -79,6 +79,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get uint32"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -89,11 +92,11 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected uint32_t, found " << t.info() << "Wrong token type - expected label (uint32), found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -79,6 +79,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get uint64"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
@ -89,11 +92,11 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected uint64_t, found " << t.info() << "Wrong token type - expected label (uint64), found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,31 +36,34 @@ Foam::fileName::fileName(Istream& is)
} }
Foam::Istream& Foam::operator>>(Istream& is, fileName& fn) Foam::Istream& Foam::operator>>(Istream& is, fileName& val)
{ {
token t(is); token t(is);
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get string"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
if (t.isString()) if (t.isString())
{ {
fn = t.stringToken(); val = t.stringToken();
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected string, found " << t.info() << "Wrong token type - expected string, found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }
fn.stripInvalid(); val.stripInvalid();
is.check(FUNCTION_NAME); is.check(FUNCTION_NAME);
return is; return is;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -58,44 +58,46 @@ bool Foam::keyType::match(const std::string& text, bool literal) const
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, keyType& kw) Foam::Istream& Foam::operator>>(Istream& is, keyType& val)
{ {
token t(is); token t(is);
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get a word/regex"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
if (t.isWord()) if (t.isWord())
{ {
kw = t.wordToken(); val = t.wordToken();
} }
else if (t.isString()) else if (t.isString())
{ {
// Assign from string. Set as regular expression. // Assign from string, treat as regular expression
kw = t.stringToken(); val = t.stringToken();
kw.isPattern_ = true; val.isPattern_ = true;
// flag empty strings as an error // Flag empty strings as an error
if (kw.empty()) if (val.empty())
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "empty word/expression " << "Empty word/expression"
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected word or string, found " << "Wrong token type - expected word or string, found "
<< t.info() << t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,27 +36,30 @@ Foam::string::string(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, string& s) Foam::Istream& Foam::operator>>(Istream& is, string& val)
{ {
token t(is); token t(is);
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get string"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
if (t.isString()) if (t.isString())
{ {
s = t.stringToken(); val = t.stringToken();
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected string, found " << t.info() << "Wrong token type - expected string, found "
<< t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,46 +36,47 @@ Foam::word::word(Istream& is)
} }
Foam::Istream& Foam::operator>>(Istream& is, word& w) Foam::Istream& Foam::operator>>(Istream& is, word& val)
{ {
token t(is); token t(is);
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get word"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
if (t.isWord()) if (t.isWord())
{ {
w = t.wordToken(); val = t.wordToken();
} }
else if (t.isString()) else if (t.isString())
{ {
// Try a bit harder and convert string to word // Try a bit harder and convert string to word
w = t.stringToken(); val = t.stringToken();
string::stripInvalid<word>(w); string::stripInvalid<word>(val);
// Flag empty strings and bad chars as an error // Flag empty strings and bad chars as an error
if (w.empty() || w.size() != t.stringToken().size()) if (val.empty() || val.size() != t.stringToken().size())
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected word," << "Empty word or non-word characters "
" found non-word characters "
<< t.info() << t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected word, found " << "Wrong token type - expected word, found "
<< t.info() << t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,43 +45,45 @@ Foam::wordRe::wordRe(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, wordRe& w) Foam::Istream& Foam::operator>>(Istream& is, wordRe& val)
{ {
token t(is); token t(is);
if (!t.good()) if (!t.good())
{ {
FatalIOErrorInFunction(is)
<< "Bad token - could not get wordRe"
<< exit(FatalIOError);
is.setBad(); is.setBad();
return is; return is;
} }
if (t.isWord()) if (t.isWord())
{ {
w = t.wordToken(); val = t.wordToken();
} }
else if (t.isString()) else if (t.isString())
{ {
// Auto-tests for regular expression // Auto-detects regex
w = t.stringToken(); val = t.stringToken();
// flag empty strings as an error // Flag empty strings as an error
if (w.empty()) if (val.empty())
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "empty word/expression " << "Empty word/expression"
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }
} }
else else
{ {
is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected word or string, found " << "Wrong token type - expected word or string, found "
<< t.info() << t.info()
<< exit(FatalIOError); << exit(FatalIOError);
is.setBad();
return is; return is;
} }