Skip to content

Commit

Permalink
Fixed #2815 - Python: crash with bad variable name (#2824)
Browse files Browse the repository at this point in the history
* Fixed #2815

* clang-format
  • Loading branch information
clyne committed Jul 24, 2021
1 parent 663a5cd commit d613051
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions include/vapor/XmlNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ class PARAMS_API XmlNode : public Wasp::MyBase {
// I don't know how to export an operator<< !
static ostream &streamOut(ostream &os, const XmlNode &node);

static bool IsValidXMLElement(string s);

private:
static vector<long> _emptyLongVec; // empty elements
static vector<double> _emptyDoubleVec;
Expand Down
41 changes: 21 additions & 20 deletions lib/params/XmlNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ const string StringType = "String";

namespace {

bool isValidXMLElement(string s)
{
if (s.empty()) return (false);
if (!(std::isalpha(s[0]) || s[0] == '_')) return (false);
for (string::const_iterator itr = s.begin(); itr != s.end(); ++itr) {
if (!(std::isalnum(*itr) || std::isdigit(*itr) || *itr == '-' || *itr == '_' || *itr == '.')) { return (false); }
if (isspace(*itr)) return (false);
}

return (true);
}

string escapeStr(string s)
{
Expand All @@ -88,9 +77,21 @@ string escapeStr(string s)
}
}; // namespace

bool XmlNode::IsValidXMLElement(string s)
{
if (s.empty()) return (false);
if (!(std::isalpha(s[0]) || s[0] == '_')) return (false);
for (string::const_iterator itr = s.begin(); itr != s.end(); ++itr) {
if (!(std::isalnum(*itr) || std::isdigit(*itr) || *itr == '-' || *itr == '_' || *itr == '.')) { return (false); }
if (isspace(*itr)) return (false);
}

return (true);
}

XmlNode::XmlNode(const string &tag, const map<string, string> &attrs, size_t numChildrenHint)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));

_longmap.clear();
_doublemap.clear();
Expand All @@ -113,7 +114,7 @@ XmlNode::XmlNode(const string &tag, const map<string, string> &attrs, size_t num

XmlNode::XmlNode(const string &tag, size_t numChildrenHint)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));

_longmap.clear();
_doublemap.clear();
Expand Down Expand Up @@ -221,7 +222,7 @@ XmlNode::~XmlNode()

void XmlNode::SetElementLong(const string &tag, const vector<long> &values)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));
_longmap[tag] = values;
}

Expand All @@ -238,7 +239,7 @@ void XmlNode::SetElementLong(const vector<string> &tags, const vector<long> &val
}

string tag = tags[tags.size() - 1];
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));
currNode->_longmap[tag] = values;
}

Expand All @@ -255,7 +256,7 @@ void XmlNode::SetElementDouble(const vector<string> &tags, const vector<double>
}

string tag = tags[tags.size() - 1];
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));
currNode->_doublemap[tag] = values;
}

Expand All @@ -278,7 +279,7 @@ bool XmlNode::HasElementLong(const string &tag) const

void XmlNode::SetElementDouble(const string &tag, const vector<double> &values)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));
_doublemap[tag] = values;
}

Expand All @@ -302,14 +303,14 @@ bool XmlNode::HasElementDouble(const string &tag) const

void XmlNode::SetElementString(const string &tag, const string &str)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));

_stringmap[tag] = str;
}

void XmlNode::SetElementStringVec(const string &tag, const vector<string> &strvec)
{
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));

string s;
for (int i = 0; i < strvec.size(); i++) {
Expand All @@ -332,7 +333,7 @@ void XmlNode::SetElementStringVec(const vector<string> &tags, const vector<strin
currNode = child;
}
string tag = tags[tags.size() - 1];
VAssert(isValidXMLElement(tag));
VAssert(IsValidXMLElement(tag));

string s;
for (int i = 0; i < strvec.size(); i++) {
Expand Down
7 changes: 7 additions & 0 deletions lib/render/PyEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vapor/utils.h>
#include <vapor/DataMgrUtils.h>
#include <vapor/PyEngine.h>
#include <vapor/XmlNode.h>
using namespace Wasp;
using namespace VAPoR;

Expand Down Expand Up @@ -797,6 +798,12 @@ int PyEngine::_checkOutVars(const vector<string> &outputVarNames) const
{
for (int i = 0; i < outputVarNames.size(); i++) {
string vname = outputVarNames[i];

if (!XmlNode::IsValidXMLElement(vname)) {
SetErrMsg("Invalid variable name: %s ", vname.c_str());
return (-1);
}

if (!_validOutputVar(outputVarNames[i])) {
SetErrMsg("Invalid derived variable name %s. Already in use.", vname.c_str());
return (-1);
Expand Down

0 comments on commit d613051

Please sign in to comment.