diff --git a/src/factory/FieldCreateFactory.cpp b/src/factory/FieldCreateFactory.cpp index 76eb7f1..bcf7038 100644 --- a/src/factory/FieldCreateFactory.cpp +++ b/src/factory/FieldCreateFactory.cpp @@ -947,9 +947,41 @@ StructureConstPtr FieldCreate::createStructure () const return createStructure(fieldNames,fields); } +namespace { +void validateFieldName(const std::string& n) +{ + for(size_t i=0, N=n.size(); i='a' && c<='z') {} + else if(c>='A' && c<='Z') {} + else if(c>='0' && c<='9') {} + else { + switch(c){ + case '_': + break; + default: + { + std::ostringstream msg; + msg<<"Invalid charactor '"< sp(new Structure(fieldNames,fields), Field::Deleter()); StructureConstPtr structure = sp; @@ -961,6 +993,7 @@ StructureConstPtr FieldCreate::createStructure ( StringArray const & fieldNames, FieldConstPtrArray const & fields) const { + validateFieldNames(fieldNames); // TODO use std::make_shared std::tr1::shared_ptr sp(new Structure(fieldNames,fields,id), Field::Deleter()); StructureConstPtr structure = sp; @@ -979,6 +1012,7 @@ StructureArrayConstPtr FieldCreate::createStructureArray( UnionConstPtr FieldCreate::createUnion ( StringArray const & fieldNames,FieldConstPtrArray const & fields) const { + validateFieldNames(fieldNames); // TODO use std::make_shared std::tr1::shared_ptr sp(new Union(fieldNames,fields), Field::Deleter()); UnionConstPtr punion = sp; @@ -990,6 +1024,7 @@ UnionConstPtr FieldCreate::createUnion ( StringArray const & fieldNames, FieldConstPtrArray const & fields) const { + validateFieldNames(fieldNames); // TODO use std::make_shared std::tr1::shared_ptr sp(new Union(fieldNames,fields,id), Field::Deleter()); UnionConstPtr punion = sp; @@ -1039,6 +1074,7 @@ StructureConstPtr FieldCreate::appendFields( StringArray const & fieldNames, FieldConstPtrArray const & fields) const { + validateFieldNames(fieldNames); StringArray const & oldNames = structure->getFieldNames(); FieldConstPtrArray const & oldFields = structure->getFields(); size_t oldLen = oldNames.size(); diff --git a/testApp/pv/testPVData.cpp b/testApp/pv/testPVData.cpp index 1c83f88..000d808 100644 --- a/testApp/pv/testPVData.cpp +++ b/testApp/pv/testPVData.cpp @@ -65,6 +65,24 @@ static void testCreatePVStructure() std::cout << "testCreatePVStructure PASSED" << std::endl; } +static void testCreatePVStructureWithInvalidName() +{ + testDiag("testCreatePVStructureWithInvalidName"); + StringArray fieldNames; + fieldNames.push_back("ok"); + fieldNames.push_back("this.is-wrong"); + PVFieldPtrArray pvFields; + pvFields.push_back(pvDataCreate->createPVScalar(pvString)); + pvFields.push_back(pvDataCreate->createPVScalar(pvInt)); + try{ + PVStructurePtr pvParent = pvDataCreate->createPVStructure( + fieldNames,pvFields); + testFail("Creation of invalid field name '%s' was allowed", fieldNames[1].c_str()); + } catch(std::invalid_argument& e) { + testPass("Creation of invalid field name '%s' fails as expected", fieldNames[1].c_str()); + } +} + static void testPVScalarCommon(string /*fieldName*/,ScalarType stype) { PVScalarPtr pvScalar = pvDataCreate->createPVScalar(stype); @@ -641,13 +659,14 @@ static void testFieldAccess() MAIN(testPVData) { - testPlan(242); + testPlan(243); fieldCreate = getFieldCreate(); pvDataCreate = getPVDataCreate(); standardField = getStandardField(); standardPVField = getStandardPVField(); convert = getConvert(); testCreatePVStructure(); + testCreatePVStructureWithInvalidName(); testPVScalar(); testScalarArray(); testRequest();