-
Notifications
You must be signed in to change notification settings - Fork 302
Fixed invalid memory read/segfault #2379
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
Conversation
I have not added any specific tests as I was not able to reproduce the issue using just C code. I my project I am using libyang-python, which actually triggers this and I was able to find the issue by using valgrind plugin. I am still not 100% that the proposed patch is correct solution as this behavior of hash_table is not explained in documentation nor expect by the user. I might even suggest to rather have like internal structure of pointers to the final records and if you need to reallocate, reallocate just that part but keep the returned record pointers valid for the caller.
Hope it helps to explain the problem. Feel free to propose better solution. |
884b088
to
3bc3815
Compare
I have rewritten the bugfix based on comment in the issue. I get checks error related to MSVC, but it seems not to be problem of my code, if I understand correctly the output. If there is some issue with my patch just let me know, I will fix it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code changes seem fine, just a minor issue.
But why do you actually need records not to be reallocated? Is that a libyang requirement or of your application and specific usage?
Well as the leafref link records include eventually cyclic reference to each other. On one side leafref is having refrerence its target while target is able to see which leafrefs are pointing to it. The code always build both references and similarly it removes it from references in case of removal. And the removal is the problem as I need to make sure that record entry in memory is still valid regardless of operation done on target node. Unfortunately in case of remove it is not true as removal of reference can lead to having just an empty record with no targets or leafrefs. In that case I wanted to remove the record from good from memory as it is no-use. |
3bc3815
to
49b8153
Compare
How? I see that |
Yes and no. The relationship originally should be as follows:
Because you asked me to not modify lyd_node_term structure, we are using hash_table instead which changes the relationship as follows:
So the cyclic relationship is there like this:
Hope it helps to understand the problem |
So the link |
Yes, it is not directly link/pointer. So let's say that behavior of hash_table is as it is and the scope of my issue just that function. So I have two solutions:
Either way it will work for my case. I would prefer to keep the current proposed solution (1). Is that ok with you? Or do you prefer different solution? |
I think there is another solution.
And I think I prefer this one except there is one API function |
I don't think it is good idea. The record itself includes two arrays of pointers to nodes, so if I return a copy, shall this be copied as well (a.k.a. deepcopy) or just copy array pointer (a.k.a. shallow copy). I guess you are assuming doing shallow-copy. But then when it comes to array deletion, the pointer in hash_table record can be left in invalid state after removal of final element from array. Deepcopy is even worse. The other question would be who would release the memory of the "copy" record and when? I can imagine that it can be put on caller stack, so freeing memory will be done automatically once the record is not needed anymore. That being said, I would rather not change it in this way. But rather use solution 1 or 2. |
Okay, fair enough, solution 1 will do. |
07d424b
to
0dfbc87
Compare
This patch fixes segfaults caused by invalid memory read during leafref links removal process. It is tight hash_table implementation, as hash_table removal of record can invalidate all other record pointers due to memory reallocation process
This patch fixes segfaults caused by invalid memory read during leafref links removal process. It is tight hash_table implementation, as hash_table removal of record can invalidate all other record pointers due to memory reallocation process
Related to issue #2374