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

Memory error when moving a cse::uri #361

Closed
kyllingstad opened this issue Sep 10, 2019 · 0 comments
Closed

Memory error when moving a cse::uri #361

kyllingstad opened this issue Sep 10, 2019 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@kyllingstad
Copy link
Member

The following code has a potential memory error:

cse::uri u("/foo");
auto v = std::move(u);

The std::string_view objects that refer to the various URI components (scheme, authority, path, etc.) may point to invalid memory after this.

They are supposed to always point to the cse::uri's internal std::string buffer. However, due to the small string optimisation, where sufficiently small strings may be stored in the memory area occupied by the std::string object itself (typically on the stack), as opposed to a separate (heap) memory area, that buffer may move along with the cse::uri, invalidating the std::string_view objects.

I've run into this error on VS2017, but I would be surprised if it didn't happen on other platforms as well.

@kyllingstad kyllingstad added the bug Something isn't working label Sep 10, 2019
@kyllingstad kyllingstad self-assigned this Sep 10, 2019
kyllingstad added a commit that referenced this issue Sep 16, 2019
I've replaced the internal `std::string_view` members in `cse::uri` with
a new type (`subrange`) which is independent of absolute memory location
but otherwise contains the same information.  To be able to take
advantage of all the nice functionality built into `std::string_view`,
`subrange` is only used for storage, not for processing.
@ljamt ljamt closed this as completed in 59c75f6 Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant