Skip to content

Commit c0210c2

Browse files
authored
[Spreadsheet] fix isValidAlias() (FreeCAD#18567)
1 parent 300f3bc commit c0210c2

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/Mod/Spreadsheet/App/PropertySheet.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,34 +129,36 @@ const Cell* PropertySheet::getValueFromAlias(const std::string& alias) const
129129

130130
bool PropertySheet::isValidCellAddressName(const std::string& candidate)
131131
{
132-
static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$");
133-
boost::cmatch cm;
134-
135132
/* Check if it matches a cell reference */
136-
if (boost::regex_match(candidate.c_str(), cm, gen)) {
137-
static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})");
133+
static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})");
134+
boost::cmatch cm;
138135

139-
if (boost::regex_match(candidate.c_str(), cm, e)) {
140-
const boost::sub_match<const char*> colstr = cm[1];
141-
const boost::sub_match<const char*> rowstr = cm[2];
136+
if (boost::regex_match(candidate.c_str(), cm, e)) {
137+
const boost::sub_match<const char*> colstr = cm[1];
138+
const boost::sub_match<const char*> rowstr = cm[2];
142139

143-
if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) {
144-
return true;
145-
}
140+
if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) {
141+
return true;
146142
}
147143
}
148144
return false;
149145
}
150146

151147
bool PropertySheet::isValidAlias(const std::string& candidate)
152148
{
149+
/* Ensure it only contains allowed characters */
150+
static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$");
151+
boost::cmatch cm;
152+
if (!boost::regex_match(candidate.c_str(), cm, gen)) {
153+
return false;
154+
}
153155

154156
/* Check if it is used before */
155157
if (getValueFromAlias(candidate)) {
156158
return false;
157159
}
158160

159-
/* check if it would be a valid cell address name, e.g. "A2" or "C3" */
161+
/* Check if it would be a valid cell address name, e.g. "A2" or "C3" */
160162
if (isValidCellAddressName(candidate)) {
161163
return false;
162164
}

tests/src/Mod/Spreadsheet/App/PropertySheet.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ TEST_F(PropertySheetTest, validAliases) // NOLINT
7676

7777
TEST_F(PropertySheetTest, invalidAliases) // NOLINT
7878
{
79-
std::vector<std::string> invalidAliases {"A1", "ZZ1234", "mm"};
79+
std::vector<std::string> invalidAliases {"A1",
80+
"ZZ1234",
81+
"mm",
82+
"no spaces allowed",
83+
"\'NoLeadingQuotes"};
84+
8085
for (const auto& name : invalidAliases) {
8186
EXPECT_FALSE(propertySheet()->isValidAlias(name))
8287
<< "\"" << name << "\" was accepted as an alias name, and should not be";

0 commit comments

Comments
 (0)