From 27889739bdb17b6689951fbba811bdb047dea759 Mon Sep 17 00:00:00 2001 From: Mark Ganson TheMarkster <39143564+mwganson@users.noreply.github.com> Date: Mon, 23 Dec 2024 11:18:30 -0600 Subject: [PATCH] [Spreadsheet] fix isValidAlias() (#18567) --- src/Mod/Spreadsheet/App/PropertySheet.cpp | 26 ++++++++++--------- .../src/Mod/Spreadsheet/App/PropertySheet.cpp | 7 ++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Mod/Spreadsheet/App/PropertySheet.cpp b/src/Mod/Spreadsheet/App/PropertySheet.cpp index 9695a1d411..73fb0a11fd 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -129,20 +129,16 @@ const Cell* PropertySheet::getValueFromAlias(const std::string& alias) const bool PropertySheet::isValidCellAddressName(const std::string& candidate) { - static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$"); + /* Check if it matches a cell reference */ + static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})"); boost::cmatch cm; - /* Check if it matches a cell reference */ - if (boost::regex_match(candidate.c_str(), cm, gen)) { - static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})"); + if (boost::regex_match(candidate.c_str(), cm, e)) { + const boost::sub_match colstr = cm[1]; + const boost::sub_match rowstr = cm[2]; - if (boost::regex_match(candidate.c_str(), cm, e)) { - const boost::sub_match colstr = cm[1]; - const boost::sub_match rowstr = cm[2]; - - if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) { - return true; - } + if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) { + return true; } } return false; @@ -150,13 +146,19 @@ bool PropertySheet::isValidCellAddressName(const std::string& candidate) bool PropertySheet::isValidAlias(const std::string& candidate) { + /* Ensure it only contains allowed characters */ + static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$"); + boost::cmatch cm; + if (!boost::regex_match(candidate.c_str(), cm, gen)) { + return false; + } /* Check if it is used before */ if (getValueFromAlias(candidate)) { return false; } - /* check if it would be a valid cell address name, e.g. "A2" or "C3" */ + /* Check if it would be a valid cell address name, e.g. "A2" or "C3" */ if (isValidCellAddressName(candidate)) { return false; } diff --git a/tests/src/Mod/Spreadsheet/App/PropertySheet.cpp b/tests/src/Mod/Spreadsheet/App/PropertySheet.cpp index 4e63b35d03..a3199f8966 100644 --- a/tests/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/tests/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -76,7 +76,12 @@ TEST_F(PropertySheetTest, validAliases) // NOLINT TEST_F(PropertySheetTest, invalidAliases) // NOLINT { - std::vector invalidAliases {"A1", "ZZ1234", "mm"}; + std::vector invalidAliases {"A1", + "ZZ1234", + "mm", + "no spaces allowed", + "\'NoLeadingQuotes"}; + for (const auto& name : invalidAliases) { EXPECT_FALSE(propertySheet()->isValidAlias(name)) << "\"" << name << "\" was accepted as an alias name, and should not be";