https://bugs.documentfoundation.org/show_bug.cgi?id=163691

            Bug ID: 163691
           Summary: Use std::copy() instead of memcpy()
           Product: LibreOffice
           Version: unspecified
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: medium
         Component: Writer
          Assignee: [email protected]
          Reporter: [email protected]

Description:
In LibreOffice core, there are many places where a block of memory is copied
from one place to another. In this case, two functions are used:

1. Older C style memcpy(), which is defined in <string.h> (alternatively
<cstring>) and is used as:

memcpy( to, from, count ); // to and from are void* pointers
https://en.cppreference.com/w/cpp/string/byte/memcpy

2. Newer C++ style std::copy, which is defined in <algorithm> and is used with
pointers and iterators as:

std::copy( from, from + size, to ); // pointers
std::copy( from_iter_first, from_iter_last, to_iter_first ); // iterators
https://en.cppreference.com/w/cpp/algorithm/copy 

The goal here is to refactor the code and replace the usages of memcpy() with
equivalent std::copy().

Finding instances:
You can find instances of memcpy() usage in C++ code with:

$ git grep -w memcpy *.hxx *.cxx

Currently there are 720 instances of using memcpy in the code.

Notes:
1. Correctly setting the parameters is important, as they are different in
std::copy() and memcpy().

2. You have to make sure that the code behavior does not change with your
patch. If classes with user-defined copy constructor are used instead of basic
data types, you have to make sure that std::copy which invokes copy constructor
does not make any difference.

Example:
You can see the two std::copy() calls (commented) equivalent to memcpy() in
this code snippet:

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

int main()
{
        std::vector<int> from {1,2,3,4,5}, to(5);

        std::memcpy(to.data(), from.data(), 5 * sizeof(int));
//      std::copy(from.data(), from.data() + 5, to.data());
//      std::copy(from.begin(), from.end(), to.begin());

        for (int num : to)
                std::cout << num << " ";
        std::cout << "\n";

        return 0;
}

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to