[Libreoffice-commits] online.git: kit/DummyLibreOfficeKit.cpp

Tor Lillqvist tml at collabora.com
Wed Jan 10 14:19:03 UTC 2018


 kit/DummyLibreOfficeKit.cpp |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

New commits:
commit 457ec5b2b2d2d5a5b0bd8f3508513492afa74552
Author: Tor Lillqvist <tml at collabora.com>
Date:   Wed Jan 10 14:48:30 2018 +0200

    Use strdup() instead of fragile malloc+strncpy pairs...
    
    ...with manually counted string literal lengths even.
    
    Change-Id: If11c65be25953b710a16d11e790d1b6d40119287

diff --git a/kit/DummyLibreOfficeKit.cpp b/kit/DummyLibreOfficeKit.cpp
index 6f167089..a52f8f66 100644
--- a/kit/DummyLibreOfficeKit.cpp
+++ b/kit/DummyLibreOfficeKit.cpp
@@ -279,8 +279,7 @@ static char* doc_getPartName(LibreOfficeKitDocument* pThis, int nPart)
     (void) pThis;
     (void) nPart;
 
-    char* pMemory = static_cast<char*>(malloc(11));
-    strncpy(pMemory, "Dummy part", 10);
+    char* pMemory = strdup("Dummy part");
     return pMemory;
 
 }
@@ -402,13 +401,11 @@ static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMi
     (void) pMimeType;
     (void) pUsedMimeType;
 
-    char* pMemory = static_cast<char*>(malloc(11));
-    strncpy(pMemory, "Dummy text", 10);
+    char* pMemory = strdup("Dummy text");
 
     if (pUsedMimeType)
     {
-        *pUsedMimeType = static_cast<char*>(malloc(25));
-        strncpy(*pUsedMimeType, "text/plain;charset=utf-8", 24);
+        *pUsedMimeType = strdup("text/plain;charset=utf-8");
     }
 
     return pMemory;
@@ -442,8 +439,7 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCo
     (void) pThis;
     (void) pCommand;
 
-    char* pMemory = static_cast<char*>(malloc(1));
-    strncpy(pMemory, "", 0);
+    char* pMemory = strdup("");
     return pMemory;
 }
 
@@ -527,8 +523,7 @@ static char* lo_getError (LibreOfficeKit *pThis)
 {
     (void) pThis;
 
-    char* pMemory = static_cast<char*>(malloc(12));
-    strncpy(pMemory, "Dummy error", 11);
+    char* pMemory = strdup("Dummy error");
     return pMemory;
 }
 
@@ -569,8 +564,7 @@ static char* lo_getVersionInfo(LibreOfficeKit* /*pThis*/)
         "\"BuildId\": \"1\" "
         "}";
 
-    char* pVersion = static_cast<char*>(malloc(sizeof(version)));
-    strncpy(pVersion, version, sizeof(version) - 1);
+    char* pVersion = strdup(version);
     return pVersion;
 }
 


More information about the Libreoffice-commits mailing list