[Libreoffice-commits] core.git: include/tools tools/qa tools/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Fri Jun 19 13:39:29 UTC 2020


 include/tools/json_writer.hxx         |   14 +++++++-------
 tools/qa/cppunit/test_json_writer.cxx |   10 +++++++++-
 tools/source/misc/json_writer.cxx     |   16 +++++++++++-----
 3 files changed, 27 insertions(+), 13 deletions(-)

New commits:
commit 8df7447139b1171d8fd29f5d682b053df5936006
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Fri Jun 19 09:57:58 2020 +0200
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Fri Jun 19 15:38:52 2020 +0200

    asan: need to use malloc instead of new for tools::JsonWriter
    
    because the LOK API expects memory returned from those calls to be
    malloc'ed
    
    Change-Id: If6fbfb60c433bd6e58bc90a9a90a90663e2e1c60
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96676
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx
index c1438783de78..c0312f6c581d 100644
--- a/include/tools/json_writer.hxx
+++ b/include/tools/json_writer.hxx
@@ -11,7 +11,6 @@
 #include <tools/toolsdllapi.h>
 #include <rtl/ustring.hxx>
 #include <algorithm>
-#include <memory>
 
 /** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
  *
@@ -28,7 +27,7 @@ class TOOLS_DLLPUBLIC JsonWriter
     friend class ScopedJsonWriterNode;
 
     int mSpaceAllocated;
-    std::unique_ptr<char[]> maBuffer;
+    char* mpBuffer;
     int mStartNodeCount;
     char* mPos;
     bool mbFirstFieldInNode;
@@ -54,14 +53,15 @@ private:
 
     inline void ensureSpace(int noMoreBytesRequired)
     {
-        int currentUsed = mPos - maBuffer.get();
+        int currentUsed = mPos - mpBuffer;
         if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
         {
             auto newSize = std::max(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2);
-            auto pNew = new char[newSize];
-            memcpy(pNew, maBuffer.get(), currentUsed);
-            maBuffer.reset(pNew);
-            mPos = maBuffer.get();
+            char* pNew = static_cast<char*>(malloc(newSize));
+            memcpy(pNew, mpBuffer, currentUsed);
+            free(mpBuffer);
+            mpBuffer = pNew;
+            mPos = mpBuffer;
         }
     }
 };
diff --git a/tools/qa/cppunit/test_json_writer.cxx b/tools/qa/cppunit/test_json_writer.cxx
index c4e9331c8d17..e27afc95f712 100644
--- a/tools/qa/cppunit/test_json_writer.cxx
+++ b/tools/qa/cppunit/test_json_writer.cxx
@@ -7,6 +7,10 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
+#include <sal/config.h>
+
+#include <cstdlib>
+
 #include <cppunit/extensions/HelperMacros.h>
 #include <test/bootstrapfixture.hxx>
 #include <rtl/ustring.hxx>
@@ -44,7 +48,11 @@ void JsonWriterTest::test1()
         aJson.put("int", 12);
     }
 
-    std::unique_ptr<char[]> result(aJson.extractData());
+    struct Free
+    {
+        void operator()(void* p) const { std::free(p); }
+    };
+    std::unique_ptr<char, Free> result(aJson.extractData());
 
     CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"oustring\": \"val1\", \"ostring\": \"val2\", "
                                      "\"charptr\": \"val3\", \"int\": 12}}"),
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
index 81b22e92a2d1..8a57e2ac337f 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -19,9 +19,9 @@ constexpr int DEFAULT_BUFFER_SIZE = 2048;
 
 JsonWriter::JsonWriter()
     : mSpaceAllocated(DEFAULT_BUFFER_SIZE)
-    , maBuffer(new char[mSpaceAllocated])
+    , mpBuffer(static_cast<char*>(malloc(mSpaceAllocated)))
     , mStartNodeCount(0)
-    , mPos(maBuffer.get())
+    , mPos(mpBuffer)
 {
     *mPos = '{';
     ++mPos;
@@ -29,7 +29,11 @@ JsonWriter::JsonWriter()
     ++mPos;
 }
 
-JsonWriter::~JsonWriter() { assert(!maBuffer && "forgot to extract data?"); }
+JsonWriter::~JsonWriter()
+{
+    assert(!mpBuffer && "forgot to extract data?");
+    free(mpBuffer);
+}
 
 ScopedJsonWriterNode JsonWriter::startNode(const char* pNodeName)
 {
@@ -239,14 +243,16 @@ void JsonWriter::addCommaBeforeField()
 char* JsonWriter::extractData()
 {
     assert(mStartNodeCount == 0 && "did not close all nodes");
-    assert(maBuffer && "data already extracted");
+    assert(mpBuffer && "data already extracted");
     // add closing brace
     *mPos = '}';
     ++mPos;
     // null-terminate
     *mPos = 0;
     mPos = nullptr;
-    return maBuffer.release();
+    char* pRet = nullptr;
+    std::swap(pRet, mpBuffer);
+    return pRet;
 }
 
 } // namespace tools


More information about the Libreoffice-commits mailing list