sc/inc/table.hxx                 |    2 ++
 sc/source/core/data/document.cxx |   12 ++++++++----
 sc/source/core/data/table2.cxx   |   15 +++++++++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)

New commits:
commit e331ce93775c11bdfc2c8782bc1217c835d01b93
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Thu Feb 17 15:27:59 2022 +0100
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Fri Feb 18 20:42:15 2022 +0100

    fix range checking in calls like ScDocument::GetNote()
    
    Change-Id: I5612e765b3484b0515f4a16030ee19133ae3126a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130076
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>
    (cherry picked from commit 957d99a539df6e21fd40370938ca5dab1613cf8c)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130104
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index fa8d6326d479..e61f2f717d89 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -473,6 +473,8 @@ public:
     void        GetLastDataPos(SCCOL& rCol, SCROW& rRow) const;
 
     std::unique_ptr<ScPostIt> ReleaseNote( SCCOL nCol, SCROW nRow );
+    ScPostIt*                 GetNote( SCCOL nCol, SCROW nRow );
+    void                      SetNote( SCCOL nCol, SCROW nRow, 
std::unique_ptr<ScPostIt> pNote );
 
     size_t GetNoteCount( SCCOL nCol ) const;
     SCROW GetNotePosition( SCCOL nCol, size_t nIndex ) const;
diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 3592eb4a3ee1..b62407916300 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -6520,9 +6520,8 @@ ScPostIt* ScDocument::GetNote(const ScAddress& rPos)
 
 ScPostIt* ScDocument::GetNote(SCCOL nCol, SCROW nRow, SCTAB nTab)
 {
-    if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) &&
-        nCol < maTabs[nTab]->GetAllocatedColumnsCount())
-        return maTabs[nTab]->aCol[nCol].GetCellNote(nRow);
+    if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()))
+        return maTabs[nTab]->GetNote(nCol, nRow);
     else
         return nullptr;
 
@@ -6535,7 +6534,8 @@ void ScDocument::SetNote(const ScAddress& rPos, 
std::unique_ptr<ScPostIt> pNote)
 
 void ScDocument::SetNote(SCCOL nCol, SCROW nRow, SCTAB nTab, 
std::unique_ptr<ScPostIt> pNote)
 {
-    return maTabs[nTab]->CreateColumnIfNotExists(nCol).SetCellNote(nRow, 
std::move(pNote));
+    if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()))
+        maTabs[nTab]->SetNote(nCol, nRow, std::move(pNote));
 }
 
 bool ScDocument::HasNote(const ScAddress& rPos) const
@@ -6582,6 +6582,9 @@ bool ScDocument::HasColNotes(SCCOL nCol, SCTAB nTab) const
     if (!pTab)
         return false;
 
+    if (nCol >= pTab->GetAllocatedColumnsCount())
+        return false;
+
     return pTab->aCol[nCol].HasCellNotes();
 }
 
@@ -6625,6 +6628,7 @@ ScPostIt* ScDocument::GetOrCreateNote(const ScAddress& 
rPos)
     else
         return CreateNote(rPos);
 }
+
 ScPostIt* ScDocument::CreateNote(const ScAddress& rPos)
 {
     ScPostIt* pPostIt = new ScPostIt(*this, rPos);
diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx
index 027e61d0ab7b..0ab94dea70bc 100644
--- a/sc/source/core/data/table2.cxx
+++ b/sc/source/core/data/table2.cxx
@@ -1818,6 +1818,21 @@ std::unique_ptr<ScPostIt> ScTable::ReleaseNote( SCCOL 
nCol, SCROW nRow )
     return aCol[nCol].ReleaseNote(nRow);
 }
 
+ScPostIt* ScTable::GetNote( SCCOL nCol, SCROW nRow )
+{
+    if (!ValidCol(nCol) || nCol >= GetAllocatedColumnsCount())
+        return nullptr;
+    return aCol[nCol].GetCellNote(nRow);
+}
+
+void ScTable::SetNote( SCCOL nCol, SCROW nRow, std::unique_ptr<ScPostIt> pNote 
)
+{
+    if (!ValidColRow(nCol, nRow))
+        return;
+
+    CreateColumnIfNotExists(nCol).SetCellNote(nRow, std::move(pNote));
+}
+
 size_t ScTable::GetNoteCount( SCCOL nCol ) const
 {
     if (!ValidCol(nCol) || nCol >= GetAllocatedColumnsCount())

Reply via email to