Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kamahen opened this issue Dec 21, 2023 · 0 comments
Open

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

kamahen opened this issue Dec 21, 2023 · 0 comments

Comments

@kamahen
Copy link

kamahen commented Dec 21, 2023

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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant