Skip to content

Memcpy to nullptr in CSD_HTFC::CSD_HTFC() #274

Open
@kamahen

Description

@kamahen

The GCC compiler warned about memcpy() to nullptr in CSD_HTFC::CSD_HTFC. It appears this was introduced with commit 7e43168.
It's not clear to me whether this bug would have caused a crash or merely have caused the delta encoding to be ineffective; and I don't know how to test my change.
(As a meta-comment, I noticed a number of places where a similar change would improve the code quality -- that is switch from using char* with malloc()/free() or new/delete instead of std::string or std::basic_string or possibly std::vector. Similarly, there are many places where the code could be improved by using std::unique_ptr instead of new/delete.)

A fix for this is here: JanWielemaker@e77521a:

diff --git a/libhdt/src/libdcs/CSD_HTFC.cpp b/libhdt/src/libdcs/CSD_HTFC.cpp
index 6508568..cb795aa 100644
--- a/libhdt/src/libdcs/CSD_HTFC.cpp
+++ b/libhdt/src/libdcs/CSD_HTFC.cpp
@@ -25,6 +25,7 @@
  *   Miguel A. Martinez-Prieto:  migumar2@infor.uva.es
  */
 
+#include <string>
 #include "CSD_HTFC.h"
 
 #if HAVE_CDS
@@ -57,8 +58,9 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,
 
   vector<uint> xblocks; // Temporal storage for start positions
 
-  unsigned char *previousStr = NULL, *currentStr = NULL;
-  uint previousLength = 0, currentLength = 0;
+  std::basic_string<unsigned char> previousStr((const unsigned char*)"");
+  unsigned char *currentStr = NULL;
+  uint currentLength = 0;
 
   while (it->hasNext()) {
     currentStr = it->next();
@@ -99,8 +101,8 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,
       // Regular string
 
       // Calculating the length of the long common prefix
-      uint delta = longest_common_prefix(previousStr, currentStr,
-                                         previousLength, currentLength);
+      uint delta = longest_common_prefix(previousStr.data(), currentStr,
+                                         previousStr.length(), currentLength);
 
       // cout << "Block: " << nblocks << " Pos: "<< length << endl;
       // cout << previousStr << endl << currentStr << endl << " Delta: " <<
@@ -121,8 +123,7 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,
 
     // New string processed
     numstrings++;
-    memcpy(previousStr, currentStr, currentLength);
-    previousLength = currentLength;
+    previousStr.assign(currentStr, currentLength);
 
     it->freeStr(currentStr);
     // NOTIFYCOND(listener, "Converting dictionary to HTFC", length,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions