-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Double initialization of uv_tcp_t (probably any other uv_handle_t) cause random segfaults #3266
Comments
this cannot be done, since we make no assumption about the prior state of the memory |
Ok, docs doesn't mention zeroing memory before init.
|
I have proposed fixing this in v2 (#2630), but not yet sure it will get support |
You got my thumbs-up :)
|
W.r.t. #3266 (comment) - I think most are mentioned throughout the docs already, just not in a centralized location. I somewhat feel they're really just special cases of the general ownership issues you have to think about in C but I won't object to a "do's and don'ts" section in the intro if you want to send a pull request. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
For future reference:
You don't have to zero memory before init
This is okay, as long as you run the loop on the same stack or deeper until they're closed (used during tests, can't imagine very many uses of this in real code)
I feel like this is already expected with most C APIs I think a lot of these come from a misunderstanding of how libuv (and most C libraries) treats memory you give it. For uv_*_init, it can't assume anything about it. So it can't check if they're already initialized, etc. Once you have called uv_*_init, you should expect to only be able to free that memory once the callback is called (for requests), or the close callback is called (for handles).
I think there's a mention of this somewhere... |
So, what's the advantage using heap over stack in this case? I am not too familliar with memory-related subject, please explain to me if you don't mind @Matheus28 |
Double initialization of
uv_tcp_t
(probably any otheruv_handle_t
) cause screw onhandle->handle_queue
and random segfaults inuv_*_init
oruv_close
.I know this is obvious. But it took me a few days to find out in a large codebase that this was happening.
My suggestion is to make a flag that protects against re-initialization and to return an error code if this flag is set. In addition, this will save you from those who try to reuse handle's memory without zeroing it.
The text was updated successfully, but these errors were encountered: