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

[C-API] Http transport doesn't correctly preserve the request body #5890

Closed
nirinchev opened this issue Sep 26, 2022 · 5 comments · Fixed by #5898
Closed

[C-API] Http transport doesn't correctly preserve the request body #5890

nirinchev opened this issue Sep 26, 2022 · 5 comments · Fixed by #5898
Assignees

Comments

@nirinchev
Copy link
Member

When strings are small, they will be allocated on the stack rather than the heap, which means that a pointer to their .data() will become invalid when the string is moved. This seems to be what's happening after #5743 started preserving the request in the completion callback.

realm_http_request_t c_request{realm_http_request_method_e(request.method),
request.url.c_str(),
request.timeout_ms,
c_headers.data(),
c_headers.size(),
request.body.data(),
request.body.size()};
auto completion_data = std::make_unique<HttpCompletionData>(std::move(request), std::move(completion_block));

Here, we construct a realm_http_request_t where the body points to request.body.data() but then we std::move the request to construct completion_data. When the request body is small, e.g. { "name": "a" }, the string will be copied and the old data will now be garbage. I imagine the same issue may affect the url, but it seems our urls are long enough for it not to be a problem.

@nirinchev
Copy link
Member Author

cc @michael-wb - I've been chasing this for a while in dart and this is the best explanation I could come up with. My understanding of C++ is rather superficial though, so there may be something else at play.

@michael-wb
Copy link
Contributor

@nirinchev - I am reworking this area due to requests from @RedBeard0531, so I will look into this issue as part of that.

@michael-wb michael-wb self-assigned this Sep 26, 2022
@RedBeard0531
Copy link
Contributor

@nirinchev's analysis is correct. You need to either copy the request into the CompletionData, or create the CompletionData first, and have the c request refer into its request members.

@sync-by-unito
Copy link

sync-by-unito bot commented Oct 1, 2022

➤ Michael Wilkerson-Barker commented:

Updated send_request_to_server has been fixed in PR 5898
A new c_api test for the network transport has also been added.

@sync-by-unito
Copy link

sync-by-unito bot commented Oct 3, 2022

➤ Michael Wilkerson-Barker commented:

Updated mechanism for the http C_API and added a test to verify operation

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants