]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/mmap.cc
fix "Mismatched free() / delete / delete []" in simple_buffer
[apt.git] / apt-pkg / contrib / mmap.cc
index 33c33d1c1b7ec437f9627629864f57aa3ab1eac1..74870b404b3f3a9f1d23e72d99002bb84b16ba9b 100644 (file)
@@ -16,7 +16,7 @@
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
 #include <config.h>
 
 #include <apt-pkg/mmap.h>
 #include <config.h>
 
 #include <apt-pkg/mmap.h>
@@ -411,7 +411,7 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
                                                                        /*}}}*/
 // DynamicMMap::WriteString - Write a string to the file               /*{{{*/
 // ---------------------------------------------------------------------
                                                                        /*}}}*/
 // DynamicMMap::WriteString - Write a string to the file               /*{{{*/
 // ---------------------------------------------------------------------
-/* Strings are not aligned to anything */
+/* Strings are aligned to 16 bytes */
 unsigned long DynamicMMap::WriteString(const char *String,
                                       unsigned long Len)
 {
 unsigned long DynamicMMap::WriteString(const char *String,
                                       unsigned long Len)
 {
@@ -419,13 +419,20 @@ unsigned long DynamicMMap::WriteString(const char *String,
       Len = strlen(String);
 
    _error->PushToStack();
       Len = strlen(String);
 
    _error->PushToStack();
-   unsigned long const Result = RawAllocate(Len+1,0);
+   unsigned long Result = RawAllocate(Len+1+sizeof(uint16_t),sizeof(uint16_t));
    bool const newError = _error->PendingError();
    _error->MergeWithStack();
 
    if (Base == NULL || (Result == 0 && newError))
       return 0;
 
    bool const newError = _error->PendingError();
    _error->MergeWithStack();
 
    if (Base == NULL || (Result == 0 && newError))
       return 0;
 
+   if (Len >= std::numeric_limits<uint16_t>::max())
+      abort();
+
+   uint16_t LenToWrite = Len;
+   memcpy((char *)Base + Result, &LenToWrite, sizeof(LenToWrite));
+   Result += + sizeof(LenToWrite);
+
    memcpy((char *)Base + Result,String,Len);
    ((char *)Base)[Result + Len] = 0;
    return Result;
    memcpy((char *)Base + Result,String,Len);
    ((char *)Base)[Result + Len] = 0;
    return Result;