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
DOC_VALID() usage correctness #3872
Comments
Sigh, welcome to asynchronous programming and state changing beneath you.
Document objects are never freed, so you will never get an invalid pointer, so thats one less worry. So no need to check if its in the document array, it will be. But since closed document objects are reused for a different file you are correct that simply testing See plugin API docs. The exact semantics of this changed at one point (vague memory, it was a while ago) and I bet lots of plugins didn't get updated, and new plugin writers didn't RTF above documentation. 😁 Using Also its not guaranteed that Geany callbacks "do the right thing" either, Footnotes
|
@elextr Thanks for the answer.
Alright, that's the piece of information I was missing - I never actually looked at the implementation until now, and yes, there are the pointers from the array that get reused so the code does the "right thing" even though checking the ID would be better. |
No, actually checking
So now, unless Geany checks both After finding And of course the sequence above is just one way for it to happen. But I suspect a lot of Geany doesn't check |
Yes, but I think (3) is really rare - closing documents is typically done by users; if plugins do that, it would be very unexpected. That doesn't mean there couldn't be some races like that when a user closes a document, opens another, and only after that an asynchronous operation with a doc argument finishes. |
Yes agree, but any scenario that is possible can occur, and the case of the user closes document while dialog open has been reported already, and see #2599 for a non-KDE example. Since we don't have many occurrences its not something that needs rushing into, just put it on the eternal todo list and consider it when "somebody" addresses one of the issues. |
While looking at some Geany code, I noticed that
DOC_VALID()
is used in some callbacks where it's supposed to check whether the document was closed in the meantime:geany/src/document.c
Lines 1282 to 1292 in 11b4a00
Unless I'm missing something, I don't think it's correct - if the pointer to the
doc
was freed in the meantime,DOC_VALID()
usage would lead to memory access over invalid pointer.In the LSP plugin plugin I have to deal with a similar situation where I cannot be sure whether the document still exists when I receive a response from the LSP server as a callback. I use this function:
Even this function isn't 100% correct - the original document could have been closed and a new document created at the same time and the new document's pointer could be identical to the closed document's pointer so
doc
would be valid but pointing to a different document. (I decided to accept this limitation as I think it's not so frequent.)My question is, shouldn't we do something similar in Geany?
The text was updated successfully, but these errors were encountered: