8000 Delete the copy constructor and copy assignment operators for StaticJsonBufferBase. by luisrayas3 · Pull Request #524 · bblanchon/ArduinoJson · GitHub
[go: up one dir, main page]

Skip to content

Delete the copy constructor and copy assignment operators for StaticJsonBufferBase. #524

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

Closed

Conversation

luisrayas3
Copy link
@luisrayas3 luisrayas3 commented Jun 12, 2017

These are always invalid, regardless of usage of objects created in the
buffer(s). The root of the problem with using the default copy operators is that
the _buffer pointer is copied as well, which means that the copied-to buffer
now points to the copied-from buffer's memory.

Consider the clearing example at the bottom of
https://arduinojson.org/faq/how-to-reuse-a-jsonbuffer/

StaticJsonBuffer<200> buf{};

JsonObject& obj = buf.createObject();
// Use obj, etc.

buf = StaticJsonBuffer<200>();
// StaticJsonBufferBase::_buffer points to deallocated memory of the temporary

JsonObject& new_obj = buf.createObject();
// Even if I never touch `obj` again, new_obj can easily become corrupted.

Properly implemented copy operators will have to be provided by the concrete
buffer types (e.g. StaticJsonBuffer) and something sensible needs to be
done about mis-matched capacities.

…sonBufferBase.

These are always invalid, regardless of usage of objects created in the
buffer(s). The root of the problem with using the default copy operators is that
the `_buffer` pointer is copied as well, which means that the copied-to buffer
now points to the copied-from buffer's memory.

Consider the clearing example at the bottom of
https://bblanchon.github.io/ArduinoJson/faq/how-to-reuse-a-jsonbuffer/

```
StaticJsonBuffer<200> buf{};

JsonObject& obj = buf.createObject();
// Use obj, etc.

buf = StaticJsonBuffer<200>();
// StaticJsonBufferBase::_buffer points to deallocated memory of the temporary

JsonObject& new_obj = buf.createObject();
// Even if I never touch `obj` again, new_obj can easily become corrupted.
```

Copy-assignment could be easily overloaded to be correct, by simply copying
everything but the `_buffer` pointer. But a properly implemented copy
constructor will have to be provided by the concrete buffer types (e.g.
`StaticJsonBuffer`) and call `StaticJsonBufferBase`s public constructor with the
right capacity and `char*` pointer.
@luisrayas3 luisrayas3 changed the title Delete the copy constructor and copy assignment operators for StaticJ… Delete the copy constructor and copy assignment operators for StaticJsonBufferBase. Jun 12, 2017
@bblanchon
Copy link
Owner

You're right 😲

Don't bother with the pull request, I'll fix that myself.

Thank you very much 👍

@bblanchon bblanchon added the bug label Jun 12, 2017
bblanchon added a commit that referenced this pull request Jun 13, 2017
bblanchon added a commit that referenced this pull request Jun 17, 2017
@bblanchon bblanchon closed this Jun 17, 2017
Repository owner locked and limited conversation to collaborators Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0