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

Correct object copying in document.h #1698

Closed
wants to merge 2 commits into from

Conversation

ns-osmolsky
Copy link

@ns-osmolsky ns-osmolsky commented Apr 22, 2020

  • this came up when building the code with GCC9
    • copying C++ objects with memcpy() is only permitted for Trivial types, the rest
      should be copied via copy or assignment
    • tweaked the code to construct the objects in place and then assign

Ran "make test", looks OK.

Fixes #1700

 - this came up when building the code with GCC9
   + copying C++ objects with memcpy() is only permitted for Trivial types, the rest
     should be copied via copy or assignment
   + tweaked the code to construct the objects in place and then assign

Ran "make test", looks OK.
@ns-osmolsky
Copy link
Author

Wow, it looks like the old Visual Studio 2013 does not understand = default.

@miloyip
Copy link
Collaborator

miloyip commented Apr 23, 2020

=default is C++11. Need C++03.

@miloyip
Copy link
Collaborator

miloyip commented Apr 23, 2020

I am not sure if this change would affect runtime performance.

@ns-osmolsky
Copy link
Author

OK, I've just pushed a C++03 workaround.

As for perf, I ran the tests on my VM and don't see any difference.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 99.587% when pulling 6c812e0 on ns-osmolsky:master into 8f4c021 on Tencent:master.

@miloyip
Copy link
Collaborator

miloyip commented Apr 23, 2020

But I think it may have performance overhead, for example, default construction and assignment is non-trivial. Default construction sets type to kNullType, and then assignment needs to first checking the type and deallocate it if needed.

@ns-osmolsky
Copy link
Author

There is definitely overhead, yet it may be insignificant for the types in play here. Do you have a concrete benchmark that you are concerned with?

Or looking at it from the opposite perspective, this change is about correctness. Doing memcpy() on complex C++ objects is crazy.

@miloyip
Copy link
Collaborator

miloyip commented Apr 23, 2020

GenericValue is not really complex object at all. It is not polymorphic. It does not contains non-trival member variables. Actually it is designed for possibility of memcpy.
I don't think it is "incorrect".

@ns-osmolsky
Copy link
Author

Please build with GCC9 and it will tell you that the code is incorrect... even if it happens to work in this special case. What you are doing results in undefined behavior as per the ISO C++ Standard.

@pah
Copy link
Contributor

pah commented Apr 28, 2020

As usual, another possibility is to add a new level of indirection and put all members into a trivial local struct and memcpy that instead of the whole object. This will satisfy the C++ standard and achieves the same behaviour as before.

Edit: Ok, this is not as straight-forward as we keep real objects in the array/object buffers.

@ns-osmolsky
Copy link
Author

s usual, another possibility is to add a new level of indirection and put all members into a trivial local struct and memcpy that instead of the whole object. This will satisfy the C++ standard and achieves the same behaviour as before.

Well, yet that would still involve construction for each object (as I am doing already) followed by a memcpy of a member (while I am doing copy assignment of the whole).

The net result is looks identical to me.

@ns-osmolsky
Copy link
Author

Closing the PR as it is just hanging here:

Hoshpak added a commit to void-linux/void-packages that referenced this pull request Oct 27, 2020
the patch intended to fix compilation broke rapidjson as documented in
#25912
I verified this by compiling and running the test suite with and without
the patch applied.

There is a proposed fix in Tencent/rapidjson#1698
which seemingly was rejected by the upstream developers, the issue
is documented at Tencent/rapidjson#1700.
Since upstream insists that what they are doing is correct, make the
compiler ignore the errors for now. Alternatively, we could still
apply the proposed patch to our package.
Logarithmus pushed a commit to Logarithmus/void-packages that referenced this pull request Nov 15, 2020
the patch intended to fix compilation broke rapidjson as documented in
void-linux#25912
I verified this by compiling and running the test suite with and without
the patch applied.

There is a proposed fix in Tencent/rapidjson#1698
which seemingly was rejected by the upstream developers, the issue
is documented at Tencent/rapidjson#1700.
Since upstream insists that what they are doing is correct, make the
compiler ignore the errors for now. Alternatively, we could still
apply the proposed patch to our package.
@kvtb
Copy link

kvtb commented Oct 12, 2021

the PR is merge error to the latest version, so it is better to keep it open and rebase from time to time

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

Successfully merging this pull request may close these issues.

GCC9 break: trying to memcpy C++ objects
5 participants