sc/qa/filter/html/data/bad-json.html |    4 ++++
 sc/qa/filter/html/html.cxx           |   15 +++++++++++++++
 sc/source/filter/html/htmlpars.cxx   |   10 +++++++++-
 3 files changed, 28 insertions(+), 1 deletion(-)

New commits:
commit 45c7bb44b4d76a387dbb0dc6b10e353440cb3923
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue Aug 27 10:06:59 2024 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Tue Aug 27 11:11:50 2024 +0200

    tdf#162574 sc HTML paste: handle not well-formed json for td 
data-sheets-value
    
    Regression from commit f5f6db55a5938f37a1c136be904ad7f10a3438ef
    (tdf#159483 sc HTML paste: handle data-sheets-value here, too,
    2024-02-08), in case the HTML markup is like:
    
    <td data-sheets-value="... garbage ...">
    
    then just ignore it, as if the attribute would be missing. We're only
    interested in the case where it contains well-formed JSON.
    
    Change-Id: Id625205e56ae2bfe05a26296a1a0f3abf6d5a4f2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172452
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/sc/qa/filter/html/data/bad-json.html 
b/sc/qa/filter/html/data/bad-json.html
new file mode 100644
index 000000000000..3a978e777f6d
--- /dev/null
+++ b/sc/qa/filter/html/data/bad-json.html
@@ -0,0 +1,4 @@
+<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><table>
+<tbody>
+<tr><td data-sheets-value="{"></td></tr>
+</tbody></table></div>
diff --git a/sc/qa/filter/html/html.cxx b/sc/qa/filter/html/html.cxx
index 38b0500297ba..9e60af2565f9 100644
--- a/sc/qa/filter/html/html.cxx
+++ b/sc/qa/filter/html/html.cxx
@@ -85,6 +85,21 @@ CPPUNIT_TEST_FIXTURE(Test, testPasteTdAsText)
     CPPUNIT_ASSERT_EQUAL(CELLTYPE_STRING, eCellType);
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testPasteBadJson)
+{
+    createScDoc();
+
+    // Just care we don't crash on not-well-formed JSON:
+    ScDocument* pDoc = getScDoc();
+    ScAddress aCellPos(/*nColP=*/0, /*nRowP=*/0, /*nTabP=*/0);
+    ScImportExport aImporter(*pDoc, aCellPos);
+    SvFileStream aFile(createFileURL(u"bad-json.html"), StreamMode::READ);
+    SvMemoryStream aMemory;
+    aMemory.WriteStream(aFile);
+    aMemory.Seek(0);
+    CPPUNIT_ASSERT(aImporter.ImportStream(aMemory, OUString(), 
SotClipboardFormatId::HTML));
+}
+
 CPPUNIT_TEST_FIXTURE(Test, testPasteTdAsBools)
 {
     // Given an empty document:
diff --git a/sc/source/filter/html/htmlpars.cxx 
b/sc/source/filter/html/htmlpars.cxx
index 2ef84c4cdb12..cf4aa77a3ae3 100644
--- a/sc/source/filter/html/htmlpars.cxx
+++ b/sc/source/filter/html/htmlpars.cxx
@@ -84,7 +84,15 @@ void ParseDataSheetsValue(const OUString& rDataSheetsValue, 
std::optional<OUStri
     const char* pEncodedOption = aEncodedOption.getStr();
     std::stringstream aStream(pEncodedOption);
     boost::property_tree::ptree aTree;
-    boost::property_tree::read_json(aStream, aTree);
+    try
+    {
+        boost::property_tree::read_json(aStream, aTree);
+    }
+    catch (const std::exception&)
+    {
+        SAL_WARN("sc", "ParseDataSheetsValue: not well-formed json");
+        return;
+    }
     // The "1" key describes the original data type.
     auto it = aTree.find("1");
     if (it != aTree.not_found())

Reply via email to